Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import os
|
4 |
-
import io
|
5 |
|
6 |
-
def
|
7 |
-
"""Downloads
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
return
|
35 |
-
|
36 |
-
def app(video_link):
|
37 |
-
video_data, filename, video_title = download_youtube_video(video_link)
|
38 |
-
if video_data and filename:
|
39 |
-
return (filename, video_data), f"Download successful! Video: {video_title}"
|
40 |
else:
|
41 |
-
return None, "Download failed. Please check the
|
42 |
|
43 |
with gr.Blocks() as interface:
|
44 |
-
gr.Markdown("## YouTube Video Downloader")
|
45 |
-
|
46 |
-
download_button = gr.Button("Download")
|
47 |
-
output = gr.File(label="Downloaded
|
48 |
message = gr.Markdown()
|
49 |
|
50 |
download_button.click(
|
51 |
fn=app,
|
52 |
-
inputs=
|
53 |
outputs=[output, message],
|
54 |
api_name="download"
|
55 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
from pytube import YouTube
|
3 |
import os
|
|
|
4 |
|
5 |
+
def download_youtube_videos(urls):
|
6 |
+
"""Downloads multiple YouTube videos and returns a list of downloaded file paths."""
|
7 |
+
downloaded_files = []
|
8 |
+
for url in urls:
|
9 |
+
try:
|
10 |
+
yt = YouTube(url)
|
11 |
+
video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
12 |
+
filename = f"{yt.title}.mp4"
|
13 |
+
video.download(filename=filename)
|
14 |
+
downloaded_files.append(filename)
|
15 |
+
except Exception as e:
|
16 |
+
print(f"Error downloading {url}: {str(e)}")
|
17 |
+
|
18 |
+
return downloaded_files
|
19 |
+
|
20 |
+
def app(urls):
|
21 |
+
# Split the input string into a list of URLs
|
22 |
+
url_list = [url.strip() for url in urls.split('\n') if url.strip()]
|
23 |
+
|
24 |
+
if not url_list:
|
25 |
+
return None, "No valid URLs provided. Please enter at least one YouTube URL."
|
26 |
+
|
27 |
+
downloaded_files = download_youtube_videos(url_list)
|
28 |
+
|
29 |
+
if downloaded_files:
|
30 |
+
# Create a list of (filename, file path) tuples for Gradio File component
|
31 |
+
file_tuples = [(f, f) for f in downloaded_files if os.path.exists(f)]
|
32 |
+
message = f"Successfully downloaded {len(file_tuples)} videos."
|
33 |
+
return file_tuples, message
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
+
return None, "Download failed. Please check the URLs and try again."
|
36 |
|
37 |
with gr.Blocks() as interface:
|
38 |
+
gr.Markdown("## YouTube Batch Video Downloader")
|
39 |
+
urls_input = gr.Textbox(label="Enter YouTube links (one per line) 🔗", lines=5)
|
40 |
+
download_button = gr.Button("Download Videos")
|
41 |
+
output = gr.File(label="Downloaded Videos", file_count="multiple")
|
42 |
message = gr.Markdown()
|
43 |
|
44 |
download_button.click(
|
45 |
fn=app,
|
46 |
+
inputs=urls_input,
|
47 |
outputs=[output, message],
|
48 |
api_name="download"
|
49 |
)
|