Parth KabraCLI and Terminal

Youtube DL Usage

June 2021 · Learn how to use youtube-dl

Links: Installation, Options, Output Template, AtomicParsley

Download audio and video

This guide aims to document the usage of youtube-dl in a simplified manner. Every section explains a flag for the CLI and provides examples.

Video

Running youtube-dl dQw4w9WgXcQ will download the video, but we want to specify the output file name and location. We can specify the output using the --output (-o) flag:

youtube-dl dQw4w9WgXcQ -o '~/Downloads/%(title)s.%(ext)s'

This will

  1. Download the video to my Downloads/ directory and
  2. Set the file name to be the <video title>.mp3.

Video format

Pass in the --format (-f) flag to choose choose the format:

  • Pass in either 3gp, aac, flv, m4a, mp3, mp4, ogg, wav, or webm
  • Pass best to select the best quality format represented by a single file with video and audio
  • Pass bestvideo to select the best quality video-only format

See “Format Selection” in the docs for more information.

Audio

Add the flag --extract-audio (-x).

Audio format

We can also specify a format with the flag --audio-format.

  • Pass in aac, flac, mp3, m4a, opus, vorbis, or wav
  • Or pass bestaudio to select the best audio-only format

Merging the best audio and video and more examples

Use -f bestvideo+bestaudio to download the best video-only format and the best audio-only format and merge them with ffmpeg or avonv.

youtube-dl dQw4w9WgXcQ -o '~/Downloads/%(title)s.%(ext)s' -f bestvideo+bestaudio

Note that the default format for downloading videos is -f bestvideo+bestaudio/best. If ffmpeg/avonv are available, it will download then merge the bestvideo and bestaudio. If not, it will download best.

See more examples in the official documentation.

Output file name

Variables from “Output Template” can be passed in the --output to customize the output file name/location.

To use them, wrap the variable name in %( and )s, for instance -o ~/%(creator)s/%(license)s/%(title)s.%(ext)s.

Metadata

Subtitles

Pass --write-sub to download the subtitles as an additional file.

Additionally, pass in --embed-subs to embed the subtitles into the video file. Note that this is only supported for mp4, webm and mkv videos.

Use --sub-format with --write-sub to specify the subtitle format: ass, srt, vtt, or best.