Spaces:
Sleeping
Sleeping
Update run.py
Browse files
run.py
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import os
|
4 |
|
5 |
-
def download_youtube(url,
|
6 |
ydl_opts = {}
|
7 |
-
|
8 |
-
|
9 |
-
if output_format == "audio":
|
10 |
ydl_opts = {
|
11 |
'format': 'bestaudio/best',
|
12 |
'postprocessors': [{
|
@@ -16,40 +14,38 @@ def download_youtube(url, output_format):
|
|
16 |
}],
|
17 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
18 |
}
|
19 |
-
|
20 |
ydl_opts = {
|
21 |
'format': 'bestvideo+bestaudio/best',
|
22 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
23 |
}
|
24 |
|
25 |
-
with YoutubeDL(ydl_opts) as ydl:
|
26 |
-
|
27 |
-
file_path = ydl.prepare_filename(
|
28 |
-
|
29 |
-
|
30 |
-
file_path = file_path.replace(result['ext'], 'mp3')
|
31 |
|
32 |
return file_path
|
33 |
|
34 |
-
def
|
35 |
-
if
|
36 |
-
return gr.Audio
|
37 |
-
|
38 |
-
return gr.Video
|
39 |
-
|
40 |
-
def yt_download(url, output_format):
|
41 |
-
file_path = download_youtube(url, output_format)
|
42 |
-
return show_media(file_path, output_format)
|
43 |
|
44 |
with gr.Blocks() as demo:
|
45 |
-
gr.
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
audio_output = gr.Audio(label="Audio Output")
|
52 |
-
|
53 |
-
video_output = gr.Video(label="Video Output")
|
54 |
-
|
55 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
import os
|
4 |
|
5 |
+
def download_youtube(url, file_format):
|
6 |
ydl_opts = {}
|
7 |
+
if file_format == "audio":
|
|
|
|
|
8 |
ydl_opts = {
|
9 |
'format': 'bestaudio/best',
|
10 |
'postprocessors': [{
|
|
|
14 |
}],
|
15 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
16 |
}
|
17 |
+
else:
|
18 |
ydl_opts = {
|
19 |
'format': 'bestvideo+bestaudio/best',
|
20 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
21 |
}
|
22 |
|
23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
24 |
+
info_dict = ydl.extract_info(url, download=True)
|
25 |
+
file_path = ydl.prepare_filename(info_dict)
|
26 |
+
if file_format == "audio":
|
27 |
+
file_path = os.path.splitext(file_path)[0] + ".mp3"
|
|
|
28 |
|
29 |
return file_path
|
30 |
|
31 |
+
def show_output(file_path):
|
32 |
+
if file_path.endswith(".mp3"):
|
33 |
+
return gr.Audio(file_path, label="Audio Output"), None
|
34 |
+
else:
|
35 |
+
return None, gr.Video(file_path, label="Video Output")
|
|
|
|
|
|
|
|
|
36 |
|
37 |
with gr.Blocks() as demo:
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
url_input = gr.Textbox(label="YouTube URL")
|
41 |
+
format_input = gr.Dropdown(choices=["audio", "video"], label="Format")
|
42 |
+
download_button = gr.Button("Download")
|
43 |
+
with gr.Column():
|
44 |
+
audio_output = gr.Audio(label="Audio Output", visible=False)
|
45 |
+
video_output = gr.Video(label="Video Output", visible=False)
|
46 |
+
|
47 |
+
download_button.click(download_youtube, inputs=[url_input, format_input], outputs=None).then(
|
48 |
+
show_output, inputs=None, outputs=[audio_output, video_output]
|
49 |
+
)
|
50 |
|
|
|
|
|
|
|
|
|
51 |
demo.launch()
|