---
title: "Concatenate MP4 files in C# without re-encoding"
url: https://recursiveai.org/onemedia/cookbook/concatenate-video-without-reencoding/
description: "Concatenate compatible MP4 files in C# without re-encoding. Join them in order with OneMedia and see what to do when tracks don’t match."
version: "1.0.2"
---

# 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.

[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/concatenate-video-without-reencoding/concatenate-video-without-reencoding.zip)

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

```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 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](https://recursiveai.org/onemedia/cookbook/concatenate-video-without-reencoding/concatenate-video-without-reencoding.zip), then `dotnet run`.

## The full program

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

```csharp
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:

```csharp
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.

```csharp
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
```

## Related recipes

- [Trim a video in C# without re-encoding](https://recursiveai.org/onemedia/cookbook/trim-video-without-reencoding/)
- [Read MP4 metadata in C#](https://recursiveai.org/onemedia/cookbook/read-mp4-metadata/)

## 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](https://github.com/recursive-ai-oss/onemedia-csharp/blob/ec3528e692cf5a9c532f023a15c5b4e1f2ff76d1/src/OneMedia/Editing/IsoJoiner.cs#L68-L85) 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](https://github.com/recursive-ai-oss/onemedia-csharp/blob/ec3528e692cf5a9c532f023a15c5b4e1f2ff76d1/src/OneMedia/Editing/IsoJoiner.cs#L280-L313) 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](https://github.com/recursive-ai-oss/onemedia-csharp/blob/ec3528e692cf5a9c532f023a15c5b4e1f2ff76d1/src/OneMedia/Editing/IsoJoiner.cs#L5-L10).

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.
