Downloading YouTube videos and playlists with yt-dlp

yt-dlp is a fork from the now troubled youtube-dl, the best part about yt-dlp aside from being actively maintained is that it follows similar commands to youtube-dl.

yt dlp install and usage guide

Installing yt-dlp

Installing yt-dlp can simply be done with:

sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp

Followed by:

sudo chmod a+rx /usr/local/bin/yt-dlp

If you ever need to update yt-dlp use:

yt-dlp -U

Some common usage examples for yt-dlp:

Print out the available formats and information with:

yt-dlp --list-formats  https://www.youtube.com/watch?v=1La4QzGeaaQ

Print JSON information for the video and audio streams:

yt-dlp --dump-json https://www.youtube.com/watch?v=1La4QzGeaaQ

Single video or audio downloads

Download the best format (video + audio) that is equal to or greater than 720p width. Save this file as video_id.extension (1La4QzGeaaQ.mp4):

yt-dlp -f "best[height>=720]" https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.%(ext)s'

Download and merge the best video stream with the best audio stream:

yt-dlp -f 'bv*+ba' https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.%(ext)s'

Download 1080p video and merge with best audio stream:

yt-dlp -f 'bv*[height=1080]+ba' https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.%(ext)s'

Download 1080p video that is mp4 format and merge with best m4a audio format:

yt-dlp -f 'bv[height=1080][ext=mp4]+ba[ext=m4a]' --merge-output-format mp4 https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.mp4'

Embed video thumbnail into video file use --embed-thumbnail:

yt-dlp -f 'bv[height=1080][ext=mp4]+ba[ext=m4a]' --embed-thumbnail --merge-output-format mp4 https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.mp4'

Embed subtitles to video file (if they exist) --embed-subs :

yt-dlp -f 'bv[height=1080][ext=mp4]+ba[ext=m4a]' --embed-subs --merge-output-format mp4 https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.mp4'

Embed metadata about the video --embed-metadata :

yt-dlp -f 'bv[height=1080][ext=mp4]+ba[ext=m4a]' --embed-metadata --merge-output-format mp4 https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.mp4'

Get the best audio into mp3 file:

yt-dlp -f 'ba' -x --audio-format mp3 https://www.youtube.com/watch?v=1La4QzGeaaQ -o '%(id)s.mp3'

All the options for format selection and filtering can be found here, There are a lot.

Playlists

Download a YouTube playlist with the videos being 1080p and the best audio. Save into channel_id/playlist_id directory with the video added to an archive text file:

yt-dlp -f 'bv*[height=1080]+ba' --download-archive videos.txt  https://www.youtube.com/playlist?list=PLlVlyGVtvuVnUjA4d6gHKCSrLAAm2n1e6 -o '%(channel_id)s/%(playlist_id)s/%(id)s.%(ext)s'

Channels

Download an entire YouTube channel as 720p video with best audio. Save into a folder named after the channel name with the video files being the title of the video (Foo the Flowerhorn/5 Months Update – Flowerhorn Foods.webm).

yt-dlp -f 'bv*[height=720]+ba' --download-archive videos.txt https://www.youtube.com/c/FootheFlowerhorn/videos -o '%(channel)s/%(title)s.%(ext)s'