Sunday 6 January 2019

Downloading YouTube videos with hardcoded subtitles

I recently bought a Chromecast device, and I use VLC in my Linux computer to stream videos to our TV. Though not always, sometimes these videos come from YouTube, and I would like to be able to see the video subtitles in the TV, but as the time of writing this, VLC+Chromecast and subtitles don't seem to play along nicely (see https://code.videolan.org/videolan/vlc-android/issues/502). While a future version of VLC is likely to include this feature, in the meantime I figured out the way to hardcode the subtitles. It is very easy, but time consuming, since we have to encode the video again. The following commands are an example of what needs to be done:

(useful to see the different available formats, and get the one that corresponds to 1280x720)
youtube-dl  --list-formats  https://www.youtube.com/watch?v=AW138ZTKioM

(and the available subtitles)
youtube-dl  --list-subs https://www.youtube.com/watch?v=AW138ZTKioM
 
(download the video in the required format, -f, and convert the subtitles to the .srt format)
youtube-dl  -f 22  --convert-subs srt --write-sub --sub-lang en https://www.youtube.com/watch?v=AW138ZTKioM
 
(use ffmpeg to reencode with the downloaded subtitles hardcoded)
ffmpeg -i The\ Anthropocene\ -\ The\ age\ of\ mankind\ -\ Docu\ -\ 2017-AW138ZTKioM.mp4 -vf "subtitles='The Anthropocene - The age of mankind - Docu - 2017-AW138ZTKioM.en.srt'" The_age_of_mankind.mp4

Done!

[update: 8/1/2019. Hardcoding the subtitles might be handy in several situations, but for watching YouTube videos via Chromecast, it is actually much easier to use Chromium (I'm using ArchLinux where Chrome is not in the official repositories), which comes with the option of casting YouTube, a given tab, or even the whole desktop).