Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import yt_dlp
|
3 |
-
import subprocess
|
4 |
import os
|
5 |
from pydub import AudioSegment
|
6 |
|
@@ -16,7 +15,7 @@ def process_youtube_url(url, uploaded_file, state):
|
|
16 |
ydl_opts = {
|
17 |
'format': 'bestaudio/best',
|
18 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
19 |
-
'cookiefile': 'cookies.txt' #YT
|
20 |
}
|
21 |
|
22 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
@@ -37,7 +36,6 @@ def process_youtube_url(url, uploaded_file, state):
|
|
37 |
|
38 |
log.append("Converting audio to MP3 manually...")
|
39 |
|
40 |
-
|
41 |
mp3_filename = 'Ringtone.mp3'
|
42 |
audio = AudioSegment.from_file(filename)
|
43 |
audio.export(mp3_filename, format="mp3")
|
@@ -45,41 +43,38 @@ def process_youtube_url(url, uploaded_file, state):
|
|
45 |
|
46 |
log.append("Creating ringtone...")
|
47 |
|
48 |
-
|
49 |
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
50 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
51 |
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
52 |
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
|
53 |
|
54 |
-
|
55 |
ringtone_filename_m4r = 'Apple.m4r'
|
56 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
57 |
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
|
58 |
|
59 |
-
return
|
60 |
|
61 |
except Exception as e:
|
62 |
log.append(f"Error: {str(e)}")
|
63 |
-
return
|
64 |
|
65 |
with gr.Blocks() as interface:
|
66 |
gr.HTML("""
|
67 |
<h1>Python YouTube Ringtones</h1>
|
68 |
-
<p>
|
69 |
""")
|
70 |
|
71 |
with gr.Row():
|
72 |
-
youtube_url = gr.Textbox(label="Enter YouTube URL
|
73 |
mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
|
74 |
state = gr.State()
|
75 |
|
76 |
with gr.Row():
|
77 |
-
status_log = gr.Textbox(label="Status")
|
78 |
mp3_download = gr.File(label="Android Ringtone")
|
79 |
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
80 |
|
81 |
process_button = gr.Button("Create Ringtones")
|
82 |
-
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload, state], outputs=[
|
83 |
|
84 |
interface.launch()
|
85 |
|
@@ -91,3 +86,4 @@ interface.launch()
|
|
91 |
|
92 |
|
93 |
|
|
|
|
1 |
import gradio as gr
|
2 |
import yt_dlp
|
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
|
|
15 |
ydl_opts = {
|
16 |
'format': 'bestaudio/best',
|
17 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
18 |
+
'cookiefile': 'cookies.txt' # YT
|
19 |
}
|
20 |
|
21 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
36 |
|
37 |
log.append("Converting audio to MP3 manually...")
|
38 |
|
|
|
39 |
mp3_filename = 'Ringtone.mp3'
|
40 |
audio = AudioSegment.from_file(filename)
|
41 |
audio.export(mp3_filename, format="mp3")
|
|
|
43 |
|
44 |
log.append("Creating ringtone...")
|
45 |
|
|
|
46 |
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
47 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
48 |
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
49 |
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
|
50 |
|
|
|
51 |
ringtone_filename_m4r = 'Apple.m4r'
|
52 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
53 |
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
|
54 |
|
55 |
+
return mp3_filename, ringtone_filename_m4r, state
|
56 |
|
57 |
except Exception as e:
|
58 |
log.append(f"Error: {str(e)}")
|
59 |
+
return None, None, state
|
60 |
|
61 |
with gr.Blocks() as interface:
|
62 |
gr.HTML("""
|
63 |
<h1>Python YouTube Ringtones</h1>
|
64 |
+
<p>Insert a URL to make ringtones or upload an MP3 to convert</p>
|
65 |
""")
|
66 |
|
67 |
with gr.Row():
|
68 |
+
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
69 |
mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
|
70 |
state = gr.State()
|
71 |
|
72 |
with gr.Row():
|
|
|
73 |
mp3_download = gr.File(label="Android Ringtone")
|
74 |
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
75 |
|
76 |
process_button = gr.Button("Create Ringtones")
|
77 |
+
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload, state], outputs=[mp3_download, iphone_ringtone])
|
78 |
|
79 |
interface.launch()
|
80 |
|
|
|
86 |
|
87 |
|
88 |
|
89 |
+
|