How to Attach Fonts to MKV with FFmpeg
Embedding external fonts or attachments directly into an MKV (Matroska) container ensures that styled subtitles render correctly across different media players without requiring users to manually install those fonts on their local systems. This article provides a direct, step-by-step guide on how to use FFmpeg to insert fonts and other attachments into an MKV file, detailing the correct commands and parameter configurations for both single and multiple file attachments.
The Basic Command for Adding a Font
To attach a single TrueType (.ttf) or OpenType (.otf) font to an MKV
file, use FFmpeg’s -attach option and define the
appropriate MIME type for the attachment stream.
Here is the basic command:
ffmpeg -i input.mkv -attach "path/to/font.ttf" -metadata:s:t:0 mimetype=application/x-truetype-font -c copy output.mkvParameter Breakdown
-i input.mkv: Specifies the source video file.-attach "path/to/font.ttf": Tells FFmpeg to load the specified font file as an attachment stream.-metadata:s:t:0 mimetype=application/x-truetype-font: Sets the metadata for the attachment. Thes:t:0selector targets the first attachment stream (index 0). Themimetypetells the media player how to interpret the file.-c copy: Instructs FFmpeg to copy all existing video, audio, and subtitle streams without re-encoding them, which makes the process extremely fast and lossless.output.mkv: The path for the newly generated MKV file containing the embedded font.
Font MIME Types
You must specify the correct MIME type depending on the font format you are embedding:
- TrueType (.ttf):
application/x-truetype-fontorfont/sfnt - OpenType (.otf):
application/vnd.ms-opentypeorfont/otf - Web Open Font Format (.woff):
font/woff
How to Attach Multiple Fonts
If your subtitles use multiple fonts, you can attach them all in a
single command. You must increment the stream index (s:t:0,
s:t:1, etc.) for each attachment’s metadata:
ffmpeg -i input.mkv \
-attach "Arial.ttf" \
-attach "TimesNewRoman.otf" \
-metadata:s:t:0 mimetype=application/x-truetype-font \
-metadata:s:t:1 mimetype=application/vnd.ms-opentype \
-c copy output.mkvIn this command, s:t:0 refers to Arial.ttf,
while s:t:1 refers to TimesNewRoman.otf. All
streams are copied directly into the new output container.