Hev832 commited on
Commit
cd512fa
·
verified ·
1 Parent(s): 2cb5213

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +34 -45
run.py CHANGED
@@ -2,54 +2,43 @@ import gradio as gr
2
  import yt_dlp
3
  import os
4
 
5
- def download_youtube(url, file_format):
6
- ydl_opts = {}
7
- try:
8
- if file_format == "audio":
9
- ydl_opts = {
10
- 'format': 'bestaudio/best',
11
- 'postprocessors': [{
12
- 'key': 'FFmpegExtractAudio',
13
- 'preferredcodec': 'mp3',
14
- 'preferredquality': '192',
15
- }],
16
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
17
- }
18
- else:
19
- ydl_opts = {
20
- 'format': 'bestvideo+bestaudio/best',
21
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
22
- }
 
23
 
24
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
- info_dict = ydl.extract_info(url, download=True)
26
- file_path = ydl.prepare_filename(info_dict)
27
- if file_format == "audio":
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 None, gr.Audio.update(visible=False), gr.Video.update(value=file_path, visible=True)
41
 
42
  with gr.Blocks() as demo:
43
- with gr.Row():
44
- with gr.Column():
45
- url_input = gr.Textbox(label="YouTube URL")
46
- format_input = gr.Dropdown(choices=["audio", "video"], label="Format")
47
- download_button = gr.Button("Download")
48
- with gr.Column():
49
- error_output = gr.Textbox(label="Error Output", visible=True)
50
- audio_output = gr.Audio(label="Audio Output", visible=False)
51
- video_output = gr.Video(label="Video Output", visible=False)
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()