# Install yt-dlp if not already installed
!pip install yt-dlp
import yt_dlp
import os
from ipywidgets import *
from IPython.display import display, clear_output
# Create UI Widgets
url_input = Text(description='Video URL:', layout=Layout(width='80%'))
format_dropdown = Dropdown(
options=[
'best',
'worst',
'bestvideo+bestaudio',
'bestaudio',
'worstvideo',
'mp4',
'mp3',
'webm'
],
description='Format:',
layout=Layout(width='80%')
)
output_dir_input = Text(description='Output Directory:', value='/content/', layout=Layout(width='80%'))
extract_audio_checkbox = Checkbox(value=False, description='Extract Audio', layout=Layout(width='80%'))
download_subtitles_checkbox = Checkbox(value=False, description='Download Subtitles', layout=Layout(width='80%'))
download_button = Button(description="Download", layout=Layout(width='20%'))
def on_download_button_click(b):
# Collect user inputs
url = url_input.value
format_choice = format_dropdown.value
output_dir = output_dir_input.value
extract_audio = extract_audio_checkbox.value
download_subtitles = download_subtitles_checkbox.value
# Validate URL
if not url:
print("⚠️ Please enter a valid YouTube URL.")
return
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Prepare yt-dlp options
ydl_opts = {
'format': format_choice,
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
'progress_hooks': [lambda d: print(f"Downloading: {d.get('status', '')} | {d.get('filename', '')}")]
}
# Apply additional options based on user selections
if extract_audio:
ydl_opts['postprocessors'] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
if download_subtitles:
ydl_opts['writesubtitles'] = True
ydl_opts['subtitlesformat'] = 'srt'
# Perform the download
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print("✅ Download completed successfully!")
except Exception as e:
print(f"❌ Error: {str(e)}")
# Attach event handler
download_button.on_click(on_download_button_click)
# Display the UI
print("🎬 YouTube Downloader (yt-dlp) - Google Colab")
print("--------------------------------------------------")
display(url_input, format_dropdown, output_dir_input, extract_audio_checkbox, download_subtitles_checkbox, download_button)
📝 Features & Usage
- User-Friendly Interface: A clean, interactive form to configure your download.
- Format Selection: Choose from common formats like
best, mp3, mp4, etc.
- Output Directory: Specify where to save the file (default:
/content/).
- Audio Extraction: Convert videos to MP3 with high-quality audio.
- Subtitles: Download SRT subtitles if available.
- No Command Line Required: All settings are controlled via the web-based UI.
🔧 Example Workflow
- Enter a YouTube URL (e.g.,
https://www.youtube.com/watch?v=example)
- Select Format: Choose
mp3 for audio only, or best for full video.
- Check Options:
Extract Audio to convert to MP3.
Download Subtitles to get SRT files.
- Click "Download" to start.
⚠️ Notes
- Ensure the YouTube URL is valid and accessible.
- If using
Extract Audio, ensure FFmpeg is available (it's pre-installed in Colab).
- The output will be saved in the specified directory, which you can access via the Colab file explorer.
This script provides a complete, end-to-end solution for downloading YouTube content with full control over yt-dlp settings, all within a simple, interactive UI in Google Colab.