Abhaykoul commited on
Commit
0bf364b
1 Parent(s): aea3be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -1,52 +1,56 @@
1
  import streamlit as st
2
  from pytube import YouTube
3
- import validators
4
- from tqdm import tqdm
5
- import os
6
 
7
  class YouTubeDownloader:
8
- def __init__(self):
9
- st.set_page_config(page_title="YouTube Video Downloader", layout="wide")
10
- self.output_dir = "downloads"
11
- os.makedirs(self.output_dir, exist_ok=True)
12
-
13
- def run(self):
14
- st.title("YouTube Video Downloader")
15
  url = st.text_input("Enter YouTube URL to download:")
16
  if url:
17
- if self.validate_url(url):
18
- video, file_size = self.download_video(url)
19
- if video:
20
- self.display_video(video)
21
- self.cleanup(video)
22
- else:
23
- st.error("Download failed. Please try again.")
24
-
25
- def validate_url(self, url):
26
- if not validators.url(url):
27
- st.error("Invalid URL. Please enter a valid YouTube URL.")
28
- return False
29
- return True
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- def download_video(self, url):
32
- st.info("Downloading... Please wait.")
33
- try:
34
- yt = YouTube(url)
35
- stream = yt.streams.get_highest_resolution()
36
- video = stream.download(output_path=self.output_dir, filename="video")
37
- st.success("Downloaded successfully!")
38
 
39
- return video, os.path.getsize(video)
40
- except Exception as e:
41
- st.error(f"An error occurred: {str(e)}")
42
- return None, 0
43
 
44
- def display_video(self, video_path):
45
- st.video(video_path)
 
46
 
47
- def cleanup(self, video_path):
48
- os.remove(video_path)
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
- downloader = YouTubeDownloader()
52
- downloader.run()
 
1
  import streamlit as st
2
  from pytube import YouTube
 
 
 
3
 
4
  class YouTubeDownloader:
5
+ @staticmethod
6
+ def run():
7
+ st.header("YouTube Video Downloader")
 
 
 
 
8
  url = st.text_input("Enter YouTube URL to download:")
9
  if url:
10
+ YouTubeDownloader.validate_url(url)
11
+ with st.expander("Preview video"):
12
+ st.video(url)
13
+ if st.button("Download"):
14
+ YouTubeDownloader.cleanup()
15
+ file_ = YouTubeDownloader.download_video(url)
16
+ st.video(file_)
17
+ YouTubeDownloader.helper_message()
18
+
19
+ @staticmethod
20
+ def download_video(url):
21
+ with st.spinner("Downloading..."):
22
+ local_file = (
23
+ YouTube(url)
24
+ .streams.filter(progressive=True, file_extension="mp4")
25
+ .first()
26
+ .download()
27
+ )
28
+ st.success("Downloaded")
29
+ return local_file
30
+
31
+ @staticmethod
32
+ def validate_url(url):
33
+ import validators
34
 
35
+ if not validators.url(url):
36
+ st.error("Hi there 👋 URL seems invalid 👽")
37
+ st.stop()
 
 
 
 
38
 
39
+ @classmethod
40
+ def cleanup(cls):
41
+ import pathlib
42
+ import glob
43
 
44
+ junks = glob.glob("*.mp4")
45
+ for junk in junks:
46
+ pathlib.Path(junk).unlink()
47
 
48
+ @classmethod
49
+ def helper_message(cls):
50
+ st.write(
51
+ "> To save the video to the local computer, "
52
+ "click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
53
+ )
54
 
55
  if __name__ == "__main__":
56
+ YouTubeDownloader.run()