Write a full single python code for Google Colab. It features a user friendly yt-dlp application. All together, the UI gives you end‑to‑end control over all yt-dlp settings and features without ever touching the command line.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a complete Python script designed for Google Colab that creates a user-friendly interface for yt-dlp. This script leverages the ipywidgets library to provide an interactive UI, allowing users to configure and execute yt-dlp commands without using the command line. The script includes options for video/audio format selection, download location, and additional settings.
python
# Install yt-dlp and required libraries
!pip install yt-dlp ipywidgets
import os
import ipywidgets as widgets
from IPython.display import display, clear_output
from google.colab import files
# Function to run yt-dlp with selected options
def download_video(button):
clear_output(wait=True)
# Retrieve user inputs
url = url_input.value.strip()
output_dir = output_dir_input.value.strip()
format_choice = format_dropdown.value
quality_choice = quality_dropdown.value
custom_args = custom_args_input.value.strip()
if not url:
print("Error: Please provide a valid URL.")
return
if not output_dir:
output_dir = "/content/downloads"
os.makedirs(output_dir, exist_ok=True)
# Build yt-dlp command
command = f"yt-dlp -o '{output_dir}/%(title)s.%(ext)s'"
if format_choice == "Audio Only":
command += " -x --audio-format mp3"
elif format_choice == "Video":
if quality_choice != "Best":
command += f" -f 'bestvideo[height<={quality_choice}]+bestaudio/best'"
else:
command += " -f bestvideo+bestaudio/best"
if custom_args:
command += f" {custom_args}"
command += f" {url}"
# Display the command (optional)
print(f"Running command: {command}")
# Execute the command
try:
!{command}
print("\nDownload completed successfully!")
except Exception as e:
print(f"An error occurred: {e}")
# Create UI elements
url_input = widgets.Text(
placeholder="Enter video URL",
description="URL:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='50%')
)
output_dir_input = widgets.Text(
placeholder="/content/downloads",
description="Output Directory:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='50%')
)
format_dropdown = widgets.Dropdown(
options=["Video", "Audio Only"],
value="Video",
description="Format:",
style={'description_width': 'initial'}
)
quality_dropdown = widgets.Dropdown(
options=["Best", "1080p", "720p", "480p", "360p", "240p", "144p"],
value="Best",
description="Quality:",
style={'description_width': 'initial'}
)
custom_args_input = widgets.Text(
placeholder="--no-playlist --write-sub",
description="Custom Args:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='50%')
)
download_button = widgets.Button(
description="Download",
button_style="success",
tooltip="Start Download",
icon="download"
)
# Attach callback to the button
download_button.on_click(download_video)
# Display the UI
display(widgets.VBox([
widgets.Label("YouTube Downloader"),
url_input,
output_dir_input,
format_dropdown,
quality_dropdown,
custom_args_input,
download_button
]))
Key Features of the Code:
Interactive Widgets: The script uses ipywidgets to create an intuitive UI.
Flexible Format Selection: Users can choose between downloading video or audio-only formats.
Quality Options: Users can select video quality from predefined options like 1080p, 720p, etc., or opt for the best available quality.
Custom Arguments: Advanced users can input custom yt-dlp arguments for additional control.
Error Handling: The script checks for missing URLs and handles errors gracefully.
Default Output Directory: If no output directory is specified, files are saved in /content/downloads.
How to Use:
Copy and paste the code into a Google Colab notebook cell.
Run the cell to generate the UI.
Enter the video URL, select your desired format and quality, and specify any custom arguments.
Click the "Download" button to start the download process.
This script provides a seamless way to use yt-dlp without needing to interact with the command line directly.