How to download YouTube videos

I have mentioned about youtube-dl several times previously, but never really had a time to explain why I like it so much. Therefore I will describe it now.

Installation

Install youtube-dl package.

$ sudo apt-get install youtube-dl

Notice that there will be times when the application from package repository will temporarily not work due to some YouTube changes, but do not worry as it is constantly updated to reflect such changes.

Getting video information

Print video title, duration and format.

$ youtube-dl --get-title --get-duration --get-format  https://www.youtube.com/watch?v=5D4kSVcELjM
Open Source Awards - OSCON 2015
5:10
22 - 1280x720

Print video title and description.

$ youtube-dl --get-title --get-description  https://www.youtube.com/watch?v=5D4kSVcELjM
Open Source Awards - OSCON 2015
From OSCON 2015 in Portland.

Watch more from OSCON 2015: https://goo.gl/vD6vda
Find out more about OSCON: http://oscon.com/open-source-2015
Don't miss an upload! Subscribe! http://goo.gl/szEauh
Stay Connected to O'Reilly Media by Email - http://goo.gl/YZSWbO

Follow O'Reilly Media:
http://plus.google.com/+oreillymedia
https://www.facebook.com/OReilly
https://twitter.com/OReillyMedia

Filter and print video titles from given playlist.

$ youtube-dl --get-title --match-title "interview" https://www.youtube.com/playlist?list=PL055Epbe6d5YhDchEvY3O4nIuSLYyrx7K 
Interview with Cisco's Ben Schumacher at OSCON 2015
OSCON Interview: Huawei's Peixin Hou
Interview with GitHub's Brent Beer at OSCON 2015
OSCON Interview: HP's Mark Atwood
OSCON Interview: Secret Lab's Paris Buttfield Addison
OSCON Interview: Software Freedom Conservancy's Bradley Kuhn
OSCON Interview: CA Technologies' Mike Amundsen
OSCON Interview: Matrix.org's Matthew Hodgson
OSCON Interview: Cloudera's Ryan Blue
OSCON Interview: Puppet Labs' Kara Sowles
OSCON Interview: Cerner's Michelle Brush
OSCON interview: Microsoft's Scott Hanselman
OSCON Interview: Microsoft's Maria Naggaga
OSCON Interview: LivingSocial's Tom Marrs
OSCON Interview: Two Sigma's David Greenberg
OSCON Interview: Box's Joe Marrama
OSCON Interview: Red Hat's Federico Lucifredi
OSCON interview: Red Hat's Rich Bowen
OSCON Interview: Erlang Solutions' Francesco Cesarini
OSCON interview: Saint Joseph's College's Brian Capouch
OSCON Interview: Jen Kramer
OSCON Interview: Vector's Dave Kelnar

Downloading single video

Download single video.

$ youtube-dl https://www.youtube.com/watch?v=5D4kSVcELjM
[youtube] 5D4kSVcELjM: Downloading webpage
[youtube] 5D4kSVcELjM: Extracting video information
[youtube] 5D4kSVcELjM: Downloading DASH manifest
[download] Destination: Open Source Awards - OSCON 2015-5D4kSVcELjM.mp4
[download] 100% of 52.87MiB in 00:35

Download single video and store its thumbnail.

$ youtube-dl --write-thumbnail https://www.youtube.com/watch?v=5D4kSVcELjM
[youtube] 5D4kSVcELjM: Downloading webpage
[youtube] 5D4kSVcELjM: Extracting video information
[youtube] 5D4kSVcELjM: Downloading DASH manifest
[youtube] 5D4kSVcELjM: Downloading thumbnail ...
[youtube] 5D4kSVcELjM: Writing thumbnail to: Open Source Awards - OSCON 2015-5D4kSVcELjM.jpg
[download] Destination: Open Source Awards - OSCON 2015-5D4kSVcELjM.mp4
[download] 100% of 52.87MiB in 00:35

Download single video and extract audio.

