---
title: "Extract embedded fonts from MKV in C#"
url: https://recursiveai.org/onemedia/cookbook/extract-mkv-font-attachments/
description: "Extract embedded fonts from MKV in C# with OneMedia. List Matroska attachments and save the original TTF or OTF bytes without extracting subtitle tracks."
version: "1.0.2"
---

# Extract embedded fonts from MKV in C#

**OneMedia** is a free, open-source media library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMedia) · [GitHub](https://github.com/recursive-ai-oss/onemedia-csharp) · [Docs](https://recursiveai.org/onemedia/docs/) · [Download example (.zip)](https://recursiveai.org/onemedia/cookbook/extract-mkv-font-attachments/extract-mkv-font-attachments.zip)

The zip’s `fonts.mkv` has a TrueType font attached.

```shell
dotnet add package RecursiveAI.OneMedia
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Tags.Matroska;
using var media = Media.Open("fonts.mkv");
var font = media.Metadata.Containers
    .OfType<MatroskaAttachments>().First()
    .Tags.First(f => f.MimeType == "font/ttf");
File.WriteAllBytes("font.ttf",
    font.ReadValue());
```

Unzip [the example](https://recursiveai.org/onemedia/cookbook/extract-mkv-font-attachments/extract-mkv-font-attachments.zip), then `dotnet run`.

Writes `font.ttf` (343140 bytes), replacing it if it already exists. The snippet throws `InvalidOperationException` if there’s no `font/ttf` attachment; the full program below also handles OTF, TTC, and older font media types.

## Full program

The input is a black clip with `DejaVuSansMono.ttf` attached as `font/ttf`. The zip includes the [DejaVu font’s license](https://dejavu-fonts.github.io/License.html). The full program lists the attachment sets, saves recognized TrueType and OpenType files and collections, and uses numbered names rather than writing to paths supplied by the MKV.

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Tags.Matroska;

using var media = Media.Open("fonts.mkv");
var attachments = media.Metadata.Containers
    .OfType<MatroskaAttachments>().SelectMany(set => set.Tags);
int count = 0;
foreach (var file in attachments)
{
    string? extension = file.MimeType?.ToLowerInvariant() switch
    {
        "font/ttf" or "application/x-font-ttf" => ".ttf",
        "font/otf" or "application/vnd.ms-opentype" => ".otf",
        "font/collection" => ".ttc",
        "application/x-truetype-font" or "application/octet-stream" =>
            Path.GetExtension(file.Key).ToLowerInvariant() switch
            {
                ".ttf" => ".ttf",
                ".otf" => ".otf",
                ".ttc" => ".ttc",
                _ => null
            },
        _ => null
    };
    if (extension is null)
        continue;

    byte[] bytes = file.ReadValue();
    if (bytes.LongLength != file.ValueRange.Length)
        throw new InvalidDataException($"Incomplete attachment: {file.Key}");

    // Use the original name for display only, never as an output path.
    string path = $"font-{++count}{extension}";
    using var output = File.Open(path, FileMode.CreateNew);
    output.Write(bytes);
    Console.WriteLine($"{file.Key} -> {path} ({bytes.Length} bytes)");
}
if (count == 0)
    Console.WriteLine("No TTF, OTF or TTC attachments found.");
```

`dotnet run` prints:

```text
DejaVuSansMono.ttf -> font-1.ttf (343140 bytes)
```

If there are no matching attachments, the full program prints `No TTF, OTF or TTC attachments found.` If an output already exists, it refuses to overwrite it; move the previous output before rerunning.

## Variations

**List all attachment names and media types without saving the files.** This includes non-font attachments such as cover images, when present:

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Tags.Matroska;
using var media = Media.Open("fonts.mkv");
foreach (var set in media.Metadata.Containers.OfType<MatroskaAttachments>())
    foreach (var file in set.Tags)
        Console.WriteLine($"{file.Key}: {file.MimeType}");
```

```text
DejaVuSansMono.ttf: font/ttf
```

## Related recipes

[Read MKV file info](https://recursiveai.org/onemedia/cookbook/read-mkv-webm-info/) · [Extract SRT from MKV](https://recursiveai.org/onemedia/cookbook/extract-subtitles-from-mkv/)

---

## The short answer

Open the MKV with `Media.Open`, then look for `MatroskaAttachments` in `media.Metadata.Containers`. Each item in an attachment set’s `Tags` represents a file. `Key` holds the stored filename, `MimeType` holds the declared media type, and `ReadValue()` returns the file bytes. Writing those bytes to disk extracts the font; it doesn’t install it, change it, or export the subtitles.

## How do I extract font attachments from MKV?

Enumerate the attachments, not the video, audio or subtitle tracks. This is also why looking only at `media.Tags.Pictures` won’t find a font: that view is for images. OneMedia [reads attachment data lazily](https://recursiveai.org/onemedia/docs/tags/), so you can first inspect names and types, then load only the attachments you want. Keep `media` open until the last `ReadValue()` call. A short read can return fewer bytes than the listed `ValueRange.Length`, so the full program checks the length before writing.

Look for `font/ttf`, `font/otf`, or `font/collection` if you want TrueType, OpenType or font collections. Older MKVs sometimes use types such as `application/x-font-ttf` or `application/vnd.ms-opentype`. For `application/octet-stream` and the ambiguous `application/x-truetype-font`, the example uses the stored extension as a hint. [The Matroska specification](https://www.rfc-editor.org/rfc/rfc9559.html#section-21) lists these conventions and other font media types. The example only exports TTF, OTF and TTC, and its filter uses metadata, not font validation.

Use the MKV’s original filename as a display label. It isn’t necessarily a safe or unique name on your filesystem. The numbered output names avoid using attachment names as paths and keep two files with the same stored name separate.

## Why are fonts attached to MKV subtitles?

Styled subtitles such as ASS can name a font that a player needs to render the subtitles as intended. [Matroska’s attachment rules](https://www.rfc-editor.org/rfc/rfc9559.html#section-21) explain why the file can carry that font alongside the track: the subtitle usually refers to the font by its internal name, not the attachment filename. Renaming the extracted file does not change that internal name.

Exporting the subtitle and exporting the font solve different problems. If you need the original styling outside the MKV, keep the styled subtitle format and any fonts it needs. If you only need plain dialogue and timing, an SRT export won’t use the fonts. A subtitle track can still name fonts when the MKV has no attachments; extraction can only return files that are actually in the container. Check each font’s license before redistributing it separately.
