Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pytube import YouTube
|
3 |
+
import validators
|
4 |
+
import os
|
5 |
+
|
6 |
+
class YouTubeDownloader:
|
7 |
+
def __init__(self):
|
8 |
+
st.set_page_config(page_title="YouTube Video Downloader", layout="wide")
|
9 |
+
self.output_dir = "downloads"
|
10 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
11 |
+
|
12 |
+
def run(self):
|
13 |
+
st.title("YouTube Video Downloader")
|
14 |
+
url = st.text_input("Enter YouTube URL to download:")
|
15 |
+
if url:
|
16 |
+
if self.validate_url(url):
|
17 |
+
video, file_size = self.download_video(url)
|
18 |
+
if video:
|
19 |
+
self.display_video(video)
|
20 |
+
self.cleanup(video)
|
21 |
+
else:
|
22 |
+
st.error("Download failed. Please try again.")
|
23 |
+
|
24 |
+
def validate_url(self, url):
|
25 |
+
if not validators.url(url):
|
26 |
+
st.error("Invalid URL. Please enter a valid YouTube URL.")
|
27 |
+
return False
|
28 |
+
return True
|
29 |
+
|
30 |
+
def download_video(self, url):
|
31 |
+
st.info("Downloading... Please wait.")
|
32 |
+
try:
|
33 |
+
yt = YouTube(url)
|
34 |
+
stream = yt.streams.get_highest_resolution()
|
35 |
+
video = stream.download(output_path=self.output_dir, filename="video")
|
36 |
+
st.success("Downloaded successfully!")
|
37 |
+
|
38 |
+
return video, os.path.getsize(video)
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"An error occurred: {str(e)}")
|
41 |
+
return None, 0
|
42 |
+
|
43 |
+
def display_video(self, video_path):
|
44 |
+
st.video(video_path)
|
45 |
+
|
46 |
+
def cleanup(self, video_path):
|
47 |
+
os.remove(video_path)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
downloader = YouTubeDownloader()
|
51 |
+
downloader.run()
|