File size: 1,072 Bytes
456ca01
 
062054f
 
456ca01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pytube
import os
from pytube import YouTube


def download_youtube_video(youtube_url):
    """Downloads a YouTube video, renames it to the first three characters, and returns the downloaded file path."""

    try:
        # Create a YouTube object
        yt = pytube.YouTube(youtube_url)

        # Get the highest resolution progressive stream
        video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()

        # Download the video
        print("Downloading...")
        video.download()

        # Get the original filename
        original_filename = video.default_filename

        # Extract the first three characters and keep the file extension
        new_filename = original_filename[:12] + os.path.splitext(original_filename)[1]

        # Rename the downloaded file
        os.rename(original_filename, new_filename)

        print("Download complete! Video saved to:", new_filename)
        return new_filename

    except Exception as e:
        print("An error occurred:", e)
        return None