PYTR / app.py
ilostmyipod's picture
Update app.py
9c804d2 verified
raw
history blame
3.46 kB
import gradio as gr
import yt_dlp
import subprocess
import os
from pydub import AudioSegment
def process_youtube_url(url, uploaded_file, state):
log = []
try:
if url:
log.append("Starting download...")
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'downloads/%(id)s.%(ext)s',
'cookiefile': 'cookies.txt' # Using YouTube cookies
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
log.append(f"Downloaded: {info['title']}")
filename = os.path.join('downloads', f"{info['id']}.webm")
elif uploaded_file:
filename = uploaded_file.name
log.append(f"Uploaded MP3 file: {filename}")
else:
raise ValueError("No YouTube URL or MP3 file provided.")
log.append("Converting audio to MP3 and creating ringtone...")
mp3_filename = 'Ringtone.mp3'
ffmpeg_command = [
'ffmpeg', '-i', filename, '-vn', '-acodec', 'libmp3lame', '-b:a', '192k', mp3_filename
]
subprocess.run(ffmpeg_command, check=True))
log.append(f"MP3 saved: {mp3_filename}")
ringtone_filename_m4a = 'temp_ringtone.m4a'
ffmpeg_ringtone_command = [
'ffmpeg', '-i', mp3_filename, '-t', '00:00:20', ringtone_filename_m4a
]
subprocess.run(ffmpeg_ringtone_command, check=True)
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
ringtone_filename_m4r = 'Apple.m4r'
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
return '\n'.join(log), mp3_filename, ringtone_filename_m4r, state
except Exception as e:
log.append(f"Error: {str(e)}")
return '\n'.join(log), None, None, state
def push_to_android(mp3_filename):
log = []
try:
adb_push_command = f'adb push {mp3_filename} /sdcard/Ringtones/'
subprocess.run(adb_push_command, shell=True, check=True)
log.append(f"Successfully pushed {mp3_filename} to Android device.")
except Exception as e:
log.append(f"Error pushing to Android device: {str(e)}")
return '\n'.join(log)
with gr.Blocks() as interface:
gr.HTML("<h1>Python Ringtones</h1><br/>Download and convert YouTube audio to MP3 (Ringtone.mp3) and create an iPhone-compatible M4R ringtone (Apple.m4r). Upload an MP3 file to convert it too.")
with gr.Row():
youtube_url = gr.Textbox(label="Enter YouTube or Song URL to create a ringtone", placeholder="Paste the URL here...")
mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
state = gr.State()
with gr.Row():
status_log = gr.Textbox(label="Status Log")
mp3_download = gr.File(label="Download Android Ringtone")
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
def on_process_button_click(url, uploaded_file):
return process_youtube_url(url, uploaded_file, state)
process_button = gr.Button("Create Ringtone")
process_button.click(on_process_button_click, inputs=[youtube_url, mp3_upload], outputs=[status_log, mp3_download, iphone_ringtone])
push_button = gr.Button("Push to Android")
push_button.click(push_to_android, inputs=mp3_download, outputs=status_log)
interface.launch()