Downloading YouTube Videos with Youtube-dl: A Comprehensive Guide
YouTube is a vast repository of video content, ranging from educational tutorials and documentaries to music videos and entertainment clips. While YouTube offers offline viewing for its Premium subscribers on mobile devices, it doesn’t provide a straightforward way to download videos directly to your computer for permanent access or archival purposes. This is where youtube-dl, a powerful command-line tool, comes in handy.
This comprehensive guide will walk you through the process of installing and using youtube-dl to download videos from YouTube and other supported websites. We’ll cover installation on various operating systems, basic usage, advanced options, and troubleshooting common issues.
What is Youtube-dl?
Youtube-dl is a free and open-source command-line program for downloading videos from YouTube and hundreds of other video-sharing websites. It’s written in Python and supports a wide range of features, including:
* **Downloading videos in various formats and resolutions:** You can choose the specific video quality and file format you want to download.
* **Downloading entire playlists and channels:** This is incredibly useful for archiving complete series or collections of videos.
* **Downloading audio only:** Extract audio tracks from videos as MP3, AAC, or other audio formats.
* **Resuming interrupted downloads:** If your download gets interrupted, youtube-dl can resume it from where it left off.
* **Using proxies and authentication:** Download videos through a proxy server or with specific user credentials.
* **Downloading subtitles:** Download subtitles along with the video in various formats.
Why Use Youtube-dl?
There are several reasons why youtube-dl is a popular choice for downloading YouTube videos:
* **Versatility:** Supports a vast number of websites beyond just YouTube.
* **Flexibility:** Offers granular control over download options, allowing you to customize the process to your specific needs.
* **Command-line Interface:** While it requires some familiarity with the command line, it enables automation and scripting of download tasks.
* **Open-Source and Free:** It’s free to use and modify, and its source code is publicly available.
* **No Ads or Bundled Software:** Unlike many downloaders, youtube-dl is a clean and simple tool without any bundled adware or malware.
Disclaimer
Before we proceed, it’s crucial to understand the legal and ethical considerations of downloading videos from YouTube. Always respect copyright laws and the terms of service of YouTube and other websites. Download videos only for personal use, fair use, or with the explicit permission of the copyright holder. Downloading copyrighted content for commercial purposes or distribution without authorization is illegal.
Installation
The installation process varies slightly depending on your operating system.
Windows
1. **Download the Executable:**
* Go to the official youtube-dl website or a trusted source like GitHub releases.
* Download the `youtube-dl.exe` file. Look for the latest release under the “Releases” section. Avoid downloading from untrusted third-party websites.
2. **Place the Executable in a Directory:**
* Create a new folder where you want to store youtube-dl (e.g., `C:\youtube-dl`).
* Move the `youtube-dl.exe` file into this folder.
3. **Add the Directory to Your System PATH (Recommended):**
* This allows you to run youtube-dl from any command prompt window without having to navigate to its directory.
* **Search for “Environment Variables”** in the Windows search bar and open “Edit the system environment variables”.
* Click on “Environment Variables…”.
* Under “System variables”, find the variable named “Path” and select it.
* Click “Edit…”.
* Click “New” and add the path to the directory where you placed `youtube-dl.exe` (e.g., `C:\youtube-dl`).
* Click “OK” on all the dialog boxes to save the changes.
4. **Verify the Installation:**
* Open a new Command Prompt window (search for “cmd” in the Windows search bar).
* Type `youtube-dl –version` and press Enter.
* If youtube-dl is installed correctly, it will display the current version number. If you receive an error, double-check that you added the directory to your PATH correctly and that the Command Prompt window was opened *after* you made the changes.
macOS
The recommended way to install youtube-dl on macOS is using Homebrew, a package manager for macOS.
1. **Install Homebrew (if you don’t have it already):**
* Open Terminal (found in `/Applications/Utilities/`).
* Paste the following command and press Enter:
bash
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
* Follow the on-screen instructions to complete the Homebrew installation.
2. **Install youtube-dl:**
* Open Terminal.
* Type `brew install youtube-dl` and press Enter.
* Homebrew will download and install youtube-dl and its dependencies.
3. **Verify the Installation:**
* Open Terminal.
* Type `youtube-dl –version` and press Enter.
* If youtube-dl is installed correctly, it will display the current version number.
Linux
Youtube-dl is often available in the package repositories of many Linux distributions. The exact command for installation may vary depending on your distribution.
1. **Debian/Ubuntu:**
* Open Terminal.
* Type `sudo apt update` and press Enter to update the package list.
* Type `sudo apt install youtube-dl` and press Enter.
2. **Fedora/CentOS/RHEL:**
* Open Terminal.
* Type `sudo dnf install youtube-dl` and press Enter.
3. **Arch Linux:**
* Open Terminal.
* Type `sudo pacman -S youtube-dl` and press Enter.
4. **Verify the Installation:**
* Open Terminal.
* Type `youtube-dl –version` and press Enter.
* If youtube-dl is installed correctly, it will display the current version number.
**Note:** For some Linux distributions, the version of youtube-dl in the repositories might be outdated. In such cases, consider using `pip` (Python package installer) to install the latest version. See the next section for instructions on installing with pip.
Installing with pip (Python Package Installer)
If you have Python installed, you can use `pip` to install youtube-dl. This method is platform-independent and usually provides the most up-to-date version.
1. **Make sure Python and pip are installed:**
* Open Terminal (or Command Prompt on Windows).
* Type `python3 –version` or `python –version` and press Enter. If Python is installed, it will display the version number.
* Type `pip3 –version` or `pip –version` and press Enter. If pip is installed, it will display the version number.
* If Python or pip are not installed, you’ll need to install them first. Instructions for installing Python can be found on the official Python website (python.org).
2. **Install youtube-dl:**
* Open Terminal (or Command Prompt on Windows).
* Type `pip3 install youtube-dl` or `pip install youtube-dl` and press Enter. Using `pip3` is recommended if you have both Python 2 and Python 3 installed.
3. **Verify the Installation:**
* Open Terminal (or Command Prompt on Windows).
* Type `youtube-dl –version` and press Enter.
* If youtube-dl is installed correctly, it will display the current version number.
Basic Usage
The basic syntax for using youtube-dl is:
bash
youtube-dl [options] [URL]
* `youtube-dl`: The command to run the program.
* `[options]`: Optional parameters to customize the download (we’ll cover these later).
* `[URL]`: The URL of the YouTube video or playlist you want to download.
**Example:**
To download a single YouTube video, simply paste the video’s URL after the `youtube-dl` command:
bash
youtube-dl https://www.youtube.com/watch?v=dQw4w9WgXcQ
This will download the video in the best available quality to your current working directory. You’ll see progress information displayed in the terminal.
Common Options
Youtube-dl offers a wide array of options to customize your downloads. Here are some of the most frequently used options:
* **`-o` or `–output`:** Specifies the output filename and directory.
bash
youtube-dl -o “/path/to/downloads/%(title)s.%(ext)s” https://www.youtube.com/watch?v=dQw4w9WgXcQ
This will download the video to the `/path/to/downloads` directory and name it according to the video’s title and extension. The `%(title)s` and `%(ext)s` are placeholders that youtube-dl replaces with the actual video title and file extension. Make sure the directory exists before running the command.
* **`-f` or `–format`:** Specifies the video format to download. You can use `youtube-dl -F [URL]` to list the available formats for a specific video.
bash
youtube-dl -F https://www.youtube.com/watch?v=dQw4w9WgXcQ
The output will show a list of formats with corresponding codes. For example:
[youtube] dQw4w9WgXcQ: Downloading webpage
[info] Available formats for dQw4w9WgXcQ:
format code extension resolution note
249 webm audio only tiny 49k , opus @ 50k (48kHz), 3.97MiB
250 webm audio only tiny 63k , opus @ 70k (48kHz), 5.01MiB
251 webm audio only tiny 126k , opus @160k (48kHz), 9.91MiB
140 m4a audio only tiny 128k , m4a_dash container, mp4a.40.2@128k (44100Hz), 10.10MiB
278 webm 144p 111k , webm container, vp9@111k, 30fps, video only, 2.48MiB
160 mp4 144p 111k , mp4 container, avc1.4d400c@111k, 30fps, video only, 2.52MiB
242 webm 240p 227k , webm container, vp9@227k, 30fps, video only, 4.84MiB
133 mp4 240p 245k , mp4 container, avc1.4d4015@245k, 30fps, video only, 5.22MiB
243 webm 360p 431k , webm container, vp9@431k, 30fps, video only, 9.17MiB
134 mp4 360p 582k , mp4 container, avc1.4d401e@582k, 30fps, video only, 12.38MiB
244 webm 480p 798k , webm container, vp9@798k, 30fps, video only, 16.98MiB
135 mp4 480p 1124k , mp4 container, avc1.4d4028@1124k, 30fps, video only, 23.88MiB
247 webm 720p 1490k , webm container, vp9@1490k, 30fps, video only, 31.78MiB
136 mp4 720p 2411k , mp4 container, avc1.4d4032@2411k, 30fps, video only, 51.27MiB
248 webm 1080p 2677k , webm container, vp9@2677k, 30fps, video only, 57.03MiB
137 mp4 1080p 4587k , mp4 container, avc1.640028@4587k, 30fps, video only, 97.67MiB
271 webm 1440p 5648k , webm container, vp9@5648k, 30fps, video only, 120.29MiB
313 webm 2160p 14675k , webm container, vp9@14675k, 30fps, video only, 312.94MiB
18 mp4 360p 640k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 13.68MiB
22 mp4 720p 1280k , avc1.64001F, mp4a.40.2@192k (44100Hz), 27.45MiB
43 webm 360p 500k , vp8.0, vorbis@128k (44100Hz), 11.12MiB
Choose the format code you want and use it with the `-f` option:
bash
youtube-dl -f 137 https://www.youtube.com/watch?v=dQw4w9WgXcQ
This will download the 1080p MP4 video (format code 137). Note that you might need to install `ffmpeg` if you want to merge separate audio and video streams. (See the “Combining Audio and Video” section below).
* **`-w` or `–no-overwrites`:** Prevents overwriting existing files with the same name. If a file with the same name already exists, youtube-dl will skip downloading the video.
* **`-c` or `–continue`:** Resumes partially downloaded files. If a download is interrupted, youtube-dl will attempt to resume it from where it left off.
* **`–playlist-start` and `–playlist-end`:** Specify the start and end index of videos to download from a playlist.
bash
youtube-dl –playlist-start 5 –playlist-end 10 https://www.youtube.com/playlist?list=PLxxxxxxxxx
This will download videos from index 5 to 10 (inclusive) from the specified playlist.
* **`–username` and `–password`:** Provide login credentials for websites that require authentication.
bash
youtube-dl –username myusername –password mypassword https://example.com/protected-video
**Warning:** Storing passwords directly in the command line can be insecure. Consider using environment variables or a configuration file for storing sensitive information.
* **`–proxy`:** Specifies a proxy server to use for downloading.
bash
youtube-dl –proxy socks5://127.0.0.1:1080 https://www.youtube.com/watch?v=dQw4w9WgXcQ
This will download the video through a SOCKS5 proxy running on localhost port 1080.
* **`–cookies-from-browser`**: Downloads cookies from your browser for authentication purposes. You need to specify which browser you want to use. Useful for websites that require login.
bash
youtube-dl –cookies-from-browser chrome https://example.com/protected-video
* **`–write-sub` or `–write-srt`:** Downloads subtitles in `.srt` format (or other supported formats). `–all-subs` downloads all available subtitles.
bash
youtube-dl –write-srt –all-subs https://www.youtube.com/watch?v=dQw4w9WgXcQ
* **`–audio-format` and `–extract-audio`:** Downloads audio from video in specified format.
bash
youtube-dl –extract-audio –audio-format mp3 https://www.youtube.com/watch?v=dQw4w9WgXcQ
This downloads the audio track of the specified video in MP3 format.
Downloading Playlists and Channels
Youtube-dl makes it easy to download entire playlists or channels.
* **Downloading a Playlist:**
Simply provide the URL of the playlist:
bash
youtube-dl https://www.youtube.com/playlist?list=PLxxxxxxxxx
Youtube-dl will download all videos in the playlist sequentially.
* **Downloading a Channel:**
Provide the URL of the channel:
bash
youtube-dl https://www.youtube.com/channel/UCxxxxxxxxxx
Or the URL of a user’s videos page:
bash
youtube-dl https://www.youtube.com/user/username/videos
Youtube-dl will download all publicly available videos from the channel.
Combining Audio and Video (FFmpeg)
Sometimes, the best quality video and audio streams are available as separate files. In these cases, you’ll need to combine them into a single video file using a tool like FFmpeg.
1. **Install FFmpeg:**
* **Windows:** Download FFmpeg from the official website (ffmpeg.org) and follow the installation instructions. Make sure to add the FFmpeg `bin` directory to your system PATH.
* **macOS:** Use Homebrew: `brew install ffmpeg`
* **Linux:** Use your distribution’s package manager: `sudo apt install ffmpeg` (Debian/Ubuntu), `sudo dnf install ffmpeg` (Fedora/CentOS), `sudo pacman -S ffmpeg` (Arch Linux).
2. **Download the separate audio and video streams:**
First list the available formats:
bash
youtube-dl -F https://www.youtube.com/watch?v=dQw4w9WgXcQ
Then download the audio and video separately using their format codes:
bash
youtube-dl -f 137+140 https://www.youtube.com/watch?v=dQw4w9WgXcQ
This example downloads the 1080p video (format 137) and the best audio (format 140), and youtube-dl will automatically merge them using FFmpeg, assuming FFmpeg is correctly installed and in your system PATH.
If youtube-dl doesn’t automatically merge, you can do it manually using FFmpeg:
bash
ffmpeg -i video.mp4 -i audio.m4a -c copy output.mp4
Replace `video.mp4` and `audio.m4a` with the actual filenames of the downloaded video and audio files, and `output.mp4` with the desired name for the merged video.
Configuration File
For frequently used options, it’s convenient to store them in a configuration file instead of typing them every time. Youtube-dl looks for a configuration file in the following locations (in order):
* `~/.config/youtube-dl/config` (Linux, macOS)
* `%APPDATA%\youtube-dl\config.txt` (Windows)
Create a text file named `config` (or `config.txt` on Windows) in the appropriate directory and add your options, one per line:
–output “/path/to/downloads/%(title)s.%(ext)s”
–continue
–no-overwrites
–extract-audio
–audio-format mp3
Now, when you run youtube-dl, it will automatically use these options.
bash
youtube-dl https://www.youtube.com/watch?v=dQw4w9WgXcQ
Troubleshooting
Here are some common issues and solutions:
* **”youtube-dl: command not found” or similar error:**
* Make sure youtube-dl is installed correctly and the directory containing the executable is in your system PATH.
* If you installed with pip, try running `python3 -m youtube_dl –version` or `python -m youtube_dl –version`.
* **”ERROR: Unable to extract video URL” or similar error:**
* The video might be private or age-restricted. Try logging in with your YouTube account using the `–username` and `–password` options or the `–cookies-from-browser` option.
* The website’s structure might have changed. Try updating youtube-dl to the latest version using `youtube-dl -U` or `pip install –upgrade youtube_dl`.
* The video might not be supported by youtube-dl. Check the list of supported websites on the youtube-dl website.
* **Download is slow:**
* Your internet connection might be slow.
* Try using a proxy server with the `–proxy` option.
* The website might be throttling your download speed.
* **Videos are downloaded in separate audio and video files, and they don’t merge automatically:**
* Make sure FFmpeg is installed correctly and in your system PATH.
* Try downloading a format that contains both audio and video in a single stream (check the output of `youtube-dl -F [URL]`).
* Manually merge the audio and video files using FFmpeg (see the “Combining Audio and Video” section).
* **Youtube-dl stops working or gives errors after a while:**
* YouTube and other video platforms frequently change their website structures. This often requires updates to youtube-dl. Always keep youtube-dl updated using `youtube-dl -U` or `pip install –upgrade youtube_dl`.
Keeping Youtube-dl Updated
It’s essential to keep youtube-dl updated to ensure it works correctly with the latest website changes. Use the following command to update youtube-dl:
bash
youtube-dl -U
Or if you installed with pip:
bash
pip install –upgrade youtube_dl
Run this command regularly to stay up-to-date.
Conclusion
Youtube-dl is a powerful and versatile tool for downloading videos from YouTube and other websites. With its wide range of options and its ability to download playlists, channels, and audio-only files, it provides a flexible solution for archiving and accessing video content offline. By following this guide, you should be well-equipped to use youtube-dl effectively and troubleshoot common issues. Remember to always respect copyright laws and the terms of service of the websites you are downloading from. Enjoy downloading!