$ youtube-dl --extract-audio --audio-format mp3 https://www.youtube.com/watch?v=5D4kSVcELjM
[youtube] 5D4kSVcELjM: Downloading webpage
[youtube] 5D4kSVcELjM: Extracting video information
[youtube] 5D4kSVcELjM: Downloading DASH manifest
[download] Destination: Open Source Awards - OSCON 2015-5D4kSVcELjM.m4a
[download] 100% of 3.51MiB in 00:01
[ffmpeg] Correcting container in "Open Source Awards - OSCON 2015-5D4kSVcELjM.m4a"
[avconv] Destination: Open Source Awards - OSCON 2015-5D4kSVcELjM.mp3
Deleting original file Open Source Awards - OSCON 2015-5D4kSVcELjM.m4a (pass -k to keep)

Dowloading multiple videos from given playlist

Download every video from playlist and store metadata.

$ youtube-dl --add-metadata https://www.youtube.com/playlist?list=PL055Epbe6d5YhDchEvY3O4nIuSLYyrx7K
[youtube:playlist] PL055Epbe6d5YhDchEvY3O4nIuSLYyrx7K: Downloading webpage
[download] Downloading playlist: OSCON 2015
[youtube:playlist] playlist OSCON 2015: Collected 73 video ids (downloading 73 of them)
[download] Downloading video 1 of 73
[youtube] RNdD9Id7Ae8: Downloading webpage
[youtube] RNdD9Id7Ae8: Extracting video information
[youtube] RNdD9Id7Ae8: Downloading DASH manifest
[download] Destination: Advancing Open Containers Through Pan-Industry Collaboration - Jim Zemlin Keynote-RNdD9Id7Ae8.mp4
[download] 100% of 86.58MiB in 00:59
[ffmpeg] Adding metadata to 'Advancing Open Containers Through Pan-Industry Collaboration - Jim Zemlin Keynote-RNdD9Id7Ae8.mp4'
[...]
Storing metadata inside video files requires atomicparsley package.

Filter videos from given playlist by name and date, then download them.

$ youtube-dl --add-metadata --match-title "interview" --dateafter "20150727"  https://www.youtube.com/playlist?list=PL055Epbe6d5YhDchEvY3O4nIuSLYyrx7K
[youtube:playlist] PL055Epbe6d5YhDchEvY3O4nIuSLYyrx7K: Downloading webpage
[download] Downloading playlist: OSCON 2015
[youtube:playlist] playlist OSCON 2015: Collected 73 video ids (downloading 73 of them)
[download] Downloading video 1 of 73
[youtube] RNdD9Id7Ae8: Downloading webpage
[youtube] RNdD9Id7Ae8: Extracting video information
[youtube] RNdD9Id7Ae8: Downloading DASH manifest
[download] "Advancing Open Containers Through Pan-Industry Collaboration - Jim Zemlin Keynote" title did not match pattern "interview"
[...]
[download] Downloading video 22 of 73
[youtube] lfXom_mOUZ8: Downloading webpage
[youtube] lfXom_mOUZ8: Extracting video information
[youtube] lfXom_mOUZ8: Downloading DASH manifest
[download] Destination: Interview with Cisco's Ben Schumacher at OSCON 2015-lfXom_mOUZ8.mp4
[ffmpeg] Adding metadata to 'Interview with Cisco's Ben Schumacher at OSCON 2015-lfXom_mOUZ8.mp4'
[...]

Additional options

Download videos stored inside specified file.

$ youtube-dl --batch-file list 
[youtube] 5D4kSVcELjM: Downloading webpage
[youtube] 5D4kSVcELjM: Extracting video information
[youtube] 5D4kSVcELjM: Downloading DASH manifest
[download] Destination: Open Source Awards - OSCON 2015-5D4kSVcELjM.mp4
[download] 100% of 52.87MiB in 00:35
[...]

Define archive file to omit downloading same videos multiple times.

$ youtube-dl --batch-file list --download-archive /home/milosz/youtube.archive
[youtube] 5D4kSVcELjM: Downloading webpage
[youtube] 5D4kSVcELjM: Extracting video information
[youtube] 5D4kSVcELjM: Downloading DASH manifest
[download] Open Source Awards - OSCON 2015 has already been recorded in archive

Additional notes

I have received several of useful shell scripts from Omer Cunbul that will assist you in conversion process. You can access these using github.com/ocunn/shell.git repository.

Have a nice day.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu