Split pattern.mp4 (in the zip) into two clips without re-encoding.
dotnet add package RecursiveAI.OneMediaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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:
Clips: 2Unzip the example, 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.
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:
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).
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:
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 · Trim a video without re-encoding
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 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 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.