Hev832 commited on
Commit
91243d9
1 Parent(s): 80cfb0f

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +26 -30
run.py CHANGED
@@ -1,12 +1,10 @@
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': [{
@@ -16,40 +14,38 @@ def download_youtube(url, output_format):
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
- url_input = gr.Textbox(label="YouTube URL")
47
- format_input = gr.Radio(choices=["audio", "video"], label="Format")
48
- download_btn = gr.Button("Download")
49
- download_btn.click(yt_download, inputs=[url_input, format_input], outputs=[audio_output, video_output])
 
 
 
 
 
 
 
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()