Spaces:
Sleeping
Sleeping
Update run.py
Browse files
run.py
CHANGED
@@ -1,33 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import os
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from yt_dlp import YoutubeDL
|
3 |
import os
|
4 |
|
5 |
+
def download_youtube(url, output_format):
|
6 |
+
ydl_opts = {}
|
7 |
+
file_path = ""
|
8 |
+
|
9 |
+
if output_format == "audio":
|
10 |
+
ydl_opts = {
|
11 |
+
'format': 'bestaudio/best',
|
12 |
+
'postprocessors': [{
|
13 |
+
'key': 'FFmpegExtractAudio',
|
14 |
+
'preferredcodec': 'mp3',
|
15 |
+
'preferredquality': '192',
|
16 |
+
}],
|
17 |
+
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
18 |
+
}
|
19 |
+
elif output_format == "video":
|
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 |
+
result = ydl.extract_info(url, download=True)
|
27 |
+
file_path = ydl.prepare_filename(result)
|
28 |
+
|
29 |
+
if output_format == "audio":
|
30 |
+
file_path = file_path.replace(result['ext'], 'mp3')
|
31 |
+
|
32 |
+
return file_path
|
33 |
+
|
34 |
+
def show_media(file_path, output_format):
|
35 |
+
if output_format == "audio":
|
36 |
+
return gr.Audio.update(value=file_path)
|
37 |
+
elif output_format == "video":
|
38 |
+
return gr.Video.update(value=file_path)
|
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.Markdown("## YouTube Downloader")
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
url_input = gr.Textbox(label="YouTube URL")
|
49 |
+
format_input = gr.Radio(choices=["audio", "video"], label="Format")
|
50 |
+
|
51 |
+
download_btn = gr.Button("Download")
|
52 |
+
|
53 |
+
audio_output = gr.Audio(label="Audio Output")
|
54 |
+
video_output = gr.Video(label="Video Output")
|
55 |
+
|
56 |
+
download_btn.click(yt_download, inputs=[url_input, format_input], outputs=[audio_output, video_output])
|
57 |
+
|
58 |
+
demo.launch()
|