Filtering and Sorting Formats in yt-dlp
yt-dlp
offers advanced options for filtering and sorting available formats, enabling you to precisely select the media quality and type you want to download. Below is an updated guide on how to use these features effectively.
Filtering Formats
Use the -f
or --format
option to filter formats based on specific criteria.
Basic Syntax
yt-dlp -f FILTER URL
Filter Operators
Operator | Description |
---|---|
+ | Merge two formats (e.g., separate video and audio) |
/ | Fallback if the former format is unavailable |
, | Download multiple formats |
Common Filters
Filter | Description |
---|---|
best | Best quality format (video + audio). |
worst | Worst quality format. |
bestvideo | Best quality video-only format. |
bestaudio | Best quality audio-only format. |
Advanced Filtering
Use square brackets []
to apply additional filters:
bestvideo[height<=720]
: Best video with height no more than 720p.bestaudio[ext=m4a]
: Best M4A audio format.best[fps>30][height>720]
: Best format with more than 30 fps and height greater than 720p.
Filtering Examples
-
Best MP4 video with M4A audio:
yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]' URL
-
Best video no larger than 720p:
yt-dlp -f 'bestvideo[height<=720]+bestaudio/best[height<=720]' URL
-
Worst video at least 480p:
yt-dlp -f 'worst[height>=480]' URL
Sorting Formats
Use the -S
or --format-sort
option to sort formats based on multiple criteria.
Basic Syntax
yt-dlp -S SORT_ORDER URL
Sorting Fields
Field | Description |
---|---|
filesize | File size. |
res | Video resolution. |
fps | Frame rate. |
codec:vcodec | Video codec preference. |
codec:acodec | Audio codec preference. |
br | Average bitrate. |
asr | Audio sample rate. |
proto | Protocol preference. |
ext | File extension preference. |
Sorting Order
- Descending order (default):
field1,field2,field3
- Ascending order:
+field1,+field2,+field3
Sorting Examples
-
Prefer higher resolution, then higher bitrate:
yt-dlp -S 'res,br' URL
-
Prefer MP4 container, then higher resolution:
yt-dlp -S 'ext:mp4:m4a,res' URL
-
Prefer better codec, then higher bitrate, then larger file size:
yt-dlp -S '+codec,+br,+size' URL
Combining Filtering and Sorting
You can use both filtering and sorting together for precise format selection:
yt-dlp -f 'bestvideo[height<=1080]+bestaudio' -S 'proto,ext:mp4:m4a,res,br' URL
This command:
- Selects the best video up to 1080p and best audio.
- Sorts by protocol, preferring MP4/M4A container, higher resolution, and higher bitrate.
Tips for Effective Filtering and Sorting
- List Formats First: Use
-F
or--list-formats
to view available formats before filtering/sorting. - Combine Criteria: Use multiple criteria for more precise selection.
- Verbose Mode: Use
--verbose
to see howyt-dlp
interprets your format selection. - Check Availability: Not all options may be available for every video source.
- Simulate First: Test your filters with
--simulate
to ensure they work as expected.
Additional Options
-
Merge Output Format: Specify the container format when merging audio and video:
yt-dlp --merge-output-format mp4 -f 'bestvideo+bestaudio' URL
-
Download Subtitles: Include subtitles in the download:
yt-dlp --write-subs -f 'best' URL
Conclusion
By mastering format filtering and sorting in yt-dlp
, you can ensure you're always downloading the exact quality and format of media you need. Experiment with different filters and sorting options to optimize your downloads.
For more advanced usage, refer to the official yt-dlp
documentation or use yt-dlp --help
for a full list of options.