Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ import yt_dlp
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
|
|
|
|
|
|
6 |
def process_youtube_url(url, uploaded_file):
|
7 |
try:
|
8 |
filename = None
|
@@ -11,39 +14,36 @@ def process_youtube_url(url, uploaded_file):
|
|
11 |
ydl_opts = {
|
12 |
'format': 'bestaudio/best',
|
13 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
14 |
-
'cookiefile': 'cookies.txt' #
|
15 |
}
|
16 |
|
17 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
18 |
info = ydl.extract_info(url, download=True)
|
19 |
-
|
20 |
-
filename = os.path.join('downloads', f"{info['id']}.webm")
|
21 |
|
22 |
elif uploaded_file:
|
23 |
-
filename = uploaded_file
|
24 |
-
|
25 |
-
else:
|
26 |
-
return None, None
|
27 |
|
28 |
-
if not os.path.exists(filename):
|
29 |
-
return None, None
|
30 |
|
31 |
-
|
32 |
-
mp3_filename = "Ringtone.mp3"
|
33 |
audio = AudioSegment.from_file(filename)
|
34 |
audio.export(mp3_filename, format="mp3")
|
35 |
|
36 |
-
|
37 |
-
ringtone_filename_m4r = "Apple.m4r"
|
38 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000]
|
39 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
40 |
|
41 |
-
return mp3_filename, ringtone_filename_m4r
|
42 |
-
|
43 |
except Exception as e:
|
44 |
print(f"Error: {e}")
|
45 |
return None, None
|
46 |
|
|
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
49 |
<h1>Python YouTube Ringtones</h1>
|
@@ -55,8 +55,8 @@ with gr.Blocks() as interface:
|
|
55 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
56 |
|
57 |
with gr.Row():
|
58 |
-
mp3_download = gr.File(label="Android Ringtone")
|
59 |
-
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
60 |
|
61 |
process_button = gr.Button("Create Ringtones")
|
62 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
@@ -75,3 +75,4 @@ interface.launch()
|
|
75 |
|
76 |
|
77 |
|
|
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
+
# Ensure downloads directory exists
|
7 |
+
os.makedirs("downloads", exist_ok=True)
|
8 |
+
|
9 |
def process_youtube_url(url, uploaded_file):
|
10 |
try:
|
11 |
filename = None
|
|
|
14 |
ydl_opts = {
|
15 |
'format': 'bestaudio/best',
|
16 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
17 |
+
'cookiefile': 'cookies.txt' # YTcookies
|
18 |
}
|
19 |
|
20 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
21 |
info = ydl.extract_info(url, download=True)
|
22 |
+
filename = os.path.join('downloads', f"{info['id']}.webm")
|
|
|
23 |
|
24 |
elif uploaded_file:
|
25 |
+
filename = uploaded_file.name
|
|
|
|
|
|
|
26 |
|
27 |
+
if not filename or not os.path.exists(filename):
|
28 |
+
return None, None # No file, no output
|
29 |
|
30 |
+
|
31 |
+
mp3_filename = "downloads/Ringtone.mp3"
|
32 |
audio = AudioSegment.from_file(filename)
|
33 |
audio.export(mp3_filename, format="mp3")
|
34 |
|
35 |
+
|
36 |
+
ringtone_filename_m4r = "downloads/Apple.m4r"
|
37 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000]
|
38 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
39 |
|
40 |
+
return mp3_filename, ringtone_filename_m4r
|
41 |
+
|
42 |
except Exception as e:
|
43 |
print(f"Error: {e}")
|
44 |
return None, None
|
45 |
|
46 |
+
# Gradio UI
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
49 |
<h1>Python YouTube Ringtones</h1>
|
|
|
55 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
56 |
|
57 |
with gr.Row():
|
58 |
+
mp3_download = gr.File(label="Download Android Ringtone")
|
59 |
+
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
|
60 |
|
61 |
process_button = gr.Button("Create Ringtones")
|
62 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
|
|
75 |
|
76 |
|
77 |
|
78 |
+
|