Command-line program to download videos from YouTube.com and other video sites
The YoutubeDL class is the primary interface for video downloading operations. It orchestrates the entire download process from URL parsing to file output, providing extensive configuration options and hooks for customization.
Creates a new YoutubeDL instance with extensive configuration options for controlling download behavior, output formatting, and processing options.
class YoutubeDL:
def __init__(self, params=None, auto_init=True):
"""
Main youtube-dl downloader class.
Parameters:
- params (dict): Configuration dictionary with download options
- auto_init (bool): Whether to automatically initialize the instance
"""Primary methods for downloading videos and extracting information from supported sites.
def download(self, url_list):
"""
Download a list of URLs.
Parameters:
- url_list (list): List of URLs to download
Returns:
int: Return code (0 for success, non-zero for errors)
"""
def extract_info(self, url, download=True, ie_key=None, extra_info={}, process=True, force_generic_extractor=False):
"""
Extract information from URL without necessarily downloading.
Parameters:
- url (str): URL to extract information from
- download (bool): Whether to download the video file
- ie_key (str): Force specific info extractor
- extra_info (dict): Additional information to merge
- process (bool): Whether to process the extracted information
- force_generic_extractor (bool): Force using generic extractor
Returns:
dict/list: Video information dictionary or list of dictionaries
"""
def process_info(self, info_dict):
"""
Process extracted information and download if requested.
Parameters:
- info_dict (dict): Video information dictionary
Returns:
dict: Processed information dictionary
"""Methods for handling and validating URLs before processing.
def urlopen(self, req):
"""
Open URL with configured request parameters.
Parameters:
- req: Request object or URL string
Returns:
Response object
"""
def cache_fn(self, path):
"""
Return cached file path for given path.
Parameters:
- path (str): File path
Returns:
str: Cache file path
"""Methods for handling progress reporting and logging during downloads.
def to_screen(self, message, skip_eol=False):
"""
Print message to screen if not in quiet mode.
Parameters:
- message (str): Message to display
- skip_eol (bool): Whether to skip end-of-line
"""
def report_warning(self, message):
"""
Report a warning message.
Parameters:
- message (str): Warning message
"""
def report_error(self, message, tb=None):
"""
Report an error message.
Parameters:
- message (str): Error message
- tb (str): Optional traceback
"""Methods for handling playlists and multiple URL processing.
def download_with_info_file(self, info_filename):
"""
Download using information from JSON file.
Parameters:
- info_filename (str): Path to info JSON file
Returns:
int: Return code
"""
def prepare_filename(self, info_dict):
"""
Generate filename from template and video information.
Parameters:
- info_dict (dict): Video information dictionary
Returns:
str: Generated filename
"""The YoutubeDL constructor accepts a comprehensive params dictionary with these key options:
username (str): Username for authenticationpassword (str): Password for authenticationvideopassword (str): Password for video accessquiet (bool): Suppress console outputverbose (bool): Enable verbose outputno_warnings (bool): Suppress warning messagesformat (str): Video format selector (e.g., 'best', 'worst', 'bestaudio')outtmpl (str): Output filename templaterestrictfilenames (bool): Restrict filenames to ASCII charactersignoreerrors (bool): Continue on download errorsnooverwrites (bool): Don't overwrite existing filescontinuedl (bool): Continue partial downloadsretries (int/str): Number of retries or 'infinite'socket_timeout (float): Socket timeout in secondsproxy (str): Proxy URLsource_address (str): Client-side IP address to bind tosleep_interval (float): Sleep interval between downloadsmax_sleep_interval (float): Upper bound of sleep intervalpostprocessors (list): List of post-processor configurationskeepvideo (bool): Keep video file after post-processingmin_filesize (int): Minimum file size for downloadmax_filesize (int): Maximum file size for downloaddaterange (DateRange): Date range for video filteringfrom youtube_dl import YoutubeDL
ydl_opts = {}
with YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=VIDEO_ID'])with YoutubeDL({'quiet': True}) as ydl:
info = ydl.extract_info('https://www.youtube.com/watch?v=VIDEO_ID', download=False)
title = info.get('title', 'Unknown')
duration = info.get('duration', 0)ydl_opts = {
'outtmpl': '%(uploader)s/%(title)s.%(ext)s',
'format': 'best[height<=720]'
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=VIDEO_ID'])Install with Tessl CLI
npx tessl i tessl/pypi-youtube-dl