---
title: "Split an MP4 at keyframes in C#"
url: https://recursiveai.org/onemedia/cookbook/split-video-without-reencoding/
description: "Split an MP4 into separate clips in C# without re-encoding. Cut at existing keyframes and read the actual start time of each clip."
version: "1.0.2"
---

# Split an MP4 at keyframes 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/split-video-without-reencoding/split-video-without-reencoding.zip)

Split `pattern.mp4` (in the zip) into two clips without re-encoding.

```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;
var cut = TimeSpan.FromSeconds(1.5);
var parts = Media.Split("pattern.mp4",
    "clip-{0}.mp4", [cut]);
Console.WriteLine($"Clips: {parts.Count}");
```

Writes `clip-1.mp4` and `clip-2.mp4`. The cut lands at 1 second, the last keyframe before 1.5 seconds. Prints:

```text
Clips: 2
```

Unzip [the example](https://recursiveai.org/onemedia/cookbook/split-video-without-reencoding/split-video-without-reencoding.zip), then `dotnet run`.

## Full program

The CC0 sample is a three-second H.264 test pattern with keyframes at 0, 1, and 2 seconds and no audio. The program adds a second cut at 2.5 seconds. `SourceStart` gives each clip’s start on the original timeline, not its local playback time.

```csharp
using RecursiveAI.OneMedia;

var parts = Media.Split("pattern.mp4", "clip-{0}.mp4",
    [TimeSpan.FromSeconds(1.5), TimeSpan.FromSeconds(2.5)]);
Console.WriteLine($"Clips: {parts.Count}");
for (int i = 0; i < parts.Count; i++)
{
    var start = parts[i].SourceStart.ToTimeSpan();
    Console.WriteLine($"clip-{i + 1}.mp4 starts at {start}");
}
```

`dotnet run` prints:

```text
Clips: 3
clip-1.mp4 starts at 00:00:00
clip-2.mp4 starts at 00:00:01
clip-3.mp4 starts at 00:00:02
```

If the cut times aren’t positive and strictly ascending, `Media.Split` throws an `EditRefusedException` with reason `InvalidSplit`. On a successful split, it overwrites existing files with the same output names ([the file writer uses `overwrite: true`](https://github.com/recursive-ai-oss/onemedia-csharp/blob/ec3528e692cf5a9c532f023a15c5b4e1f2ff76d1/src/OneMedia/Editing/EditFiles.cs#L96-L99)).

## Variations

Split by an approximate payload budget instead of by time. The splitter cuts at a keyframe after the budget is reached, so this is not a maximum file size:

```csharp
using RecursiveAI.OneMedia;

var parts = Media.SplitBySize("pattern.mp4",
    "budget-{0}.mp4", maxPartBytes: 10_000);
Console.WriteLine($"Clips: {parts.Count}");
```

Prints `Clips: 3` for the sample file.

## Related recipes

[Get video keyframe timestamps](https://recursiveai.org/onemedia/cookbook/video-keyframes/) · [Trim a video without re-encoding](https://recursiveai.org/onemedia/cookbook/trim-video-without-reencoding/)

---

## The short answer

Use `Media.Split(input, outputPattern, cutTimes)`. OneMedia copies compressed packets to individual MP4 files. It uses the last existing keyframe at or before each requested cut. Use the returned `SourceStart` values to see where the parts actually begin. To keep just one excerpt rather than partition the input, use `Media.Trim` instead.

## How do you split a video so each clip starts with a keyframe?

A keyframe gives the decoder a starting picture it can decode without the previous clip. Pass times from the input’s playback timeline in ascending order. In the full program, the cut at 1.5 seconds falls back to 1 second and the cut at 2.5 seconds falls back to 2 seconds. The first clip starts at 0; the last clip runs to the end of the recording. If two requested times fall back to the same keyframe, they yield one cut, not two. [List the keyframes](https://recursiveai.org/onemedia/cookbook/video-keyframes/) before cutting if you need to know where the split can land.

## Can you split an MP4 file into two parts?

Yes. Pass just one time as in the snippet at the top. Pick a time at or after an interior keyframe so there is a second part, not just the original starting point. With `clip-{0}.mp4`, numbering starts at 1; `clip-{0:000}.mp4` pads the number to three digits. The part reports come back in that same order.

## How can I cut a video without re-encoding it?

[Split](https://recursiveai.org/onemedia/docs/trim/#split) keeps each original video packet in exactly one part. There’s no new lossy encoding step, but the nearest usable keyframe controls where each clip begins. If you need a single excerpt to *display* from an exact frame between keyframes, frame-accurate trim keeps the earlier packets needed for decoding and asks the player to hide them with an MP4 edit list. That is different from splitting the full recording into parts that start on keyframes with no hidden video lead-in.

The sample has no audio. With audio, a packet goes into the part in which it starts. If the first audio packet in a new part starts just after the video keyframe, OneMedia leaves a gap before that packet so the tracks stay in sync. There’s no audio pre-roll, so the first audio frame can also sound different in a separately played clip. Check the join points if the clips need to play back to back with seamless sound.
