Below is a complete Python script for Google Colab that creates a user-friendly interface for yt-dlp. The interface allows users to configure and use all yt-dlp settings and features without interacting with the command line. This script uses ipywidgets for the UI and integrates seamlessly with Google Colab.
# Install 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 run_ytdlp(url, output_dir, format_id, extract_audio, audio_format, embed_thumbnail, add_metadata):
try:
# Construct the yt-dlp command
command = f"yt-dlp -o '{output_dir}/%(title)s.%(ext)s' "
if extract_audio:
command += "--extract-audio "
if audio_format:
command += f"--audio-format {audio_format} "
else:
if format_id:
command += f"-f {format_id} "
if embed_thumbnail:
command += "--embed-thumbnail "
if add_metadata:
command += "--add-metadata "
command += f"'{url}'"
# Execute the command
print(f"Running command: {command}")
!{command}
# Display success message
output_message.value = "Download completed successfully!"
except Exception as e:
output_message.value = f"An error occurred: {str(e)}"
# Create the UI components
url_input = widgets.Text(
placeholder="Enter video URL",
description="Video URL:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='500px')
)
output_dir_input = widgets.Text(
value="/content/downloads",
description="Output Directory:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='500px')
)
format_dropdown = widgets.Dropdown(
options=["best", "mp4", "webm", "3gp", "worst"],
value="best",
description="Video Format:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='250px')
)
extract_audio_checkbox = widgets.Checkbox(
value=False,
description="Extract Audio",
style={'description_width': 'initial'}
)
audio_format_dropdown = widgets.Dropdown(
options=["mp3", "aac", "flac", "opus", "vorbis", "wav"],
value="mp3",
description="Audio Format:",
style={'description_width': 'initial'},
layout=widgets.Layout(width='250px'),
disabled=True
)
embed_thumbnail_checkbox = widgets.Checkbox(
value=False,
description="Embed Thumbnail",
style={'description_width': 'initial'}
)
add_metadata_checkbox = widgets.Checkbox(
value=False,
description="Add Metadata",
style={'description_width': 'initial'}
)
download_button = widgets.Button(
description="Download",
button_style="success",
layout=widgets.Layout(width='150px')
)
output_message = widgets.HTML(
value="",
layout=widgets.Layout(height='50px')
)
# Callbacks
def toggle_audio_format(*args):
audio_format_dropdown.disabled = not extract_audio_checkbox.value
extract_audio_checkbox.observe(toggle_audio_format, names='value')
def download_button_clicked(b):
# Clear previous output
clear_output(wait=True)
# Ensure the output directory exists
output_dir = output_dir_input.value.strip()
os.makedirs(output_dir, exist_ok=True)
# Run yt-dlp with the selected options
run_ytdlp(
url=url_input.value.strip(),
output_dir=output_dir,
format_id=format_dropdown.value if not extract_audio_checkbox.value else None,
extract_audio=extract_audio_checkbox.value,
audio_format=audio_format_dropdown.value if extract_audio_checkbox.value else None,
embed_thumbnail=embed_thumbnail_checkbox.value,
add_metadata=add_metadata_checkbox.value
)
download_button.on_click(download_button_clicked)
# Display the UI
display(
widgets.VBox([
url_input,
output_dir_input,
widgets.HBox([format_dropdown, extract_audio_checkbox]),
audio_format_dropdown,
embed_thumbnail_checkbox,
add_metadata_checkbox,
download_button,
output_message
])
)
Explanation of the Code:
-
Installation of Dependencies:
- The script installs
yt-dlp and ipywidgets using pip.
-
UI Components:
- A text input for the video URL.
- A text input for specifying the output directory (default is
/content/downloads).
- Dropdown menus for selecting video and audio formats.
- Checkboxes for enabling/disabling features like audio extraction, embedding thumbnails, and adding metadata.
-
Dynamic Behavior:
- The
audio_format_dropdown is enabled only when the "Extract Audio" checkbox is checked.
-
Download Logic:
- The
run_ytdlp function constructs and executes the yt-dlp command based on user inputs.
- It handles both video and audio downloads, including optional features like embedding thumbnails and adding metadata.
-
Error Handling:
- Errors during the download process are caught and displayed in the UI.
-
Output Directory:
- Ensures the specified output directory exists before starting the download.
-
User-Friendly Interface:
- All options are presented in a clean and intuitive layout using
ipywidgets.
How to Use:
- Copy and paste this script into a Google Colab notebook cell.
- Run the cell to load the UI.
- Enter the video URL, configure the options, and click "Download".
- The downloaded files will be saved in the specified output directory.
This script provides a comprehensive and user-friendly way to interact with yt-dlp in Google Colab without needing to use the command line directly.