Diary of youtube-dl internals, part 3
Tags: python
Published: 2020-11-30 05:42:07

This part will be about the command line options in youtube-dl. We will also inspect how some of them are implemented.

We will go over some of the basic options and a few of the more exotic options. It is good to know how your command-line arguments are being processed in an application.

First of all, the most important option (well, more of a feature than an option) is the URL that you give it at the end. From this URL, youtube-dl has to figure out from which site it comes from, and what kind of resource it points to, as in whether this is a link to a video, playlist, search result, profile or something else.


## class `YoutubeDL`

Despite it's name, it's a generic class that contains the values of the options in a condensed form, and has high-level classes for downloading URLs without having any website-specific code in it. The dirty work of doing the downloading is done by the *extractors* of each site.

When you run youtube-dl, it first initializes a `YoutubeDL` class, and then calls it's `download()` method with the list of URLs provided. At that point, youtube-dl checks the URL against the list of extractors it has to see if there is an extractor for it. If no such extractor exists for a URL, it is skipped.

[YoutubeDL.download()](https://github.com/blackjack4494/yt-dlc/blob/93201d50aa5e0bce0ecf6941cd6f9ea6c9ef86a5/youtube_dlc/YoutubeDL.py#L2107-L2130) on Github

With that out of the way, we now turn our attention to the `YoutubeDL` parameters.

## Parameters

Here I will cover some of those command-line options that correspond to `YoutubeDL` the parameters dictionary `params`. They are *not* keyword arguments (`**kwargs`). The names below references the class parameters and may not have the same names as the corresponding command-line options.

`verbose`, `quiet`, `no_warnings`, `simulate`: These all do what their corresponding command-line arguments imply.

### `username` and `password`

This is the username and password to login to the website if necessary.

### `videopassword`

Some sites require a password to access a video, such as if it's password-protected.

### `playliststart` and `playlistend`

Defines the starting and ending index of the playlist. Downloads all the videos between the indices.

Alternatively use `playlist_items` to download specific indices of videos from the playlist.

### `playlistreverse` and `playlistrandom`

Download videos in a playlist in reverse or random order.

### `matchtitle`, `rejecttitle`, `minviews`, `maxviews`

Filter videos based on titles or view numbers.

### `format` and `listformats`

The first parameter is responsible for downloading the video format you specify. The second one prints detailed information about the valid format numbers along with their resolution sizes and file extensions, then quits.

### `cookie`

youtube-dl saves the login information and the rest of the cookie data that Youtube (or any other site) sets, into a file provided by this parameter. When the program starts, it also loads cookie data from this file as well.

### `proxy` and `geo_verification_proxy`

The first parameter is the URL of your proxy server that you will use to download videos. The second parameter is a proxy that will be used only to fake a geolocation check.

## Conclusion

There are more parameters that I haven't covered and I couldn't write about all of them without sounding monotonic. But hopefully you get the general idea of the YoutubeDL constructor, whose source code is located [here](https://github.com/blackjack4494/yt-dlc/blob/93201d50aa5e0bce0ecf6941cd6f9ea6c9ef86a5/youtube_dlc/YoutubeDL.py#L117) and [here](https://github.com/blackjack4494/yt-dlc/blob/93201d50aa5e0bce0ecf6941cd6f9ea6c9ef86a5/youtube_dlc/YoutubeDL.py#L347).