Spaces:
Runtime error
Runtime error
import gradio as gr | |
import subprocess | |
import os | |
import tempfile | |
# Function to run yt-dlp commands and return the file path | |
def run_yt_dlp(command, url, bitrate=None, start_time=None, end_time=None, playlist_items=None, archive_file=None): | |
# Temporary directory to store the downloaded files | |
temp_dir = tempfile.mkdtemp() | |
# Base command for yt-dlp | |
base_command = ["yt-dlp", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{temp_dir}/%(title)s.%(ext)s"] | |
if command == "list_formats": | |
base_command = ["yt-dlp", "-F", url] | |
elif command == "download_mp3_max_quality": | |
base_command.extend(["--postprocessor-args", "-b:a 320k", url]) | |
elif command == "basic_high_quality_mp3": | |
base_command.append(url) | |
elif command == "custom_bitrate": | |
base_command.extend(["--postprocessor-args", f"-b:a {bitrate}k", url]) | |
elif command == "add_metadata_tags": | |
base_command.extend(["--embed-metadata", url]) | |
elif command == "download_playlist": | |
base_command.append(url) | |
elif command == "download_playlist_range": | |
base_command.extend(["--playlist-items", playlist_items, url]) | |
elif command == "download_hq_soundcloud": | |
base_command.extend(["-o", f"{temp_dir}/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "embed_album_art": | |
base_command.extend(["--embed-thumbnail", url]) | |
elif command == "download_opus": | |
base_command.extend(["--audio-format", "opus", url]) | |
elif command == "skip_downloaded_files": | |
base_command.extend(["--download-archive", archive_file, url]) | |
elif command == "download_multiple_files": | |
base_command.extend(url.split()) | |
elif command == "download_split_chapters": | |
base_command.extend(["--split-chapters", url]) | |
elif command == "extract_portion": | |
base_command.extend(["-f", "bestvideo+bestaudio", "--external-downloader", "ffmpeg", "--external-downloader-args", f"ffmpeg_i:-ss {start_time} -to {end_time}", url]) | |
elif command == "update_metadata": | |
base_command.extend(["--download-archive", archive_file, "--skip-download", "--embed-metadata", "--embed-thumbnail", url]) | |
elif command == "auto_rename": | |
base_command.extend(["--force-overwrites", url]) | |
elif command == "download_url_list": | |
base_command.extend(["-a", url]) | |
elif command == "limit_speed": | |
base_command.extend(["--rate-limit", "1M", url]) | |
elif command == "download_vimeo": | |
base_command.extend(["-o", f"{temp_dir}/Vimeo/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_facebook": | |
base_command.extend(["-o", f"{temp_dir}/Facebook/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_instagram": | |
base_command.extend(["-o", f"{temp_dir}/Instagram/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_twitter": | |
base_command.extend(["-o", f"{temp_dir}/Twitter/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_tiktok": | |
base_command.extend(["-o", f"{temp_dir}/TikTok/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_reddit": | |
base_command.extend(["-o", f"{temp_dir}/Reddit/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_dailymotion": | |
base_command.extend(["-o", f"{temp_dir}/Dailymotion/%(uploader)s - %(title)s.%(ext)s", url]) | |
elif command == "download_transcript_text": | |
base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", url]) | |
elif command == "download_transcript_markdown": | |
base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", "--convert-subs", "srt", "-o", f"{temp_dir}/%(title)s.md", url]) | |
elif command == "download_video_info": | |
base_command.extend(["--write-info-json", "--skip-download", "-o", f"{temp_dir}/%(title)s.json", url]) | |
elif command == "list_formats_no_download": | |
base_command = ["yt-dlp", "-F", url] | |
elif command == "download_age_restricted": | |
base_command.extend(["--username", "YOUR_YOUTUBE_EMAIL", "--password", "YOUR_PASSWORD", "-f", "bestvideo+bestaudio", url]) | |
try: | |
result = subprocess.run(base_command, capture_output=True, text=True) | |
if result.returncode != 0: | |
return None, result.stderr | |
else: | |
# Find the downloaded file | |
downloaded_files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))] | |
if downloaded_files: | |
file_path = os.path.join(temp_dir, downloaded_files[0]) | |
return file_path, result.stdout | |
else: | |
return None, "No file downloaded." | |
except Exception as e: | |
return None, str(e) | |
# Gradio Interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# yt-dlp Command Executor") | |
with gr.Row(): | |
command = gr.Dropdown(choices=[ | |
"list_formats", | |
"download_mp3_max_quality", | |
"basic_high_quality_mp3", | |
"custom_bitrate", | |
"add_metadata_tags", | |
"download_playlist", | |
"download_playlist_range", | |
"download_hq_soundcloud", | |
"embed_album_art", | |
"download_opus", | |
"skip_downloaded_files", | |
"download_multiple_files", | |
"download_split_chapters", | |
"extract_portion", | |
"update_metadata", | |
"auto_rename", | |
"download_url_list", | |
"limit_speed", | |
"download_vimeo", | |
"download_facebook", | |
"download_instagram", | |
"download_twitter", | |
"download_tiktok", | |
"download_reddit", | |
"download_dailymotion", | |
"download_transcript_text", | |
"download_transcript_markdown", | |
"download_video_info", | |
"list_formats_no_download", | |
"download_age_restricted" | |
], label="Command") | |
url = gr.Textbox(label="URL", placeholder="Enter the URL here") | |
with gr.Row(): | |
bitrate = gr.Textbox(label="Bitrate (for custom bitrate)", placeholder="Enter bitrate in kbps, e.g., 192") | |
start_time = gr.Textbox(label="Start Time (HH:MM:SS)", placeholder="Enter start time, e.g., 00:01:00") | |
end_time = gr.Textbox(label="End Time (HH:MM:SS)", placeholder="Enter end time, e.g., 00:05:00") | |
playlist_items = gr.Textbox(label="Playlist Items (e.g., 1-5)", placeholder="Enter range of items, e.g., 1-5") | |
archive_file = gr.Textbox(label="Archive File", placeholder="Enter path to archive file, e.g., P:/Local Music/downloaded.txt") | |
execute_button = gr.Button("Execute Command") | |
file_output = gr.File(label="Downloaded File") | |
output = gr.Textbox(label="Output", lines=10) | |
def execute_and_download(command, url, bitrate, start_time, end_time, playlist_items, archive_file): | |
file_path, result = run_yt_dlp(command, url, bitrate, start_time, end_time, playlist_items, archive_file) | |
if file_path: | |
return file_path, result | |
else: | |
return None, result | |
execute_button.click( | |
fn=execute_and_download, | |
inputs=[command, url, bitrate, start_time, end_time, playlist_items, archive_file], | |
outputs=[file_output, output] | |
) | |
demo.launch() |