Simple Python CLI to download from YouTube. Uses pytubefix, a maintained fork of pytube. Option 3 now asks you which resolution to grab, then merges the best audio with it.
- 1. Audio Only: downloads highest bitrate audio, saves as .m4a
- 2. Video Only: downloads highest resolution MP4 video, no audio
- 3. Best Quality with choice: lists all available MP4 resolutions, you pick one, then it downloads that video plus best audio and merges to a single MP4 with ffmpeg
- 4. Progressive: downloads classic YouTube MP4 with audio and video together, max 720p, no ffmpeg needed
- Python 3.8 or newer
- pip install pytubefix
- ffmpeg in PATH (required only for option 3)
pip install pytubefixInstall ffmpeg:
- Windows: winget install ffmpeg
- macOS: brew install ffmpeg
- Linux: sudo apt install ffmpeg
Verify: ffmpeg -version
python yt_downloader.py- Paste YouTube URL
- Choose 1 to 4
- If you choose 3, you will see:
Available qualities:
1. 2160p (60fps, avc1)
2. 1440p (60fps, avc1)
3. 1080p (60fps, avc1)
4. 720p (30fps, avc1)
Choose 1-4 [Enter for best 2160p]:
Press Enter for the best, or type 2 for 1440p, etc.
Files go to ./downloads/
Selects only_audio streams, orders by abr, downloads first. Renames to .m4a. This is AAC audio, not MP3. To get MP3, run: ffmpeg -i file.m4a file.mp3
Selects only_video MP4 streams, orders by resolution, downloads first. No audio track included.
- Builds a unique list of MP4 video-only resolutions
- Lets you pick
- Downloads best audio separately
- Checks for ffmpeg first. If missing, it stops and keeps the two files
- Merges with:
ffmpeg -y -i video -i audio -c:v copy -c:a aac -movflags +faststart output.mp4 - Output name includes resolution:
Video Title_1080p.mp4
Selects progressive MP4 streams. These are pre-merged by YouTube and capped at 720p. If none exist, the script exits with a message.
downloads/
My Song.m4a
video_only_My Video.mp4
My Video_2160p.mp4
My Video_1080p.mp4
Old Video.mp4
- RegexMatchError or VideoUnavailable: YouTube changed. Update:
pip install -U pytubefix - ffmpeg not found: option 3 will not merge. Install ffmpeg and ensure it is in PATH
- No MP4 video-only streams: some videos are VP9 or AV1 only. Remove
file_extension="mp4"in the filter to see them, then output to .mkv - No progressive stream: common for 1080p and above. Use option 3
- Slow speeds: YouTube throttles. Try again later or use option 4
YouTube's true best is often VP9 in webm, not H.264 in MP4. This script filters for MP4 for phone compatibility. If you want 4K HDR VP9:
- Change
filter(only_video=True, file_extension="mp4")tofilter(only_video=True) - Change output extension to
.mkv - ffmpeg will copy both streams with no re-encode
Download only content you own or have permission to save. You are responsible for following YouTube Terms of Service and copyright law in Pakistan and your region.
- Change folder: edit
out_dir = "downloads" - Auto-pick without prompt: set
video = unique[resolutions[0]]and skip input - Force 1080p max: filter by
if int(s.resolution[:-1]) <= 1080