Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import os
|
4 |
import re
|
5 |
|
@@ -13,29 +13,41 @@ def download_audio(url, output_dir="downloads"):
|
|
13 |
if not os.path.exists(output_dir):
|
14 |
os.makedirs(output_dir)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
output_file = audio_stream.download(
|
27 |
-
output_path=output_dir,
|
28 |
-
filename=f"{safe_title}.mp3"
|
29 |
-
)
|
30 |
-
|
31 |
-
return {
|
32 |
-
"status": "success",
|
33 |
-
"message": f"Successfully downloaded: {safe_title}",
|
34 |
-
"title": yt.title,
|
35 |
-
"author": yt.author,
|
36 |
-
"duration": f"{yt.length // 60}:{yt.length % 60:02d}",
|
37 |
-
"file_path": output_file
|
38 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
except Exception as e:
|
41 |
return {
|
|
|
1 |
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
import os
|
4 |
import re
|
5 |
|
|
|
13 |
if not os.path.exists(output_dir):
|
14 |
os.makedirs(output_dir)
|
15 |
|
16 |
+
# Configure yt-dlp options
|
17 |
+
ydl_opts = {
|
18 |
+
'format': 'bestaudio/best',
|
19 |
+
'postprocessors': [{
|
20 |
+
'key': 'FFmpegExtractAudio',
|
21 |
+
'preferredcodec': 'mp3',
|
22 |
+
'preferredquality': '192',
|
23 |
+
}],
|
24 |
+
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
|
25 |
+
'verbose': True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
}
|
27 |
+
|
28 |
+
# Download with yt-dlp
|
29 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
30 |
+
# Get video info first
|
31 |
+
info = ydl.extract_info(url, download=False)
|
32 |
+
title = info.get('title', 'video')
|
33 |
+
duration = info.get('duration', 0)
|
34 |
+
author = info.get('uploader', 'Unknown')
|
35 |
+
|
36 |
+
# Download the video
|
37 |
+
ydl.download([url])
|
38 |
+
|
39 |
+
# Construct the output filepath
|
40 |
+
safe_title = sanitize_filename(title)
|
41 |
+
output_file = os.path.join(output_dir, f"{safe_title}.mp3")
|
42 |
+
|
43 |
+
return {
|
44 |
+
"status": "success",
|
45 |
+
"message": f"Successfully downloaded: {safe_title}",
|
46 |
+
"title": title,
|
47 |
+
"author": author,
|
48 |
+
"duration": f"{duration // 60}:{duration % 60:02d}",
|
49 |
+
"file_path": output_file
|
50 |
+
}
|
51 |
|
52 |
except Exception as e:
|
53 |
return {
|