How to Add Fonts to MKV Using FFmpeg

Embedding external fonts into an MKV (Matroska) container ensures that stylized subtitles render correctly on any device, even if the viewer does not have those specific fonts installed on their system. This guide provides a straightforward, step-by-step explanation of how to use FFmpeg to attach font files directly into an MKV video stream without re-encoding the video.

The Basic Command to Attach a Font

To insert a single font file into an MKV container, use the following FFmpeg command structure:

ffmpeg -i input.mkv -attach "path/to/font.ttf" -metadata:s:t:0 mimetype=application/x-truetype-font -c copy output.mkv

Understanding the Parameters

Attaching Multiple Fonts

If your subtitles require multiple fonts or font weights (such as regular, bold, and italic), you can attach multiple files in a single command. You must define the MIME type for each attachment by incrementing the stream index:

ffmpeg -i input.mkv \
  -attach "font-regular.ttf" \
  -attach "font-bold.ttf" \
  -metadata:s:t:0 mimetype=application/x-truetype-font \
  -metadata:s:t:1 mimetype=application/x-truetype-font \
  -c copy output.mkv

In this command, s:t:0 corresponds to the first attachment (font-regular.ttf), and s:t:1 corresponds to the second attachment (font-bold.ttf).