Spaces:
Running
Running
import gradio as gr | |
import subprocess | |
import os | |
import logging | |
import pyperclip | |
logging.basicConfig(level=logging.INFO) | |
DOWNLOAD_FOLDER = './output' | |
COOKIES_PATH = './cookies.txt' | |
# Ensure the download folder exists | |
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True) | |
def create_readme(): | |
readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt') | |
try: | |
with open(readme_path, 'w') as readme_file: | |
readme_file.write( | |
"Android: copy the .mp3 to the ringtones folder and reboot device.\n" | |
"iPhone: drag and drop the .m4r file in iTunes and sync device." | |
) | |
return f"readme.txt created at: {readme_path}" | |
except Exception as e: | |
return f"Failed to create readme.txt: {str(e)}" | |
def get_video_filename(): | |
for file in os.listdir(DOWNLOAD_FOLDER): | |
if file.endswith(".mp4"): | |
return os.path.join(DOWNLOAD_FOLDER, file) | |
return None | |
def download_video(url): | |
try: | |
output_template = os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s') | |
command = ['yt-dlp', '-f', 'mp4', '-o', output_template, url] | |
if os.path.exists(COOKIES_PATH): | |
command += ['--cookies', COOKIES_PATH] | |
else: | |
return f"Error: Cookies file {COOKIES_PATH} not found." | |
logging.info(f"Running command: {' '.join(command)}") | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
logging.info(stdout.decode()) | |
if process.returncode != 0: | |
return f"Error downloading video: {stderr.decode()}" | |
video_filename = get_video_filename() | |
if not video_filename: | |
return "Error: Downloaded video file not found." | |
return f"Video downloaded: {video_filename}\n" + create_readme() | |
except Exception as e: | |
return f"Exception during video download: {str(e)}" | |
def android_download(): | |
try: | |
video_filename = get_video_filename() | |
if not video_filename: | |
return "Error: Video file not found for Android ringtone.", None | |
android_path = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3') | |
command = ['ffmpeg', '-y', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', android_path] | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
_, stderr = process.communicate() | |
if process.returncode == 0 and os.path.exists(android_path): | |
return "Android ringtone created successfully.", android_path | |
else: | |
return f"Error creating Android ringtone: {stderr.decode()}", None | |
except Exception as e: | |
return f"Exception during Android ringtone creation: {str(e)}", None | |
def iphone_download(): | |
try: | |
android_path = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3') | |
iphone_path = os.path.join(DOWNLOAD_FOLDER, 'iPhoneRingtone.m4r') | |
if not os.path.exists(android_path): | |
return "Error: Android ringtone not found. Create it first.", None | |
command = [ | |
'ffmpeg', '-y', '-i', android_path, '-t', '20', | |
'-acodec', 'aac', '-b:a', '128k', '-f', 'ipod', iphone_path | |
] | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
_, stderr = process.communicate() | |
if process.returncode == 0 and os.path.exists(iphone_path): | |
return "iPhone ringtone created successfully.", iphone_path | |
else: | |
return f"Error creating iPhone ringtone: {stderr.decode()}", None | |
except Exception as e: | |
return f"Exception during iPhone ringtone creation: {str(e)}", None | |
def paste_from_clipboard(): | |
try: | |
return pyperclip.paste() | |
except Exception as e: | |
return f"Clipboard error: {str(e)}" | |
def clear_input(): | |
return "", "" | |
# Gradio UI | |
with gr.Blocks() as iface: | |
gr.Markdown("## π΅ YouTube Ringtone Creator") | |
url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here") | |
status_output = gr.Textbox(label="Status", interactive=False) | |
android_download_link = gr.File(label="Android Ringtone", interactive=False) | |
iphone_download_link = gr.File(label="iPhone Ringtone", interactive=False) | |
download_button = gr.Button("π₯ Download Video") | |
android_button = gr.Button("πΆ Create Android Ringtone") | |
iphone_button = gr.Button("π± Create iPhone Ringtone") | |
paste_button = gr.Button("π Paste URL from Clipboard") | |
clear_button = gr.Button("π§Ή Clear") | |
download_button.click(fn=download_video, inputs=url_input, outputs=status_output) | |
android_button.click(fn=android_download, outputs=[status_output, android_download_link]) | |
iphone_button.click(fn=iphone_download, outputs=[status_output, iphone_download_link]) | |
paste_button.click(fn=paste_from_clipboard, outputs=url_input) | |
clear_button.click(fn=clear_input, outputs=[url_input, status_output]) | |
iface.launch(share=True) | |