Downloading videos and subtitles with youtube-dl

in #technology6 years ago

It's time for Tech Tuesday.

youtube-dl

Youtube-dl is pretty much the de-facto way to download a youtube video from the command line for all popular OSes (Windows, Linux, MacOS). You can get it from HERE. You will also need ffmpeg since youtube-dl uses it a lot.

Super straight forward if one wants to download a video only:

$ youtube-dl https://www.youtube.com/watch?v=kJQP7kiw5Fk


The great thing is that one can download also the included subtitles. Here are all the options:

--write-sub                      Write subtitle file
--write-auto-sub                 Write automatically generated subtitle file
                                 (YouTube only)
--all-subs                       Download all the available subtitles of the
                                 video
--list-subs                      List all available subtitles for the video
--sub-format FORMAT              Subtitle format, accepts formats
                                 preference, for example: "srt" or
                                 "ass/srt/best"
--sub-lang LANGS                 Languages of the subtitles to download
                                 (optional) separated by commas, use --list-
                                 subs for available language tags


Want to see all available subtitles for a video? No problem:

$ youtube-dl --list-subs --skip-download https://www.youtube.com/watch?v=kJQP7kiw5Fk
Available subtitles for kJQP7kiw5Fk:
Language formats
ja       vtt, ttml
es       vtt, ttml


Want to download english subtitles for the video? Lets try:

$ youtube-dl --write-sub --sub-lang en --skip-download https://www.youtube.com/watch?v=kJQP7kiw5Fk
WARNING: en subtitles not available for kJQP7kiw5Fk


Makes sense. From the first command it already told me that the video only has 'ja' and 'es' subtitles included. Thus changing 'en' with 'es' will download the corresponding Spanish subtitles.

However youtube auto-generates subtitles if no translator has attached subtitles to his video. We can use the '--write-auto-sub' option for that:

$ youtube-dl --write-auto-sub --sub-lang en --skip-download https://www.youtube.com/watch?v=kJQP7kiw5Fk
[info] Writing video subtitles to: Luis Fonsi - Despacito ft. Daddy Yankee-kJQP7kiw5Fk.en.vtt


Wow! Now I can finally understand!

Bonus Tip: Geo Restricted videos

Ever seen the annoying 'This video is not available in your country' restriction on youtube? Youtube-dl can deal with that too. It has some nifty GEO restriction options.

$ youtube-dl --geo-bypass [Video URL]


Simple as that. Happy off-line watching!