Spaces:
Sleeping
Sleeping
Update run.py
Browse files
run.py
CHANGED
@@ -2,54 +2,43 @@ import gradio as gr
|
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
|
5 |
-
def
|
6 |
-
ydl_opts = {
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
file_path = os.path.splitext(file_path)[0] + ".mp3"
|
29 |
-
|
30 |
-
return file_path, None
|
31 |
-
except Exception as e:
|
32 |
-
return None, str(e)
|
33 |
-
|
34 |
-
def show_output(file_path, error):
|
35 |
-
if error:
|
36 |
-
return error, gr.Audio.update(visible=False), gr.Video.update(visible=False)
|
37 |
-
if file_path.endswith(".mp3"):
|
38 |
-
return None, gr.Audio.update(value=file_path, visible=True), gr.Video.update(visible=False)
|
39 |
else:
|
40 |
-
return
|
41 |
|
42 |
with gr.Blocks() as demo:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
download_button.click(download_youtube, inputs=[url_input, format_input], outputs=[error_output, audio_output, video_output])
|
54 |
|
55 |
demo.launch()
|
|
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
|
5 |
+
def download_media(url, media_type):
|
6 |
+
ydl_opts = {
|
7 |
+
'format': 'bestaudio/best' if media_type == 'audio' else 'bestvideo+bestaudio',
|
8 |
+
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
9 |
+
'postprocessors': [{
|
10 |
+
'key': 'FFmpegExtractAudio',
|
11 |
+
'preferredcodec': 'mp3',
|
12 |
+
'preferredquality': '192',
|
13 |
+
}] if media_type == 'audio' else []
|
14 |
+
}
|
15 |
+
|
16 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
17 |
+
info = ydl.extract_info(url, download=True)
|
18 |
+
file_path = ydl.prepare_filename(info)
|
19 |
+
|
20 |
+
if media_type == 'audio':
|
21 |
+
file_path = file_path.rsplit('.', 1)[0] + '.mp3'
|
22 |
+
|
23 |
+
return file_path
|
24 |
|
25 |
+
def show_media(url, media_type):
|
26 |
+
file_path = download_media(url, media_type)
|
27 |
+
if media_type == 'audio':
|
28 |
+
return None, file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
else:
|
30 |
+
return file_path, None
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("# YouTube Downloader")
|
34 |
+
|
35 |
+
url = gr.Textbox(label="YouTube URL")
|
36 |
+
media_type = gr.Radio(label="Media Type", choices=["audio", "video"])
|
37 |
+
download_button = gr.Button("Download")
|
38 |
+
|
39 |
+
video_output = gr.Video()
|
40 |
+
audio_output = gr.Audio()
|
41 |
+
|
42 |
+
download_button.click(show_media, inputs=[url, media_type], outputs=[video_output, audio_output])
|
|
|
43 |
|
44 |
demo.launch()
|