futureprogrammer commited on
Commit
01206f7
1 Parent(s): 4d15237

Files Uploaded

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -0
  2. yt-downloader.py +94 -0
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pytube==15.0.0
2
+ streamlit==1.24.1
yt-downloader.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pytube import Playlist, YouTube
3
+ import time
4
+ import os
5
+ import re
6
+
7
+ def sanitize_filename(filename):
8
+ # Remove invalid characters and replace spaces with underscores
9
+ return re.sub(r'[\\/*?:"<>|]', "", filename.replace(" ", "_"))
10
+
11
+ def download_video(URL, save_folder):
12
+ try:
13
+ video = YouTube(URL)
14
+ video_title = sanitize_filename(video.title)
15
+ video_filename = f"{video_title}.mp4"
16
+ file_path = os.path.join(save_folder, video_filename)
17
+
18
+ st.write(f"Checking if file {file_path} exists...")
19
+ if os.path.exists(file_path):
20
+ st.write("File exists. Skipping download.")
21
+ return
22
+ else:
23
+ st.write("File does not exist. Proceeding with download.")
24
+
25
+ st.write(f"\nDownloading your File: {video_title}")
26
+
27
+ if not os.path.exists(save_folder):
28
+ os.makedirs(save_folder)
29
+
30
+ st.write("Downloading video...")
31
+ video.streams.get_highest_resolution().download(output_path=save_folder, filename=video_title)
32
+ st.write('Hurray! Successfully Downloaded')
33
+ except Exception as e:
34
+ st.write("Error occurred while downloading the video:", str(e))
35
+
36
+ return True
37
+
38
+
39
+ def download_playlist(playlist_url, save_folder):
40
+ try:
41
+ playlist = Playlist(playlist_url)
42
+ st.write(f'Downloading your Playlist: {playlist.title}')
43
+
44
+ start_time = time.time()
45
+ total_videos = len(playlist.video_urls)
46
+ downloaded_videos = 0
47
+
48
+ for video_url in playlist.video_urls:
49
+ video = YouTube(video_url)
50
+ video_title = sanitize_filename(video.title)
51
+ video_filename = f"{video_title}.mp4"
52
+ file_path = os.path.join(save_folder, video_filename)
53
+
54
+ st.write(f"Checking if file {file_path} exists...")
55
+ if os.path.exists(file_path):
56
+ st.write(f"Video '{video_title}' already exists, skipping...")
57
+ downloaded_videos += 1
58
+ continue
59
+ else:
60
+ st.write("File does not exist. Proceeding with download.")
61
+
62
+ st.write(f"Downloading video: {video_title}")
63
+ video.streams.get_highest_resolution().download(output_path=save_folder, filename=video_title)
64
+ downloaded_videos += 1
65
+ st.write(f"Downloaded video {downloaded_videos}/{total_videos}")
66
+
67
+ end_time = time.time()
68
+
69
+ st.write(f'Hurray! Successfully Downloaded Playlist: {playlist.title}')
70
+ st.write(f'Total Time Taken: {end_time - start_time:.2f} seconds')
71
+ except Exception as e:
72
+ st.write("Error occurred while processing the playlist:", str(e))
73
+
74
+ return True
75
+
76
+ def main():
77
+ st.title("YouTube Downloader")
78
+ download_type = st.selectbox("What do you want to download?", ["Single Video", "Playlist"])
79
+
80
+ if download_type == "Single Video":
81
+ URL = st.text_input("Enter the YouTube video URL")
82
+ save_folder = st.text_input("Enter the folder path where you want to save the video")
83
+ if st.button("Download Video"):
84
+ if download_video(URL, save_folder):
85
+ st.write("Do you want to download another video?")
86
+ else:
87
+ playlist_url = st.text_input("Enter the YouTube playlist URL")
88
+ save_folder = st.text_input("Enter the folder path where you want to save the videos")
89
+ if st.button("Download Playlist"):
90
+ if download_playlist(playlist_url, save_folder):
91
+ st.write("Do you want to download another playlist?")
92
+
93
+ if __name__ == "__main__":
94
+ main()