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.mkv

Parameter Breakdown

Font MIME Types

You must specify the correct MIME type depending on the font format you are embedding:

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.mkv

In 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.