On this page

I have an habit of downloading long Youtube videos and watching them locally.

I wrote a script that does it for me and want to share it with you.

yt-dlp -i -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' -o '%(title)s.%(ext)s' https://www.youtube.com/watch?v=dQw4w9WgXcQ

It uses yt-dlp, a command-line tool for downloading YouTube videos (duh). I'm on a Mac, so I installed it via Homebrew.

brew install yt-dlp

Here are the installation instructions for other systems.

Now, let's break down the rest of the command:

  • -i: This flag stands for "ignore errors". If the download fails, yt-dlp will continue running instead of stopping.
  • -f: This option specifies the format of the video and audio streams to be downloaded. The format string provided ('bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best') can be broken down as follows:
    • bestvideo[ext=mp4]: This selects the best video stream available with the file extension .mp4.
    • +: This indicates that the best audio stream should be combined with the best video stream.
    • bestaudio[ext=m4a]: This selects the best audio stream available with the file extension .m4a.
    • /: If the best video and audio options are unavailable, fallback options are specified.
    • best[ext=mp4]: This option downloads the best available format, specifically in .mp4.
    • /best: This obtains the best available format regardless of the extension if none of the previous formats are available.

-o: This flag is used to specify the output filename format.

'%(title)s.%(ext)s': This will save the downloaded file where %(title)s is replaced with the title of the video and %(ext)s is replaced with the file extension of the downloaded format.

https://www.youtube.com/watch?v=dQw4w9WgXcQ: This is the URL of the YouTube video you want to download.

To sum it up, this command downloads the specified YouTube video with the best quality video and audio formats available (preferably in MP4 and M4A) while ignoring any errors during the process, and saves it using the video's title as the filename.