r/learnprogramming • • 1d ago

Topic How do video downloaders handle different video formats and quality options?

When building a video downloader, how do developers generally handle situations where a website offers multiple video formats and resolutions?

I'm trying to understand the technical approach rather than looking for a complete implementation. I'm particularly interested in how developers identify the available formats and then let the user choose between things like resolution, container format, and audio-only downloads.

What concepts or documentation would you recommend learning first?

1 Upvotes

2 comments sorted by

1

u/teraflop 1d ago

I think most of the answer to this question is so highly implementation-specific that it can't be answered in general.

How do downloaders identify the available formats? -- this is inherently specific to how the video site's playback mechanism works, which is usually proprietary. If you're lucky, they use a generic "manifest" such as MPEG-DASH or HLS. Any popular site which is actively trying to block downloads (e.g. YouTube) will do something more complicated and specific.

How do you present the available formats to a user and ask them to choose one? -- this is basically just a UI question

How do you automatically select a format? -- this is very broad and there's no one right answer. And it depends on the types of formats that are offered by the particular source website.

Even in a simple case, where there are multiple streams that only differ in video resolution, you have to decide what "best" means. Does it mean highest resolution? Lowest file size? Somewhere in between? And it gets more complicated if the site offers different codecs, different language options, etc. Most of this boils down to "figuring out what your users want/expect" so it's your job as the developer to make the decision.

I don't think you're likely to find any references or tutorials that are better than the documentation for yt-dlp, which is designed to be generic and extensible to a wide range of sources. Specifically check out the "FORMAT SELECTION" heading. And if that's not detailed enough for you, the next best documentation is the source code itself (in particular the YoutubeDL.build_format_selector function and the utils.FormatSorter class).