Salman11223
commited on
Commit
•
456ca01
1
Parent(s):
bbe5dd4
Create download_youtube_video.py
Browse files- download_youtube_video.py +32 -0
download_youtube_video.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytube
|
2 |
+
import os
|
3 |
+
|
4 |
+
def download_youtube_video(youtube_url):
|
5 |
+
"""Downloads a YouTube video, renames it to the first three characters, and returns the downloaded file path."""
|
6 |
+
|
7 |
+
try:
|
8 |
+
# Create a YouTube object
|
9 |
+
yt = pytube.YouTube(youtube_url)
|
10 |
+
|
11 |
+
# Get the highest resolution progressive stream
|
12 |
+
video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
13 |
+
|
14 |
+
# Download the video
|
15 |
+
print("Downloading...")
|
16 |
+
video.download()
|
17 |
+
|
18 |
+
# Get the original filename
|
19 |
+
original_filename = video.default_filename
|
20 |
+
|
21 |
+
# Extract the first three characters and keep the file extension
|
22 |
+
new_filename = original_filename[:12] + os.path.splitext(original_filename)[1]
|
23 |
+
|
24 |
+
# Rename the downloaded file
|
25 |
+
os.rename(original_filename, new_filename)
|
26 |
+
|
27 |
+
print("Download complete! Video saved to:", new_filename)
|
28 |
+
return new_filename
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
print("An error occurred:", e)
|
32 |
+
return None
|