Spaces:
Sleeping
Sleeping
File size: 3,786 Bytes
ab28a19 1ac0332 7d50d22 1ac0332 3df1079 7d50d22 fc41fb4 1ac0332 3df1079 1ac0332 3df1079 1ac0332 3df1079 1ac0332 3df1079 1ac0332 bfbd8e1 1ac0332 bfbd8e1 1ac0332 3df1079 1ac0332 3df1079 1ac0332 136c2ab ab28a19 118a5f9 1ac0332 136c2ab ab28a19 3df1079 ab28a19 fc41fb4 3df1079 ab28a19 1ac0332 3df1079 ab28a19 fc41fb4 bfbd8e1 118a5f9 bfbd8e1 1ac0332 3df1079 bfbd8e1 1ac0332 3df1079 1ac0332 bfbd8e1 3df1079 1ac0332 3df1079 ab28a19 136c2ab ab28a19 fc41fb4 ab28a19 16e732a 3c7f2ec 1ac0332 16e732a 3df1079 ab28a19 136c2ab ab28a19 fc41fb4 ab28a19 118a5f9 136c2ab ab28a19 1ac0332 ab28a19 fcdb38d 16e732a ff5d792 271a312 e3aff9f 136c2ab fc41fb4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import gradio as gr
import yt_dlp
import os
from pydub import AudioSegment
from redis import Redis
import re
import requests
redis = Redis(
host="amused-walleye-31373.upstash.io",
port=31373,
password="67212443600c45ffa962e19ea9202605",
ssl=True,
db=0
)
worker_base_url = "https://server.jessejesse.workers.dev"
os.makedirs("downloads", exist_ok=True)
def sanitize_filename(filename):
"""Sanitize the filename by removing or replacing special characters."""
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
def save_ringtone_to_worker(song_name, file_path):
"""Upload the ringtone to Cloudflare Worker storage (R2 or KV) and save its URL in Redis."""
try:
sanitized_song_name = sanitize_filename(song_name)
with open(file_path, 'rb') as file:
file_data = file.read()
response = requests.put(
f"{worker_base_url}/{sanitized_song_name}",
data=file_data,
headers={"Content-Type": "audio/mpeg"}
)
if response.status_code == 200:
file_url = f"{worker_base_url}/{sanitized_song_name}"
redis.set(sanitized_song_name, file_url)
print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
return file_url
else:
print(f"Failed to upload file. Status code: {response.status_code}")
return None
except Exception as e:
print(f"Error uploading to Worker storage: {e}")
return None
def process_youtube_url(url, uploaded_file):
try:
filename = None
song_name = None
if url:
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'downloads/%(id)s.%(ext)s',
'cookiefile': 'cookies.txt' #cookies
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = os.path.join('downloads', f"{info['id']}.webm")
song_name = info['title']
elif uploaded_file:
filename = uploaded_file.name
song_name = os.path.splitext(uploaded_file.name)[0]
if not filename or not os.path.exists(filename):
return None, None
mp3_filename = f"downloads/{song_name}.mp3"
if not os.path.exists(mp3_filename):
audio = AudioSegment.from_file(filename)
audio.export(mp3_filename, format="mp3")
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
if not os.path.exists(ringtone_filename_m4r):
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
mp3_url = save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
m4r_url = save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
return mp3_url, m4r_url
except Exception as e:
print(f"Error: {e}")
return None, None
# Gradio UI
with gr.Blocks() as interface:
gr.HTML("""
<h1>Python YouTube Ringtones</h1>
<p>Insert a URL to create ringtones or Upload an MP3 to convert.</p>
""")
with gr.Row():
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
mp3_upload = gr.File(label="Upload MP3 (Optional)")
with gr.Row():
mp3_download = gr.File(label="Download Android Ringtone")
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
process_button = gr.Button("Create Ringtones")
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
interface.launch(share=True)
|