---
title: "Write a fragmented MP4 file in C#"
url: https://recursiveai.org/onemedia/cookbook/write-fragmented-mp4/
description: "Write a fragmented MP4 (fMP4) file in C# without re-encoding. Copy existing tracks into one file and choose the fragment duration."
version: "1.0.2"
---

# Write a fragmented MP4 file 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/write-fragmented-mp4/write-fragmented-mp4.zip)

Copy `pattern.mp4` (in the zip) into one MP4 file with media in fragments.

```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.Remuxing;
var report = Media.Remux("pattern.mp4",
    "out.mp4", RemuxFormat.FragmentedMp4);
Console.WriteLine(report.Tracks[0].Packets);
```

Writes `out.mp4` without re-encoding. Prints the number of video packets copied from this sample:

```text
72
```

Unzip [the example](https://recursiveai.org/onemedia/cookbook/write-fragmented-mp4/write-fragmented-mp4.zip), then `dotnet run`.

## Full program

The CC0 sample is a three-second H.264 test pattern at 24 frames per second, with one keyframe per second and no audio. The program also prints the number of entries in `report.Losses`, which lists what the remux didn’t carry over.

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Remuxing;

var report = Media.Remux("pattern.mp4", "out.mp4",
    RemuxFormat.FragmentedMp4);
Console.WriteLine($"Packets: {report.Tracks[0].Packets}");
Console.WriteLine($"Reported losses: {report.Losses.Count}");
```

`dotnet run` prints:

```text
Packets: 72
Reported losses: 0
```

If `out.mp4` already exists, the file-to-file remux refuses to replace it by default; pick a different output name or move the old file before running again.

## Variations

Set a one-second fragment target. Use `Remuxer.Remux` to pass both the explicit fragmented MP4 format and the options in the same call:

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Remuxing;

Remuxer.Remux("pattern.mp4", "one-second.mp4",
    RemuxFormat.FragmentedMp4, new RemuxOptions
    {
        FragmentDuration = MediaTime.FromTimeSpan(
            TimeSpan.FromSeconds(1))
    });
Console.WriteLine("Wrote one-second.mp4");
```

Prints:

```text
Wrote one-second.mp4
```

## Related recipes

[Move the MP4 header to the front](https://recursiveai.org/onemedia/cookbook/mp4-faststart/) · [Create a DASH manifest from an MP4](https://recursiveai.org/onemedia/cookbook/create-dash-mpd-from-mp4/)

---

## The short answer

Pass `RemuxFormat.FragmentedMp4` as the third argument to `Media.Remux`. Both files in the recipe use the `.mp4` extension, but the explicit format changes how the output stores the packets. [Remuxing](https://recursiveai.org/onemedia/docs/remux/) copies compressed media into a new container layout; it doesn’t decode the pictures or encode them again. This call writes a single `.mp4` file, not a DASH manifest, a playlist, or a directory of independent clips.

## What is fragmented MP4 format?

A fragmented MP4 (fMP4) starts with a file type box (`ftyp`) and a movie box (`moov`) that describes the tracks. It then stores media in pairs of boxes: `moof` describes the samples in a fragment and the following `mdat` holds their data. The next fragment has its own pair. [OneMedia’s muxing docs](https://recursiveai.org/onemedia/docs/muxing/#fragmented-files-and-cmaf) describe which timing and sample information the writer puts in `moof`.

The default duration target is two seconds. With video, the muxer starts a new fragment at a keyframe once it reaches that target, or cuts at the fragment byte limit. A keyframe gives the decoder a point from which it can start without decoding earlier pictures. If keyframes are farther apart, a duration setting alone won’t make them closer together: the writer is copying the existing frames. The three-second example has keyframes at 0, 1, and 2 seconds, so the default layout holds the first two seconds in one fragment and the last second in another.

## What is the difference between fragmented MP4 and MP4?

An ordinary, non-fragmented MP4 keeps its sample tables together in `moov`. A fast-start MP4 puts that box ahead of the media data so the reader encounters those tables first. Fast start does not introduce fragments. Both fast-start and fragmented files can have a `moov` box near the beginning; what distinguishes fMP4 is the series of `moof` boxes that describe successive groups of samples. See the [layout table](https://recursiveai.org/onemedia/docs/muxing/#layouts) for the box order of each form.

Choose the layout your reader expects. If it requires fMP4, pass the explicit fragmented format rather than relying on the extension. If your viewer expects an ordinary fast-start MP4, keep that layout instead. Neither layout changes the video codec, and wrapping packets in fMP4 won’t make an unsupported codec playable. When you need separate MP4 files that open on their own, [split at keyframes](https://recursiveai.org/onemedia/cookbook/split-video-without-reencoding/) instead of writing fragments inside one file.
