Creating a WebM video with subtitles

This is mostly a note pad for myself with quick instructions about how to create WebM video file with embedded subtitles from the command line.

Having a source video with A/V tracks that are WebM compliant, and a subtitles track, this is the quick reference command to copy and paste:

$ 
ffmpeg -i video.mkv -c:a copy -c:v copy -c:s webvtt video.webm

Now the long and boring story.

Lately, my preferred multimedia container is Matroska and, for easily managing videos with multiple tracks without reencoding them I use the great MKVToolNix application.

Now, when I want to put a video online, the 2 video containers with wider support among the Open Source browsers are .mp4 and .webm. I’d rather use WebM whenever possible and, additionally, it is a subset of Matroska. Indeed, MKVToolNix has an option to “Create a WebM compliant file” as output.

Up until now, when dealing with a subtitles track I’ve always tried to use the SubRip subtitles format (.srt). However, when I tried with MKVToolNix to create a WebM compliant file with a .srt track, I’d get the error The codec type 'text subtitles' cannot be used in a WebM compliant file.

Hence, in order to not get too complicated, I’d generate a Matroska file and just convert it to .mp4 without reencoding with FFmpeg:

$ 
ffmpeg -i video.mkv -c:v copy -c:a copy -c:s mov_text video.mp4

However, recently a colleague warned me that FireFox in Linux doesn’t reproduce the audio track of a .mp4 file if the codec is one of Vorbis or Opus, the ones used with WebM. Yes, FireFox supports WebM and, hence, those audio codecs, but refuses to play them if they belong inside a .mp4 container (!!)

OK, so what now … a first quick check to the WebM’s documentation told me that WebM doesn’t support subitles but that intends to support WebVTT in the future …

… but the same colleague told me that WebM actually does support WebVTT. A second look to that very same page tells me that he is right! Confusing …

Fine. Let’s use MKVToolNix to create that WebM compliant file but with a WebVTT track … oh, god I got the error The codec type 'passthrough' cannot be used in a WebM compliant file. So, does WebM support subtitles in the end or not?!!!

The answer is that WebM’s way of storing WebVTT subtitles is based on an outdated WebVTT spec, and fundamentally different than Matroska’s way (which was based on the current WebVTT specs) and MKVToolNix has no intention of supporting the former.

So, WebM does support subtitles. They need to be in WebVTT format but, in order to create a WebM with subtitles the easiest way I’ve found is through FFmpeg.

End of the story?

It seems not. GStreamer is not able to deal with the WebVTT subtitles track in a WebM or Matroska file, while other programs, like mpv, have no problems with them.

Leave a Reply

Your email address will not be published. Required fields are marked *