-
Notifications
You must be signed in to change notification settings - Fork 21
Subtitles and mp4 metadata
To embed non-subtitle metadata (like the title, penta id, ...) into an mp4 file:
apt-get install atomicparsley
AtomicParsley test.mp4 --TVShowName "FOSDEM" --TVEpisodeNum 1234 --TVSeason 18 --overWrite
Or use mp4v2-utils:
apt-get install mp4v2-utils
mp4tags -album 'FOSDEM 2018' -song 'Embedding mp4 tags in blah using llvm' -season 18 -episode 1234 -year 2018 -longdesc 'Ik heb eerbied voor jouw grijze haaaaareeeeen. Ze beschermen je lieve geziiiiiicht.' test.mp4
Then check if metadata have been recorded into the mp4 correctly:
AtomicParsley test.mp4 -t
https://amara.org is as free a subtitling webapp as it gets. It is also used by the Debconf subtitles team.
The debconf subs team has created a python script for syncing subtitle files from amara.org that might be useful for us: https://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=debconfsubs/debconfsubs.git;a=blob;f=scripts/2016/amara.py;h=54aa959ec37b0c66eab393ecfed36f79d76dfc40;hb=HEAD .
Vtt is the w3c standard format for web based subtitle playback.
- One can export subtitles in .vtt directly from amara.org.
- Ffmpeg can convert existing subtitles to .vtt:
ffmpeg -i test.srt test.vtt
In theory, html5 should support subtitle track playback without any javascript. In practice, there are still a few rough edges.
We use the clappr java script based player for streaming. It makes sense to also use that for later playback. Clappr has quite good builtin support for subtitle playback. An example:
<html>
<head>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clappr@latest/dist/clappr.min.js"></script>
</head>
<body>
<div id="player"></div>
<script>
var player = new Clappr.Player({
parentId: '#player',
source: 'http://clappr.io/highline.mp4',
poster: 'http://clappr.io/poster.png',
height: 360,
width: 640,
playback: {
crossOrigin: 'anonymous', // Required if track loaded from another domain
externalTracks: [
{lang: 'en', label: 'English', src: 'https://static.playmedia-cdn.net/clappr/en.vtt'},
],
},
});
</script>
</body>
</html>
Using mp4box.js, it should be possible to extract the subtitle track from an mp4 in the browser using javascript! This enables one to play the video with embedded subtitles in a html5/js player.
First, we install the required tools:
apt-get install gpac
Now we embed the subtitles into the mp4. It seems the embedded subtitles have to be in vtt format. This seems to confuse most other players than the custom mp4box.js one enough that they won't play the embedded subtitles. Totem even refuses to play the file with the embedded subtitles at all.
Either we embed from a vtt formatted source file:
MP4Box -add subtitle.vtt:lang=en video.mp4
or alternatively, from srt:
MP4Box -add subtitle.srt:FMT=VTT:lang=en video.mp4
It should also be possible to add some text styling options to this. Needs investigation.
Now we can play the mp4 with embedded subtitles in the browser based js player example at http://download.tsi.telecom-paristech.fr/gpac/mp4box.js/. Only, since this is is javascript, it requires permissive CORS settings on the machine serving the actual video :( For testing, one can walk around this with
chromium --test-type --disable-web-security -user-data-dir /tmp
Further reading see:
- https://en.wikipedia.org/wiki/MPEG-4_Part_17
- https://github.com/google/ExoPlayer/issues/689
- https://github.com/gpac/mp4box.js/
- https://gpac.wp.imt.fr/2015/08/19/announcing-mp4box-js/
- http://download.tsi.telecom-paristech.fr/gpac/mp4box.js/
- https://stackoverflow.com/questions/27223702/is-it-possible-for-html5-video-to-play-subtitles-embedded-in-mp4-file-as-a-track
Embedding a subtitle track into an mp4 file is easy.
ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4
...or more explicitly:
ffmpeg -i infile.mp4 -i infile.srt -c:v copy -c:a copy -c:s mov_text outfile.mp4
Burning the subtitles into the video is probably not a great idea. Still, adding it here for completeness.
It's easiest to use the libass library. Make sure your ffmpeg install has the library in the configuration --enable-libass.
First convert the subtitles to .ass format:
ffmpeg -i test.srt test.ass
Then add them using a video filter (via libass):
ffmpeg -i test.mp4 -vf ass=test.ass test_subtitles_burned_in.mp4
- https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery/Adding_captions_and_subtitles_to_HTML5_video (thanks pierres for the hint)