Spaces:
Runtime error
Runtime error
Commit
·
4074cdf
1
Parent(s):
b40326f
feat: app
Browse files- app.py +40 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
+
import hashlib
|
4 |
+
import gradio as gr
|
5 |
+
import yt_dlp
|
6 |
+
|
7 |
+
|
8 |
+
def download(url: str) -> str:
|
9 |
+
if not url:
|
10 |
+
raise gr.Error("Please input a YouTube URL")
|
11 |
+
|
12 |
+
hash = hashlib.md5(url.encode()).hexdigest()
|
13 |
+
tmp_file = os.path.join(tempfile.gettempdir(), f"{hash}")
|
14 |
+
|
15 |
+
ydl_opts = {
|
16 |
+
"format": "bestaudio/best",
|
17 |
+
"outtmpl": tmp_file,
|
18 |
+
"postprocessors": [
|
19 |
+
{
|
20 |
+
"key": "FFmpegExtractAudio",
|
21 |
+
"preferredcodec": "mp3",
|
22 |
+
"preferredquality": "192",
|
23 |
+
}
|
24 |
+
],
|
25 |
+
}
|
26 |
+
|
27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
28 |
+
ydl.download([url])
|
29 |
+
|
30 |
+
return tmp_file + ".mp3"
|
31 |
+
|
32 |
+
|
33 |
+
with gr.Blocks() as app:
|
34 |
+
url = gr.Textbox(lines=1, label="Enter URL")
|
35 |
+
btn = gr.Button("Download", variant="primary")
|
36 |
+
audio = gr.Audio(type="filepath", label="Downloaded Audio", format="mp3")
|
37 |
+
|
38 |
+
btn.click(fn=download, inputs=[url], outputs=[audio])
|
39 |
+
|
40 |
+
app.launch(show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
yt_dlp
|