Here's a comprehensive Python script for a user-friendly yt-dlp interface in Google Colab:
!pip install yt-dlp ipywidgets
import ipywidgets as widgets
from IPython.display import display, HTML, clear_output
import yt_dlp
import os
from datetime import datetime
def create_download_interface():
# Create widgets
url_input = widgets.Text(
description='URL:',
placeholder='Enter video/playlist URL',
style={'description_width': 'initial'},
layout=widgets.Layout(width='70%')
)
format_dropdown = widgets.Dropdown(
options=['Best Quality', 'Audio Only (MP3)', 'Video Only', '720p', '1080p'],
value='Best Quality',
description='Format:',
style={'description_width': 'initial'}
)
output_path = widgets.Text(
description='Save to:',
placeholder='/content/downloads',
value='/content/downloads',
style={'description_width': 'initial'}
)
status_output = widgets.Output()
download_button = widgets.Button(description='Download')
# Create additional options
subtitle_checkbox = widgets.Checkbox(
value=False,
description='Download subtitles',
style={'description_width': 'initial'}
)
thumbnail_checkbox = widgets.Checkbox(
value=False,
description='Download thumbnail',
style={'description_width': 'initial'}
)
def get_ydl_opts(format_choice, output_dir, download_subs, download_thumb):
ydl_opts = {
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
'progress_hooks': [progress_hook],
}
if format_choice == 'Audio Only (MP3)':
ydl_opts.update({
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
})
elif format_choice == '720p':
ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
elif format_choice == '1080p':
ydl_opts['format'] = 'bestvideo[height<=1080]+bestaudio/best[height<=1080]'
elif format_choice == 'Video Only':
ydl_opts['format'] = 'bestvideo/best'
if download_subs:
ydl_opts.update({
'writesubtitles': True,
'writeautomaticsub': True,
'subtitlesformat': 'srt',
})
if download_thumb:
ydl_opts['writethumbnail'] = True
return ydl_opts
def progress_hook(d):
with status_output:
if d['status'] == 'downloading':
clear_output(wait=True)
print(f"Downloading... {d.get('_percent_str', '0%')} complete")
print(f"Speed: {d.get('_speed_str', 'N/A')}")
print(f"ETA: {d.get('_eta_str', 'N/A')}")
elif d['status'] == 'finished':
print('Download completed! Processing...')
def on_download_button_clicked(b):
with status_output:
clear_output()
if not url_input.value:
print("Please enter a URL!")
return
try:
# Create output directory if it doesn't exist
os.makedirs(output_path.value, exist_ok=True)
# Configure yt-dlp options
ydl_opts = get_ydl_opts(
format_dropdown.value,
output_path.value,
subtitle_checkbox.value,
thumbnail_checkbox.value
)
print("Starting download...")
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url_input.value])
print("\nDownload and processing completed successfully!")
except Exception as e:
print(f"An error occurred: {str(e)}")
# Connect button click event
download_button.on_click(on_download_button_clicked)
# Create layout
options_box = widgets.VBox([
url_input,
format_dropdown,
output_path,
widgets.HBox([subtitle_checkbox, thumbnail_checkbox]),
download_button,
status_output
])
display(HTML("<h2>YouTube Downloader</h2>"))
display(options_box)
# Create and display the interface
create_download_interface()
This script creates a user-friendly interface with the following features:
- URL input field
- Format selection dropdown (Best Quality, Audio Only, Video Only, 720p, 1080p)
- Output directory specification
- Options for downloading subtitles and thumbnails
- Download progress display
- Error handling
- Real-time status updates
To use this in Google Colab:
- Create a new notebook
- Paste the code into a cell
- Run the cell
- Use the interface that appears
The interface will allow you to:
- Enter any YouTube URL (video or playlist)
- Select download quality/format
- Choose where to save the files
- Download subtitles and thumbnails if desired
- See download progress and status
The downloads will be saved to the specified directory (default: /content/downloads).
Would you like me to explain any particular part of the code in more detail?