Functions
download_youtube_video
Downloads a YouTube video with interactive quality selection and automatic handling of progressive and adaptive streams.The YouTube video URL to download
Path to the downloaded video file in the
videos/ directoryFeatures
- Interactive Resolution Selection: Shows top 5 available resolutions with sizes
- Auto-Selection: 5-second timeout automatically selects highest quality
- Adaptive Stream Handling: Automatically merges separate video/audio streams using ffmpeg
- Safe Filenames: Sanitizes video titles by removing invalid filename characters
Process Flow
- Fetches available video streams and audio stream
- Displays top 5 resolution options with file sizes
- Waits for user input (5 second timeout)
- Downloads selected video stream
- For adaptive streams:
- Downloads audio stream separately
- Merges video and audio using ffmpeg with H.264/AAC codecs
- Removes temporary files
- Returns path to final video file
Downloaded videos are saved to the
videos/ directory, which is automatically created if it doesn’t exist.get_video_size
Helper function to calculate video stream file size in megabytes.PyTube stream object to calculate size for
File size in megabytes (MB)
Example
Output Format
Videos are saved with sanitized titles:- Invalid characters (
|:?*<>"/\) are removed or replaced with hyphens - Format:
videos/[Sanitized Title].mp4 - Codec: H.264 (libx264) for video, AAC for audio
Error Handling
The function catches all exceptions and provides helpful error messages:- Missing dependencies (pytube, ffmpeg-python, ffmpeg)
- Network issues
- Invalid URLs
- Download failures
