How to use terminal to archive your favorite Youtube videos on macOS

Safari YouTube on Mac
Safari YouTube on Mac (Image credit: iMore)

Have you ever had issues trying to watch a YouTube video where you have spotty Wi-Fi coverage? Or worse, you've wanted to watch a 4k video but your download speeds are too slow? Perhaps you're the type of person who frets at watching the race between the YouTube buffer and the playback indicator. Whatever your reason, you can mitigate those issues by locally storing your favorite YouTube videos for personal use using the terminal on macOS. Here's how!

What is youtube-dl

You can archive YouTube videos by using youtube-dl. Youtube-dl (the dl stands for "download") is open source software that's freely available on a variety of platforms including macOS. It's a command line utility that runs in the macOS terminal application.

Installing youtube-dl

There are a number of ways to have youtube-dl installed on your Mac.

  1. You can download the source code and build it using a compiler.
  2. Or, you can install Homebrew as a package manager and then subsequently install the youtube-dl executable.
  3. Finally, you can install the pre-built application directly with the following commands in terminal.
    1. sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
    2. sudo chmod a+rx /usr/local/bin/youtube-dl

Using youtube-dl

Using youtube-dl boils down to copying the URL of the video you want to archive and appending it to the youtube-dl command in the terminal. However, there are some nifty options that you can use to make certain you get the video quality level that you want.

For example, if you simply wanted to download and watch a video, do the following.

  1. Start terminal.
  2. Type in youtube-dl URL_OF_VIDEO.
  3. Hit return.
  4. Done.

The application will download the default "best" version that contains both video and audio. You can then use your video player of choice to play the downloaded file.

However, if you wanted to download a specific resolution, just download the video portion, or if you simply wanted an audio-only portion to download, here's how you do it.

  1. Start terminal.
  2. Type in youtube-dl -F URL_OF_VIDEO.
  3. Hit return.

You'll now be presented with a list of formats you can choose from.

Some format codes include both audio and video — some just video and some just audio. Let's say for example I wanted to download the 4k version of my chosen video. In my list of formats, I can see the format code 313 is the 4k video file with no audio. In order to download that specific format, type the following in terminal.

  1. youtube-dl -f 313 URL_OF_VIDEO.

You'll now have downloaded the 4k video only portion. You'll have to then download the audio separately. In my example, the audio-only version I want is format code 251. So I'd type the following in terminal.

  1. youtube-dl -f 251 URL_OF_VIDEO -o AUDIO_ONLY.output.

This downloads the audio-only portion that I chose and renames the file to AUDIO_ONLY.output so to not overwrite my downloaded video file.

In order for me to play the two files simultaneously, I'd need a compatible media player that can mux the two files synchronously. You can use something like VLC. I stick to the command line and use mplayer instead. Continuing with my example, with mplayer installed, I'd type the following in terminal.

  1. mplayer -lavdopts threads=6 -audiofile AUDIO_ONLY.output DOWNLOADED_VIDEO_FILE_NAME.

Then I can enjoy my locally stored video and audio without the need for a good (or any) internet connection. if you need any more information on how to use youtube-dl you can read the youtube-dl manual in the terminal by typing in man youtube-dl.

Final comments

Have you ever wished that you had a local copy of a YouTube video? Do you have other suggestions as to how to ameliorate your YouTube viewing experience? Let us know in the comments!

Anthony Casella