Below is a complete Python script that you can run in a single Colab cell. It installs yt‑dlp (if needed), creates a user‑friendly interface using ipywidgets, and lets you control most yt‑dlp options (including format, output template, error handling, subtitles, and even adding custom command‑line options). When you click “Download,” the script gathers all your settings and downloads the video(s) accordingly. (If you provide “Custom Options” then the UI will switch to a command‑line call using subprocess.)
Copy and paste the following code into a Colab cell and run it:
# Install dependencies (if not already installed)
!pip install yt-dlp ipywidgets --quiet
# Import necessary modules
import ipywidgets as widgets
from IPython.display import display, clear_output
import os
import yt_dlp
# -----------------------------------------------------------------------------
# Define UI elements
# -----------------------------------------------------------------------------
# Text area for one or several URLs (separated by newline)
url_input = widgets.Textarea(
value='',
placeholder='Enter video URL(s), one per line',
description='Video URLs:',
layout=widgets.Layout(width='100%', height='100px')
)
# Text box for specifying the format string (e.g. "best" or "bestvideo+bestaudio")
format_input = widgets.Text(
value='best',
placeholder='Enter format (e.g. bestvideo+bestaudio, best)',
description='Format:'
)
# Text box for specifying the output template (see yt-dlp docs)
output_template_input = widgets.Text(
value='%(title)s.%(ext)s',
placeholder='Enter output template (e.g. %(title)s.%(ext)s)',
description='Output:'
)
# Checkboxes for common yt-dlp flags:
ignore_errors_checkbox = widgets.Checkbox(
value=False,
description='Ignore Errors'
)
write_sub_checkbox = widgets.Checkbox(
value=False,
description='Write Subtitles'
)
write_auto_sub_checkbox = widgets.Checkbox(
value=False,
description='Write Automatic Subs'
)
all_subs_checkbox = widgets.Checkbox(
value=False,
description='Download All Subs'
)
# Text area for any additional custom options.
# Note: When custom options are provided, the script will use a subprocess call
# so that those extra command-line options are respected.
custom_options_input = widgets.Textarea(
value='',
placeholder='Enter additional options (e.g. --no-check-certificate)',
description='Custom Options:',
layout=widgets.Layout(width='100%', height='80px')
)
# Download button
download_button = widgets.Button(
description='Download',
button_style='success'
)
# Output widget to show logs and download progress
output_area = widgets.Output()
# -----------------------------------------------------------------------------
# Define the download callback function
# -----------------------------------------------------------------------------
def download_callback(b):
with output_area:
clear_output() # Clear previous log messages
print("Preparing the download...\n")
# Get the video URLs (separated by newline)
urls_raw = url_input.value.strip()
if not urls_raw:
print("ERROR: Please provide at least one URL in the 'Video URLs' box.")
return
urls = [u.strip() for u in urls_raw.splitlines() if u.strip()]
# Build the yt_dlp options dictionary from widget inputs.
ydl_opts = {}
# Set the format if provided.
if format_input.value:
ydl_opts['format'] = format_input.value.strip()
# Set the output template.
if output_template_input.value:
ydl_opts['outtmpl'] = output_template_input.value.strip()
# Apply common flags.
if ignore_errors_checkbox.value:
ydl_opts['ignoreerrors'] = True
if write_sub_checkbox.value:
ydl_opts['writesubtitles'] = True
if write_auto_sub_checkbox.value:
ydl_opts['writeautomaticsub'] = True
if all_subs_checkbox.value:
ydl_opts['allsubtitles'] = True
# Check for any custom options.
custom_opts = custom_options_input.value.strip()
# If custom options are specified, we call yt-dlp as a subprocess so that
# the additional command-line options (which are not easily supported via the API)
# are passed through.
if custom_opts:
print("Custom options specified. Running a command-line call...\n")
import subprocess
# Build the command list.
command = ["yt-dlp"]
command.extend(custom_opts.split())
# Append UI-selected options to the command line.
if format_input.value:
command.extend(["-f", format_input.value.strip()])
if output_template_input.value:
command.extend(["-o", output_template_input.value.strip()])
if ignore_errors_checkbox.value:
command.append("--ignore-errors")
if write_sub_checkbox.value:
command.append("--write-subs")
if write_auto_sub_checkbox.value:
command.append("--write-auto-subs")
if all_subs_checkbox.value:
command.append("--all-subs")
# Append all URLs to the command.
command.extend(urls)
# Display the full command for debugging.
print("Executing command:\n" + " ".join(command) + "\n")
# Launch the process and stream output.
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
for line in process.stdout:
print(line, end='')
process.wait()
print("\nDownload finished (via subprocess).")
return
# If there are no custom options, use the Python API.
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
for url in urls:
print(f"Downloading: {url}")
ydl.download([url])
print("\nDownload(s) finished (via the Python API).")
except Exception as e:
print("An error occurred during download:", e)
# Register the callback with the download button.
download_button.on_click(download_callback)
# -----------------------------------------------------------------------------
# Layout the UI and display all widgets
# -----------------------------------------------------------------------------
ui = widgets.VBox([
url_input,
widgets.HBox([format_input, output_template_input]),
widgets.HBox([
ignore_errors_checkbox,
write_sub_checkbox,
write_auto_sub_checkbox,
all_subs_checkbox
]),
custom_options_input,
download_button,
output_area
])
display(ui)
How It Works
-
Installation & Imports:
The code installs the necessary packages (yt-dlp and ipywidgets) and imports modules.
-
UI Elements:
- Video URLs: Paste one or more video URLs (each on a new line).
- Format & Output: You can set the download format (for example,
"bestvideo+bestaudio" or "best") and the output filename template, e.g. "%(title)s.%(ext)s".
- Flags: Use checkboxes to toggle common options like ignoring errors or downloading subtitles.
- Custom Options: If there are extra command‑line options not covered by the UI, add them in the custom options box.
-
Download Process:
- If custom options are provided, the script uses a subprocess call to run
yt-dlp exactly as you would from the command line.
- Otherwise, it uses the yt‑dlp Python API with the options you have set.
-
Output:
The log messages and yt‑dlp output are printed into the output area below the button.
Enjoy a fully GUI‑based yt‑dlp experience inside Google Colab—no need to touch the command line!