Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import subprocess | |
| import os | |
| import logging | |
| import pyperclip | |
| logging.basicConfig(level=logging.INFO) | |
| DOWNLOAD_FOLDER = './output' | |
| COOKIES_PATH = './cookies.txt' | |
| if not os.path.exists(DOWNLOAD_FOLDER): | |
| os.makedirs(DOWNLOAD_FOLDER) | |
| def create_readme(): | |
| path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt') | |
| try: | |
| with open(path, 'w') as f: | |
| f.write("Android: Copy .mp3 to ringtones folder and reboot. iPhone: Drag .m4r into iTunes and sync.") | |
| return f"readme.txt created at: {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_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s') | |
| command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url] | |
| if os.path.exists(COOKIES_PATH): | |
| command.extend(['--cookies', COOKIES_PATH]) | |
| else: | |
| return f"Error: Cookies file {COOKIES_PATH} not found." | |
| logging.info(f"Running: {' '.join(command)}") | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| stdout, stderr = process.communicate() | |
| logging.info(stdout.decode()) | |
| logging.error(stderr.decode()) | |
| if process.returncode == 0: | |
| video_file = get_video_filename() | |
| if video_file: | |
| return f"Video downloaded: {os.path.basename(video_file)}\n" + create_readme() | |
| else: | |
| return "Error: Video file not found." | |
| else: | |
| return f"Download error: {stderr.decode()}" | |
| except Exception as e: | |
| return f"Failed to download video: {str(e)}" | |
| def android_download(): | |
| try: | |
| video_file = get_video_filename() | |
| if not video_file: | |
| return "Error: No video file found.", None | |
| output_file = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3') | |
| command = ['ffmpeg', '-i', video_file, '-t', '20', '-q:a', '0', '-map', 'a', output_file] | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| stdout, stderr = process.communicate() | |
| if process.returncode == 0: | |
| return "Android ringtone created successfully", output_file | |
| else: | |
| return f"FFmpeg error: {stderr.decode()}", None | |
| except Exception as e: | |
| return f"Error creating Android ringtone: {str(e)}", None | |
| def iphone_download(): | |
| try: | |
| input_file = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3') | |
| output_file = os.path.join(DOWNLOAD_FOLDER, 'iPhoneRingtone.m4r') | |
| command = ['ffmpeg', '-i', input_file, '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', output_file] | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| stdout, stderr = process.communicate() | |
| if process.returncode == 0: | |
| return "iPhone ringtone created successfully", output_file | |
| else: | |
| return f"FFmpeg error: {stderr.decode()}", None | |
| except Exception as e: | |
| return f"Error creating iPhone ringtone: {str(e)}", None | |
| def paste_from_clipboard(): | |
| return pyperclip.paste() | |
| def clear_input(): | |
| return "", "", None, None | |
| with gr.Blocks() as iface: | |
| gr.Markdown("# π΅ YouTube to Ringtone Converter") | |
| url_input = gr.Textbox(label="YouTube URL", placeholder="Paste URL here") | |
| status_output = gr.Textbox(label="Status", interactive=False) | |
| with gr.Row(): | |
| download_button = gr.Button("π₯ Download Video") | |
| android_button = gr.Button("π± Android Ringtone") | |
| iphone_button = gr.Button("π iPhone Ringtone") | |
| with gr.Row(): | |
| paste_button = gr.Button("π Paste from Clipboard") | |
| clear_button = gr.Button("π§Ή Clear") | |
| with gr.Row(): | |
| android_file = gr.File(label="β¬οΈ Android Ringtone (.mp3)", interactive=False) | |
| iphone_file = gr.File(label="β¬οΈ iPhone Ringtone (.m4r)", interactive=False) | |
| download_button.click(download_video, inputs=url_input, outputs=status_output) | |
| android_button.click(android_download, outputs=[status_output, android_file]) | |
| iphone_button.click(iphone_download, outputs=[status_output, iphone_file]) | |
| paste_button.click(paste_from_clipboard, outputs=url_input) | |
| clear_button.click(clear_input, outputs=[url_input, status_output, android_file, iphone_file]) | |
| iface.launch(share=True) | |