---
title: "Rebuild a corrupted AVI index in C#"
url: https://recursiveai.org/onemedia/cookbook/repair-corrupted-avi-index/
description: "Rebuild a corrupted AVI index in C# from intact media chunks. Write a new AVI without re-encoding, then reopen it, seek, and read a packet."
version: "1.0.2"
---

# Rebuild a corrupted AVI index 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/repair-corrupted-avi-index/repair-corrupted-avi-index.zip)

An AVI index tells a player where to seek. Rebuild it from intact media chunks using `broken.avi` in the zip or your own AVI file.

```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 report = Media.Repair(
    "broken.avi", "fixed.avi");
Console.WriteLine(report.Status);
```

[The example](https://recursiveai.org/onemedia/cookbook/repair-corrupted-avi-index/repair-corrupted-avi-index.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Writes a new file, `fixed.avi`, without re-encoding. The output filename must not already exist. For the sample, the console prints:

```text
Repaired
```

## The full program

The zip contains a synthetic three-second video with 12 Motion JPEG frames and no audio. All 12 frame offsets in its `idx1` index point outside the file; the actual video chunks are intact. The full program checks for a usable index, seeks to one second, and reads the encoded packet at that position.

```csharp
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Avi;

var report = Media.Repair("broken.avi", "fixed.avi");
Console.WriteLine(report.Status);
foreach (string warning in report.Warnings)
    Console.WriteLine(warning);
if (!report.OutputWritten)
    return;

using var media = Media.Open("fixed.avi");
if (media.Demuxer is not AviDemuxer { HasIndex: true } avi)
    throw new InvalidDataException("The AVI has no usable index.");

var target = MediaTime.FromSeconds(1);
var landed = avi.Seek(target);
if (!avi.TryReadPacket(out var packet))
    throw new InvalidDataException("No packet after seeking.");
Console.WriteLine("AVI index: present");
Console.WriteLine($"Seek landed at {landed.ToTimeSpan():c}");
Console.WriteLine($"Packet at {packet.Info.Pts.ToTimeSpan():c}: " +
    $"{packet.Data.Length} bytes");
```

`dotnet run` prints:

```text
Repaired
AVI index: present
Seek landed at 00:00:01
Packet at 00:00:01: 1907 bytes
```

If repair returns `Unrepairable`, there’s no output file; read `report.Warnings` before trying to open it.

## Variations

Repair `fixed.avi` from the full program again to see how the API reports a healthy index:

```csharp
using RecursiveAI.OneMedia;
var report = Media.Repair("fixed.avi", "copy.avi");
Console.WriteLine(report.Status);
```

```text
NothingToRepair
```

This still writes `copy.avi`, a byte-identical copy of `fixed.avi`.

## Related recipes

[Repair a corrupted MP4](https://recursiveai.org/onemedia/cookbook/repair-corrupted-mp4/) · [List video tracks](https://recursiveai.org/onemedia/cookbook/list-video-tracks/)

---

## The short answer

Call `Media.Repair` with the broken AVI and a new output path. OneMedia scans for intact media chunks and rebuilds the index without changing the input or re-encoding the video. Reopen the new AVI and read a packet after seeking to check that the index is usable.

## How do I rebuild an AVI index?

AVI keeps chunks of encoded media in `movi` lists. The index tells a reader where to find those chunks without scanning from the start. A file with broken index offsets may still contain valid media. In the sample, that’s the difference between the bad `idx1` entries and the intact video chunks they were supposed to point to.

According to the [OneMedia repair docs](https://recursiveai.org/onemedia/docs/repair/), repair walks each `movi` list by the chunk headers rather than trusting the old index. It rebuilds `idx1` and the applicable frame counts and sizes. For OpenDML AVI, it also rebuilds the standard indexes and per-stream super indexes. Old index chunks aren’t reused.

The example checks `HasIndex` on the repaired file and reads a packet after seeking. A seek can land on the preceding keyframe rather than the exact requested timestamp. Here every Motion JPEG frame can be read on its own, and the request for one second lands on a frame at one second.

## Can I repair a corrupted AVI if the video data is damaged?

Index repair can locate intact chunks; it can’t restore missing bytes. The AVI repair code can drop a cut final chunk or skip bytes it can’t identify as part of a chunk. On your own file, inspect `Status`, `Warnings`, and `Items` before relying on the result. A `PartiallyRepaired` status is not a guarantee that every frame was kept. Keep the original AVI, and check a few positions near any known damage rather than relying on a successful seek in an intact portion of the file.
