Recursive AI

Concatenate MP4 files in C# without re-encoding

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

Join MP4 clips with matching tracks. The example uses red.mp4 and blue.mp4 from the download: two one-second, silent H.264 clips.

Terminal
dotnet add package RecursiveAI.OneMedia

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMedia;
 
var clip = Media.Concat("joined.mp4",
    "red.mp4", "blue.mp4");
Console.WriteLine(clip.Duration.ToTimeSpan());

Writes joined.mp4 and prints its duration:

Text
00:00:02

Unzip the example, then dotnet run.

The full program

The output plays red first, then blue. The program also prints the operation’s notes, if any.

C#
using RecursiveAI.OneMedia;
 
var clip = Media.Concat("joined.mp4",
    "red.mp4", "blue.mp4");
Console.WriteLine(clip.Duration.ToTimeSpan());
Console.WriteLine($"Notes: {clip.Notes.Count}");
foreach (string note in clip.Notes)
    Console.WriteLine(note);
Text
00:00:02
Notes: 0

If a required track doesn’t match, Media.Concat throws EditRefusedException with reason NeedsReencode instead of writing a new output file. Use a different output path from your inputs; a successful join replaces an existing file at the destination.

Variations

To play the clips in the opposite order, put blue before red. Replace the Media.Concat call with:

C#
var clip = Media.Concat("reversed.mp4",
    "blue.mp4", "red.mp4");

To see which track doesn’t match, replace Program.cs with this variation. It joins the 64×48 red clip with mismatch.mp4, a 96×64 green clip in the download.

C#
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Editing;
 
try
{
    Media.Concat("refused.mp4", "red.mp4", "mismatch.mp4");
}
catch (EditRefusedException e)
{
    Console.WriteLine($"{e.Reason}, track index {e.TrackIndex}");
}
Text
NeedsReencode, track index 0

The short answer

Call Media.Concat with the output path first and then the input paths in playback order. OneMedia copies the compressed packets into a new MP4 and shifts their timestamps to follow each other. It does not decode the clips and encode them again. The first file determines which tracks OneMedia tries to keep.

How do you concatenate MP4 files with different tracks?

For a join without re-encoding, each output track needs a compatible track at the same index in each input. The public track-match code checks this before copying packets. If the first file has video at index 0 and audio at index 1, a later video-only file cannot supply the required audio track. If the first file is video-only, an extra audio track in a later clip does not become an output track.

The comparison goes beyond the filename extension and codec name. Tracks must match in kind, codec and codec tag, timescale (ticks per second), and codec configuration bytes. Audio format and video frame size, rotation and pixel aspect ratio are checked too. Two 1080p H.264 files can still differ in encoder setup and fail the join. For your own clips, start with exports made with the same settings, including the track order. If they differ, inspect the exception’s TrackIndex and message. Normalize the incompatible tracks before trying again; the join itself won’t convert them.

How do you merge MP4 files without losing quality?

Use packet copying when the tracks are compatible. Because the compressed samples are copied unchanged, there is no additional generation of lossy video compression. The file as a whole is new. OneMedia adjusts the timestamps and moves each clip’s playback instructions to its new position, so frames used to start decoding but not meant to be shown stay hidden. See the joiner’s timing rules.

Copying samples is not the same as preserving every track. Check ClipReport.Notes for tracks the writer couldn’t carry. If you need to preserve both audio and video, compare the output with the track list you expect, particularly if the clips don’t all come from the same source. The sample has one video track per input and no audio, so there are no audio settings to match in that run.

All onemedia recipes