Using youtube-dl (with examples)
youtube-dl is a powerful command-line tool that allows users to download videos and playlists from YouTube and other websites. It provides various options for customizing the download process, such as choosing specific video/audio formats, extracting only the audio, downloading subtitles, and more. In this article, we will explore eight different use cases of youtube-dl with code examples to illustrate how to use it effectively.
Download a video or playlist
youtube-dl 'https://www.youtube.com/watch?v=oHg5SJYRHA0'
Motivation: Sometimes, we may come across an interesting video or playlist on YouTube that we want to watch later or offline. By using youtube-dl, we can easily download the video or entire playlist to our local machine.
Explanation:
youtube-dl
is the command used to invoke the youtube-dl tool.'https://www.youtube.com/watch?v=oHg5SJYRHA0'
is the URL of the video or playlist we want to download.
Example output: The video or playlist will be downloaded and saved in the current directory.
List all formats that a video or playlist is available in
youtube-dl --list-formats 'https://www.youtube.com/watch?v=Mwa0_nE9H7A'
Motivation: Sometimes, we may want to know the available video/audio formats before downloading a video or playlist. This can help us choose the desired format that meets our requirements.
Explanation:
--list-formats
is an option that instructs youtube-dl to list all available formats.'https://www.youtube.com/watch?v=Mwa0_nE9H7A'
is the URL of the video or playlist.
Example output: A list of available formats with their respective codes, quality, and file extensions will be displayed.
Download a video or playlist at a specific quality
youtube-dl --format "best[height<=480]" 'https://www.youtube.com/watch?v=oHg5SJYRHA0'
Motivation: To save bandwidth and storage space, we may prefer to download videos or playlists at a specific quality that satisfies our requirements. This option allows us to choose the desired quality level, such as limiting it to a maximum height of 480 pixels.
Explanation:
--format "best[height<=480]"
is an option that specifies the desired quality level using a format specifier. In this example, it limits the height of the video to 480 pixels or less.'https://www.youtube.com/watch?v=oHg5SJYRHA0'
is the URL of the video or playlist.
Example output: The video or playlist will be downloaded at the specified quality level.
Download the audio from a video and convert it to an MP3
youtube-dl -x --audio-format mp3 'https://www.youtube.com/watch?v=url'
Motivation: Sometimes, we may want to extract only the audio from a video and save it in a convenient format like MP3. This option allows us to download the video and automatically convert it to the desired audio format.
Explanation:
-x
is a short option that tells youtube-dl to extract the audio.--audio-format mp3
is an option that specifies the desired audio format, in this case, MP3.'https://www.youtube.com/watch?v=url'
is the URL of the video.
Example output: The audio will be extracted from the video and saved as an MP3 file.
Download the best quality audio and video and merge them
youtube-dl -f bestvideo+bestaudio 'https://www.youtube.com/watch?v=url'
Motivation: In some cases, we may want to download the best quality audio and video separately and later merge them into a single file. This option allows us to achieve that by downloading the best audio and video streams available.
Explanation:
-f bestvideo+bestaudio
is a short option that tells youtube-dl to download the best video and audio streams individually.'https://www.youtube.com/watch?v=url'
is the URL of the video.
Example output: The best quality video and audio will be downloaded separately.
Download video(s) as MP4 files with custom filenames
youtube-dl --format mp4 -o "%(playlist_index)s-%(title)s by %(uploader)s on %(upload_date)s in %(playlist)s.%(ext)s" 'https://www.youtube.com/watch?v=url'
Motivation: When downloading multiple videos or playlists, it can be useful to customize the filenames to include relevant information like the video index, title, uploader, upload date, and playlist name. This option allows us to specify a custom output filename template.
Explanation:
--format mp4
is an option that specifies the desired video format, in this case, MP4.-o "%(playlist_index)s-%(title)s by %(uploader)s on %(upload_date)s in %(playlist)s.%(ext)s"
is an option that defines the custom output filename template. It uses placeholders like%(placeholder)s
to represent different metadata fields.'https://www.youtube.com/watch?v=url'
is the URL of the video or playlist.
Example output: The video(s) will be downloaded as MP4 files with the specified custom filenames.
Download a particular language’s subtitles along with the video
youtube-dl --sub-lang en --write-sub 'https://www.youtube.com/watch?v=Mwa0_nE9H7A'
Motivation: When watching videos in a language that we are not fluent in, having subtitles in our native language can be very helpful. This option allows us to download and save subtitles in a specific language along with the video.
Explanation:
--sub-lang en
is an option that specifies the desired subtitle language, in this case, English (ISO 639-1 language code).--write-sub
is an option that instructs youtube-dl to write the subtitles to a file.'https://www.youtube.com/watch?v=Mwa0_nE9H7A'
is the URL of the video.
Example output: The video and its English subtitles will be downloaded and saved in separate files.
Download a playlist and extract MP3s from it
youtube-dl -f "bestaudio" --continue --no-overwrites --ignore-errors --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" 'url_to_playlist'
Motivation: When dealing with a playlist on YouTube, we may want to extract only the audio files and save them as MP3s for offline listening. This option allows us to download a playlist, extract its audio tracks, and convert them to the desired audio format.
Explanation:
-f "bestaudio"
is a short option that tells youtube-dl to download the best audio stream available.--continue
is an option that resumes the download if it was interrupted.--no-overwrites
is an option that prevents youtube-dl from overwriting any already-existing files.--ignore-errors
is an option that skips any errors encountered during the download process.--extract-audio
is an option that instructs youtube-dl to extract the audio from the video.--audio-format mp3
is an option that specifies the desired audio format, in this case, MP3.-o "%(title)s.%(ext)s"
is an option that defines the output filename template for each downloaded audio file.'url_to_playlist'
is the URL of the playlist.
Example output: The playlist will be downloaded, and its audio tracks will be extracted and saved as MP3 files with their titles as filenames.
Conclusion
youtube-dl offers a wide range of options to suit different download requirements. In this article, we have explored eight powerful use cases of youtube-dl, including downloading videos, extracting audio, downloading specific formats, customizing filenames, and downloading subtitles. By leveraging these examples, users can efficiently utilize youtube-dl to enhance their video downloading experience.