Spaces:
Sleeping
Sleeping
Next
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
import yt_dlp
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 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 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|