Next commited on
Commit
933dd53
·
verified ·
1 Parent(s): e7e2b0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -1,22 +1,34 @@
1
- import webui
2
  import yt_dlp
3
 
4
- class YoutubeDownloader(webui.Block):
5
- def __init__(self):
6
- super().__init__()
7
- self.url_input = webui.Textbox(label="YouTube URL")
8
- self.download_button = webui.Button(label="Download")
9
- self.result_text = webui.Textarea(rows=10)
 
 
 
10
 
11
- def on_button_click(self, event):
12
- url = self.url_input.get()
13
- ydl = yt_dlp.YoutubeDLP()
14
- try:
15
- results = ydl.extract_info(url, download=True)
16
- self.result_text.set("Download information:\n" + str(results))
17
- except Exception as e:
18
- self.result_text.set("Error: " + str(e))
19
 
 
 
20
 
21
- if __name__ == "__main__":
22
- webui.launch(YoutubeDownloader(), port=8000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import yt_dlp
3
 
4
+ def download_media(url, media_type):
5
+ ydl_opts = {
6
+ 'format': 'bestaudio/best' if media_type == 'Audio' else 'bestvideo+bestaudio/best',
7
+ 'outtmpl': '%(title)s.%(ext)s',
8
+ }
9
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
10
+ result = ydl.extract_info(url, download=True)
11
+ filename = ydl.prepare_filename(result)
12
+ return filename
13
 
14
+ def download_audio(url):
15
+ return download_media(url, 'Audio')
 
 
 
 
 
 
16
 
17
+ def download_video(url):
18
+ return download_media(url, 'Video')
19
 
20
+ with gr.Blocks() as demo:
21
+ gr.Markdown("# YouTube Audio/Video Downloader")
22
+
23
+ url_input = gr.Textbox(label="YouTube URL")
24
+ media_type = gr.Radio(label="Select Media Type", choices=["Audio", "Video"])
25
+ output = gr.Textbox(label="Downloaded File")
26
+
27
+ download_button = gr.Button("Download")
28
+ download_button.click(
29
+ fn=lambda url, media: download_audio(url) if media == 'Audio' else download_video(url),
30
+ inputs=[url_input, media_type],
31
+ outputs=output
32
+ )
33
+
34
+ demo.launch()