How to Download youtube Videos using youtube-dl
YouTube is a popular video-sharing platform that millions of people use every day. Sometimes, you may want to download a video from YouTube for offline viewing, save it for later, or share it with someone who doesn't have an internet connection. YouTube itself doesn't provide an easy way to download videos, but there are several third-party tools that you can use to do so. Two of the most popular tools for downloading YouTube videos are youtube-dl and yt-dlp.
What is youtube-dl?
youtube-dl is a command-line program for downloading videos from YouTube and other video-sharing websites. It's a free and open-source tool that runs on Windows, macOS, and Linux. It's powerful and can download videos in various formats and resolutions, including 4K and 8K.
Installing youtube-dl
Dependencies
youtube-dl requires ffmpeg
or `avconv to be installed on your system to download and process videos. These multimedia frameworks allow youtube-dl to convert videos to different formats and resolutions. If you don't have ffmpeg or avconv installed, you'll see an error message when downloading a video.
Jump to Installing Dependencies for a detailed guide.
Windows
Download the youtube-dl executable from the official website.
Save the executable file in a directory on your computer, such as
C:\Program Files\youtube-dl
.Open a command prompt in the directory where you saved the executable file.
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
macOS
Install Homebrew if you don't already have it. Open a Terminal window and type the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is installed, run the following command to install youtube-dl:
brew install youtube-dl
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
Linux
Debian/Ubuntu-based distributions
Open a Terminal window.
Run the following command to update your system's package index:
sudo apt update
Run the following command to install youtube-dl:
sudo apt install youtube-dl
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
Fedora/RHEL-based distributions
Open a Terminal window.
Run the following command to install youtube-dl:
sudo dnf install youtube-dl
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
Arch-based distributions
Open a Terminal window.
Run the following command to install youtube-dl:
sudo pacman -S youtube-dl
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
Other distributions (Universal Method)
If none of the above methods work, you can use python to install youtube-dl on any of the operaing systems (either Linux or Windows, or macOS). On both Linux and MacOS, python is already installed. You can use pip to install youtube-dl.
Open a Terminal window.
Run the following command to install youtube-dl using the Python package manager, pip:
On Windows:
pip install youtube-dl
On Linux/MacOS:
pip3 install youtube-dl
Type the following command to verify that youtube-dl is working:
youtube-dl --version
.
If you encounter any errors Vist the youtube-dl Website
Installing yt-dlp
On Windows
Download the yt-dlp executable from the official website.
Save the executable file in a directory on your computer, such as
C:\Program Files\yt-dlp
.Open a command prompt in the directory where you saved the executable file.
Type the following command to verify that yt-dlp is working:
yt-dlp --version
.
Or using winget
:
winget install yt-dlp
On macOS/Linux
The installation method of yt-dlp
is similar to youtube-dl
. Just replace youtube-dl
with yt-dlp
:
pip3 install yt-dlp
Debian/Ubuntu
sudo apt install yt-dlp
MacOS
brew install yt-dlp
Arch/Manjaro
sudo pacman -S yt-dlp
Fedora/RHEL
sudo dnf install yt-dl
For more detailed instructions visit the yt-dlp installation guide.
Installing dependencies for youtube-dl and yt-dlp
As mentioned earlier, youtube-dl and yt-dlp both require ffmpeg or avconv to be installed on your system to download and process videos. These are multimedia frameworks that allow the tools to convert videos to different formats and resolutions.
To install ffmpeg, follow the instructions for your operating system:
macOS: You can install ffmpeg using Homebrew. Run the following command in the Terminal:
brew install ffmpeg
Windows: Download the latest version of ffmpeg from the official website and install it.
Linux: You can install ffmpeg using your distribution's package manager. For example, on Ubuntu or Debian, run the following command in the terminal:
sudo apt install ffmpeg
USAGE
Basic Usage
To download a video from YouTube using youtube-dl or yt-dlp, follow these steps:
Copy the URL of the YouTube video you want to download.
Open a command prompt or terminal window.
Type the following command and replace
<URL>
with the URL of the video you want to download:youtube-dl <URL>
or
yt-dlp <URL>
Press Enter. The video will begin downloading to your current directory.
Downloading Audio Only (MP3)
To download only the audio of a YouTube video as an MP3 file using youtube-dl or yt-dlp, follow these steps:
Copy the URL of the YouTube video you want to download.
Open a command prompt or terminal window.
Type the following command and replace
<URL>
with the URL of the video you want to download:youtube-dl -x --audio-format mp3 <URL>
or
yt-dlp -x --audio-format mp3 <URL>
-x
is the shorthand for--extract-audio
Press Enter. The audiimage.pngo will begin downloading to your current directory as an MP3 file.
Downloading Specific Formats
To download a video in a specific format using youtube-dl or yt-dlp, follow these steps:
Copy the URL of the YouTube video you want to download.
Open a command prompt or terminal window.
Type the following command and replace
<URL>
with the URL of the video you want to download:youtube-dl -f <format code> <URL>
or
yt-dlp -f <format code> <URL>
Press Enter. The video will begin downloading to your current directory in the specified format.
To view a list of available formats for a video, use the following command:
youtube-dl -F <URL>
or
yt-dlp -F <URL>
-F
is the shorthand for--list-formats
This will display a list of all available formats for the video, along with their format codes. To download a specific format, simply use its format code in the command above.
Downloading a Playlist
To download an entire YouTube playlist using youtube-dl or yt-dlp, follow these steps:
Copy the URL of the YouTube playlist you want to download.
Open a command prompt or terminal window.
Type the following command and replace
<URL>
with the URL of the playlist you want to download:youtube-dl -f <format code> -o '%(playlist_index)s - %(title)s.%(ext)s' <URL>
or
yt-dlp -f <format code> -o '%(playlist_index)s - %(title)s.%(ext)s' <URL>
Press Enter. The playlist will begin downloading to your current directory.
Downloading with Custom Filename
By default, youtube-dl and yt-dlp will save downloaded videos with a generic filename. To specify a custom filename, use the -o flag followed by the desired filename template. The template can include various placeholders such as video title, video ID, etc. For example, to save a video with the title "My Video" as "my_video.mp4", use the following command:
youtube-dl -o 'my_video.%(ext)s' <URL>
or
yt-dlp -o 'my_video.%(ext)s' <URL>
This will save the video with the filename "my_video.mp4". You can use various placeholders to customize the filename as desired.
Other Options
Both youtube-dl and yt-dlp offer a wide range of additional options and features. To view the full list of options, use the following commands:
youtube-dl --help
or
yt-dlp --help
This will display a list of all available options and their descriptions. You can use these options to customize your downloads further or to enable additional features.
One most common option that I often use is to embed thumnails when downloading audio files from youtube.com. You can use the following commands:
yt-dlp --extract-audio --audio-format mp3 --embed-thumbnail <URL>
--embed-thumbnail
flag will automatically download and embed the thumbnail of the video to the download audio file.
Note: You need ffmpeg
for most of the operations like, converting videos to audio, format convertion(mkv to mp4), or embeding thumbnails, etc. So make sure you have installed ffmpeg.
Conclusion
youtube-dl and yt-dlp are powerful tools for downloading videos and audio from YouTube and many other video-sharing sites. With their simple command-line interfaces and extensive feature sets, they offer a flexible and efficient way to download media for offline viewing. By following the instructions and examples above, you should be able to get started with using these tools to download videos and audio in a variety of formats.
About Me
If you liked the blog, Star it on Github and give it a follow on Github.