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.mkvUnderstanding the Parameters
-i input.mkv: Specifies the source video file.-attach "path/to/font.ttf": Tells FFmpeg to attach the specified font file to the output container.-metadata:s:t:0 mimetype=application/x-truetype-font: Sets the MIME type of the newly attached stream. Thes:t:0specifier targets the first attachment stream (stream index 0 of type attachment).- Use
application/x-truetype-font(orfont/ttf) for TrueType fonts (.ttf). - Use
application/vnd.ms-opentype(orfont/otf) for OpenType fonts (.otf).
- Use
-c copy: Copies all video, audio, and subtitle streams directly without re-encoding, ensuring the process is instant and preserves original quality.output.mkv: The name of the final file containing the video and the embedded font.
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.mkvIn 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).