Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,43 +2,98 @@ import gradio as gr
|
|
2 |
import yt_dlp
|
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
|
|
|
12 |
|
13 |
if url:
|
14 |
ydl_opts = {
|
15 |
'format': 'bestaudio/best',
|
16 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
17 |
-
'cookiefile': 'cookies.txt' #
|
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/
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
return mp3_filename, ringtone_filename_m4r
|
41 |
-
|
42 |
except Exception as e:
|
43 |
print(f"Error: {e}")
|
44 |
return None, None
|
@@ -47,7 +102,7 @@ def process_youtube_url(url, uploaded_file):
|
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
49 |
<h1>Python YouTube Ringtones</h1>
|
50 |
-
<p>Insert a URL to
|
51 |
""")
|
52 |
|
53 |
with gr.Row():
|
@@ -61,7 +116,8 @@ with gr.Blocks() as interface:
|
|
61 |
process_button = gr.Button("Create Ringtones")
|
62 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
63 |
|
64 |
-
interface.launch()
|
|
|
65 |
|
66 |
|
67 |
|
|
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
+
from redis import Redis
|
6 |
+
import re
|
7 |
+
import requests
|
8 |
+
import io
|
9 |
+
|
10 |
+
# Set up Redis connection
|
11 |
+
redis = Redis(
|
12 |
+
host="amused-walleye-31373.upstash.io",
|
13 |
+
port=31373,
|
14 |
+
password="67212443600c45ffa962e19ea9202605",
|
15 |
+
ssl=True,
|
16 |
+
db=0
|
17 |
+
)
|
18 |
+
|
19 |
+
# Cloudflare Worker URL
|
20 |
+
worker_base_url = "https://server.jessejesse.workers.dev" # Your Cloudflare Worker URL
|
21 |
|
22 |
# Ensure downloads directory exists
|
23 |
os.makedirs("downloads", exist_ok=True)
|
24 |
|
25 |
+
def sanitize_filename(filename):
|
26 |
+
"""Sanitize the filename by removing or replacing special characters."""
|
27 |
+
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
28 |
+
|
29 |
+
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 |
+
# Read file contents
|
35 |
+
with open(file_path, 'rb') as file:
|
36 |
+
file_data = file.read()
|
37 |
+
|
38 |
+
# Upload file to the worker storage (e.g., R2 or KV)
|
39 |
+
response = requests.put(
|
40 |
+
f"{worker_base_url}/{sanitized_song_name}",
|
41 |
+
data=file_data,
|
42 |
+
headers={"Content-Type": "audio/mpeg"} # For MP3, adjust for other formats
|
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:
|
57 |
filename = None
|
58 |
+
song_name = None
|
59 |
|
60 |
if url:
|
61 |
ydl_opts = {
|
62 |
'format': 'bestaudio/best',
|
63 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
64 |
+
'cookiefile': 'cookies.txt' # YT cookies (if needed)
|
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'] # Use YouTube video title as the song name
|
71 |
|
72 |
elif uploaded_file:
|
73 |
+
filename = uploaded_file.name
|
74 |
+
song_name = os.path.splitext(uploaded_file.name)[0] # Use file name as song name
|
75 |
|
76 |
if not filename or not os.path.exists(filename):
|
77 |
return None, None # No file, no output
|
78 |
|
79 |
+
# Convert to MP3
|
80 |
+
mp3_filename = f"downloads/{song_name}.mp3"
|
81 |
+
if not os.path.exists(mp3_filename):
|
82 |
+
audio = AudioSegment.from_file(filename)
|
83 |
+
audio.export(mp3_filename, format="mp3")
|
84 |
|
85 |
+
# Create iPhone ringtone (Apple M4R format)
|
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] # Clip to 20 seconds for ringtone
|
89 |
+
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
90 |
+
|
91 |
+
# Save ringtones to Worker and Redis
|
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 mp3_filename, ringtone_filename_m4r # Returning the file paths for download
|
96 |
|
|
|
|
|
97 |
except Exception as e:
|
98 |
print(f"Error: {e}")
|
99 |
return None, None
|
|
|
102 |
with gr.Blocks() as interface:
|
103 |
gr.HTML("""
|
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():
|
|
|
116 |
process_button = gr.Button("Create Ringtones")
|
117 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
118 |
|
119 |
+
interface.launch(share=True)
|
120 |
+
|
121 |
|
122 |
|
123 |
|