youtube-dl - Download videos from YouTube (and more sites)

How to download an album / playlist from YouTube with the best possible audio quality (no format conversion) ?

For an optimal (bug-free ) experience, make sure youtube-dl is up-to-date.

#!/bin/bash
playlist='https://www.youtube.com/playlist?list=PL4A181D94881BD304'
audioformatString='vorbis@128k'
youtubeBaseUrl='https://www.youtube.com/watch?v='
getVideosIdFromPlaylist() {
	playlist="$1"
#	youtube-dl --get-id --max-downloads 3 "$playlist"	for dev/debug only
	youtube-dl --get-id "$playlist"
	}
getFormatCodeOfVideoHavingBestAudioQuality() {
	youtubeVideoId="$1"
	youtube-dl -F "$youtubeBaseUrl$youtubeVideoId" | awk -v needle='audio only.*'"$audioformatString" '$0 ~ needle { print $1 }'
	}
downloadYoutubeTrack() {
	formatCode="$1"
	youtubeVideoId="$2"
	youtube-dl -f "$formatCode" -o "%(playlist)s/%(playlist_index)s - %(track_number)s - %(title)s.%(ext)s" "$youtubeBaseUrl$youtubeVideoId"
	}
for youtubeVideoId in $(getVideosIdFromPlaylist "$playlist"); do
	formatCodeOfVideoHavingBestAudioQuality=$(getFormatCodeOfVideoHavingBestAudioQuality "$youtubeVideoId")
	downloadYoutubeTrack "$formatCodeOfVideoHavingBestAudioQuality" "$youtubeVideoId"
done
As a big fat one-liner :
playlist='https://www.youtube.com/watch?v=RKmw9oS__MM&list=RDRKmw9oS__MM'; audioformatString='opus @160k'; youtubeBaseUrl='https://www.youtube.com/watch?v='; for youtubeVideoId in $(youtube-dl --get-id "$playlist"); do formatCode=$(youtube-dl -F "$youtubeBaseUrl$youtubeVideoId" | awk -v needle='audio only.*'"$audioformatString" '$0 ~ needle { print $1 }'); youtube-dl -f "$formatCode" -o "%(playlist)s/%(playlist_index)s - %(track_number)s - %(title)s.%(ext)s" "$youtubeBaseUrl$youtubeVideoId"; done
Alternate method :
1. download all streams marked as 'best quality' (mostly HD videos) :
playlist='https://www.youtube.com/watch?v=Rl-VzB3PhTo&list=RDd27mV60APRo'; audioformatString='(best)'; youtubeBaseUrl='https://www.youtube.com/watch?v='; for youtubeVideoId in $(youtube-dl --get-id "$playlist"); do formatCode=$(youtube-dl -F "$youtubeBaseUrl$youtubeVideoId" | awk -v needle="$audioformatString" '$0 ~ needle { print $1 }'); youtube-dl -f "$formatCode" -o "%(playlist)s/%(playlist_index)s - %(track_number)s - %(title)s.%(ext)s" "$youtubeBaseUrl$youtubeVideoId"; done
2. extract audio from all those videos :

How to download a YouTube playlist and convert it to MP3 ?

This method works fine, BUT :
  • I can't be sure it picks the audio track having the best quality
  • it re-encodes the audio, meaning :
    • degrading again the audio quality
    • useless CPU usage + delay
    • generating artificial "high bitrate" MP3 files (i.e. more MB, same/lower quality)
Consider downloading without format conversion.
youtube-dl --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" https://www.youtube.com/playlist?list=PLE0F4727AA947BBB9
Will output files named after : youtubeVideoTitle.mp3
youtube-dl --extract-audio --audio-format mp3 -o "%(playlist_index)s - %(title)s.%(ext)s" https://www.youtube.com/playlist?list=PL2E1BB7AF0B48892D
Will output files named after : trackNumber - youtubeVideoTitle.mp3

and find the command flag to specify the MP3 bitrate. Defaults to 64kb/s.

Output format specifiers are from the OUTPUT TEMPLATE section of the man page. Don't forget the closing s : %(formatSpecifier)s

If this fails saying :

ERROR: ffprobe or avprobe not found. Please install one.
you'll have to install either ffmpeg or avconv (source) :
apt install libav-tools

How to download a single song / video from YouTube ?

  1. List the available formats :

    youtube-dl -F https://www.youtube.com/watch?v=5pBjopDymts

    [youtube] Setting language
    [youtube] 5pBjopDymts: Downloading webpage
    [youtube] 5pBjopDymts: Downloading video info webpage
    [youtube] 5pBjopDymts: Extracting video information
    [info] Available formats for 5pBjopDymts:
    format code	extension	resolution	note
    171		webm		audio only	DASH audio , audio@ 48k (worst)
    140		m4a		audio only	DASH audio , audio@128k
    160		mp4		144p		DASH video , video only
    ...
    247		webm		720p		DASH video , video only
    136		mp4		720p		DASH video , video only	This is really VIDEO ONLY: no sound !
    248		webm		1080p		DASH video , video only
    ...
    264		mp4		1440p		DASH video , video only
    249		unknown_videounknown
    ...
    313		unknown_videounknown
    17		3gp		176x144
    36		3gp		320x240
    43		webm		640x360
    18		mp4		640x360		This is the version I'd like to download
    22		mp4		1280x720	(best)
  2. Pick the version you like :

    youtube-dl -f 18 https://www.youtube.com/watch?v=5pBjopDymts

    [youtube] Setting language
    [youtube] 5pBjopDymts: Downloading webpage
    [youtube] 5pBjopDymts: Downloading video info webpage
    [youtube] 5pBjopDymts: Extracting video information
    [download] Destination: It's Gonna Be OKAY - ThePianoGuys-5pBjopDymts.mp4
    [download] 100% of 20.70MiB in 00:59

youtube-dl

Usage :

youtube-dl can be installed via the youtube-dl Debian package (read my notes about youtube-dl setup).

youtube-dl is not limited to YouTube and can download videos (and music !) from MANY other sites I've not tried yet myself

Setup :

There are several ways to install youtube-dl :

Flags :

Flag Usage
-a file --batch-file file download URL's listed in file
you can even combine options : youtube-dl -f 22 -a list
-f formatCode --format formatCode download the video having the formatCode format
-F --list-formats list all available formats
-U --update (explicit )
If youtube-dl was installed without the distribution packages (i.e. without apt, yum, ...), it can be updated with (source) :
sudo -H pip install --upgrade youtube-dl