Spaces:
Sleeping
Sleeping
import gradio as gr | |
import subprocess | |
import os | |
import logging | |
import pyperclip | |
logging.basicConfig(level=logging.INFO) | |
DOWNLOAD_FOLDER = os.path.expanduser("./Downloads") | |
def create_readme(): | |
"""Creates a readme.txt file with the specified text""" | |
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. 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 download_video(url): | |
try: | |
output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.mp4') | |
command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url] | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode == 0: | |
video_filename = get_video_filename(output_path) | |
if video_filename: | |
return f"Video downloaded: {video_filename}\n" + create_readme() | |
else: | |
return "Error: Video file not found." | |
else: | |
return f"Error downloading video: {stderr.decode()}" | |
except Exception as e: | |
return f"Failed to download video: {str(e)}" | |
def android_download(): | |
try: | |
video_filename = get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4') | |
if video_filename: | |
command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3'] | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode == 0: | |
return "Android ringtone created successfully" | |
else: | |
return f"Error creating Android ringtone: {stderr.decode()}" | |
else: | |
return "Error: Video file not found for Android ringtone." | |
except Exception as e: | |
return f"Failed to create Android ringtone: {str(e)}" | |
def iphone_download(): | |
try: | |
command = ['ffmpeg', '-i', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3', '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r'] | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode == 0: | |
return "iPhone ringtone created successfully" | |
else: | |
return f"Error creating iPhone ringtone: {stderr.decode()}" | |
except Exception as e: | |
return f"Failed to create iPhone ringtone: {str(e)}" | |
def get_video_filename(output_path): | |
"""Finds and returns the actual downloaded filename""" | |
title = os.path.basename(output_path).replace('%(title)s', '').strip() | |
video_filename = os.path.join(DOWNLOAD_FOLDER, f"{title}.mp4") | |
if os.path.exists(video_filename): | |
return video_filename | |
else: | |
for file in os.listdir(DOWNLOAD_FOLDER): | |
if file.endswith(".mp4"): | |
return os.path.join(DOWNLOAD_FOLDER, file) | |
return None | |
def paste_from_clipboard(): | |
"""Get URL from clipboard""" | |
return pyperclip.paste() | |
def clear_input(): | |
"""Clear the input and output""" | |
return "", "" | |
# Gradio interface functions | |
def download_video_ui(url): | |
return download_video(url) | |
def android_ringtone_ui(): | |
return android_download() | |
def iphone_ringtone_ui(): | |
return iphone_download() | |
# Build the Gradio interface | |
iface = gr.Interface( | |
fn=download_video_ui, | |
inputs=gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here"), | |
outputs=gr.Textbox(label="Status"), | |
live=True | |
) | |
# Add additional buttons for Android and iPhone ringtone creation | |
iface.add_component(gr.Button("Create Android Ringtone", variant="primary").click(android_ringtone_ui, outputs="status")) | |
iface.add_component(gr.Button("Create iPhone Ringtone", variant="primary").click(iphone_ringtone_ui, outputs="status")) | |
iface.add_component(gr.Button("Paste URL from Clipboard", variant="secondary").click(paste_from_clipboard, inputs="url", outputs="url")) | |
iface.add_component(gr.Button("Clear", variant="secondary").click(clear_input, outputs=["url", "status"])) | |
# Launch the Gradio interface on Hugging Face | |
iface.launch(share=True) | |