Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ from pydub import AudioSegment
|
|
5 |
from redis import Redis
|
6 |
import re
|
7 |
import requests
|
8 |
-
import io
|
9 |
|
10 |
|
11 |
redis = Redis(
|
@@ -16,9 +15,7 @@ redis = Redis(
|
|
16 |
db=0
|
17 |
)
|
18 |
|
19 |
-
|
20 |
-
worker_base_url = "https://server.jessejesse.workers.dev"
|
21 |
-
|
22 |
|
23 |
os.makedirs("downloads", exist_ok=True)
|
24 |
|
@@ -30,27 +27,28 @@ def save_ringtone_to_worker(song_name, file_path):
|
|
30 |
"""Upload the ringtone to Cloudflare Worker storage (R2 or KV) and save its URL in Redis."""
|
31 |
try:
|
32 |
sanitized_song_name = sanitize_filename(song_name)
|
33 |
-
|
34 |
-
|
35 |
with open(file_path, 'rb') as file:
|
36 |
file_data = file.read()
|
37 |
-
|
38 |
-
|
39 |
response = requests.put(
|
40 |
f"{worker_base_url}/{sanitized_song_name}",
|
41 |
data=file_data,
|
42 |
-
headers={"Content-Type": "audio/mpeg"}
|
43 |
)
|
44 |
-
|
45 |
if response.status_code == 200:
|
46 |
file_url = f"{worker_base_url}/{sanitized_song_name}"
|
47 |
redis.set(sanitized_song_name, file_url) # Save the URL to Redis
|
48 |
print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
|
|
|
49 |
else:
|
50 |
print(f"Failed to upload file. Status code: {response.status_code}")
|
51 |
-
|
|
|
52 |
except Exception as e:
|
53 |
print(f"Error uploading to Worker storage: {e}")
|
|
|
54 |
|
55 |
def process_youtube_url(url, uploaded_file):
|
56 |
try:
|
@@ -61,17 +59,17 @@ def process_youtube_url(url, uploaded_file):
|
|
61 |
ydl_opts = {
|
62 |
'format': 'bestaudio/best',
|
63 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
64 |
-
'cookiefile': 'cookies.txt' #cookies
|
65 |
}
|
66 |
|
67 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
68 |
info = ydl.extract_info(url, download=True)
|
69 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
70 |
-
song_name = info['title']
|
71 |
|
72 |
elif uploaded_file:
|
73 |
filename = uploaded_file.name
|
74 |
-
song_name = os.path.splitext(uploaded_file.name)[0]
|
75 |
|
76 |
if not filename or not os.path.exists(filename):
|
77 |
return None, None # No file, no output
|
@@ -81,18 +79,18 @@ def process_youtube_url(url, uploaded_file):
|
|
81 |
if not os.path.exists(mp3_filename):
|
82 |
audio = AudioSegment.from_file(filename)
|
83 |
audio.export(mp3_filename, format="mp3")
|
84 |
-
|
85 |
-
|
86 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
87 |
if not os.path.exists(ringtone_filename_m4r):
|
88 |
-
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] #20 seconds
|
89 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
90 |
|
91 |
-
|
92 |
-
save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
93 |
-
save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
94 |
|
95 |
-
return
|
96 |
|
97 |
except Exception as e:
|
98 |
print(f"Error: {e}")
|
@@ -104,7 +102,7 @@ with gr.Blocks() as interface:
|
|
104 |
<h1>Python YouTube Ringtones</h1>
|
105 |
<p>Insert a URL to create ringtones or Upload an MP3 to convert.</p>
|
106 |
""")
|
107 |
-
|
108 |
with gr.Row():
|
109 |
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
110 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
|
|
5 |
from redis import Redis
|
6 |
import re
|
7 |
import requests
|
|
|
8 |
|
9 |
|
10 |
redis = Redis(
|
|
|
15 |
db=0
|
16 |
)
|
17 |
|
18 |
+
worker_base_url = "https://server.jessejesse.workers.dev"
|
|
|
|
|
19 |
|
20 |
os.makedirs("downloads", exist_ok=True)
|
21 |
|
|
|
27 |
"""Upload the ringtone to Cloudflare Worker storage (R2 or KV) and save its URL in Redis."""
|
28 |
try:
|
29 |
sanitized_song_name = sanitize_filename(song_name)
|
30 |
+
|
|
|
31 |
with open(file_path, 'rb') as file:
|
32 |
file_data = file.read()
|
33 |
+
|
|
|
34 |
response = requests.put(
|
35 |
f"{worker_base_url}/{sanitized_song_name}",
|
36 |
data=file_data,
|
37 |
+
headers={"Content-Type": "audio/mpeg"}
|
38 |
)
|
39 |
+
|
40 |
if response.status_code == 200:
|
41 |
file_url = f"{worker_base_url}/{sanitized_song_name}"
|
42 |
redis.set(sanitized_song_name, file_url) # Save the URL to Redis
|
43 |
print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
|
44 |
+
return file_url # Return the URL for later use
|
45 |
else:
|
46 |
print(f"Failed to upload file. Status code: {response.status_code}")
|
47 |
+
return None
|
48 |
+
|
49 |
except Exception as e:
|
50 |
print(f"Error uploading to Worker storage: {e}")
|
51 |
+
return None
|
52 |
|
53 |
def process_youtube_url(url, uploaded_file):
|
54 |
try:
|
|
|
59 |
ydl_opts = {
|
60 |
'format': 'bestaudio/best',
|
61 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
62 |
+
'cookiefile': 'cookies.txt' #cookies
|
63 |
}
|
64 |
|
65 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
66 |
info = ydl.extract_info(url, download=True)
|
67 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
68 |
+
song_name = info['title']
|
69 |
|
70 |
elif uploaded_file:
|
71 |
filename = uploaded_file.name
|
72 |
+
song_name = os.path.splitext(uploaded_file.name)[0]
|
73 |
|
74 |
if not filename or not os.path.exists(filename):
|
75 |
return None, None # No file, no output
|
|
|
79 |
if not os.path.exists(mp3_filename):
|
80 |
audio = AudioSegment.from_file(filename)
|
81 |
audio.export(mp3_filename, format="mp3")
|
82 |
+
|
83 |
+
# Convert to M4R (iPhone ringtone format)
|
84 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
85 |
if not os.path.exists(ringtone_filename_m4r):
|
86 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
87 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
88 |
|
89 |
+
# Upload both files to Worker and get URLs
|
90 |
+
mp3_url = save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
91 |
+
m4r_url = save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
92 |
|
93 |
+
return mp3_url, m4r_url
|
94 |
|
95 |
except Exception as e:
|
96 |
print(f"Error: {e}")
|
|
|
102 |
<h1>Python YouTube Ringtones</h1>
|
103 |
<p>Insert a URL to create ringtones or Upload an MP3 to convert.</p>
|
104 |
""")
|
105 |
+
|
106 |
with gr.Row():
|
107 |
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
108 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|