|
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:
|
|
|
|
yt = pytube.YouTube(youtube_url)
|
|
|
|
|
|
video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
|
|
|
|
|
print("Downloading...")
|
|
video.download()
|
|
|
|
|
|
original_filename = video.default_filename
|
|
|
|
|
|
new_filename = original_filename[:12] + os.path.splitext(original_filename)[1]
|
|
|
|
|
|
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
|
|
|