How do I recover truncated AVI/DV video? Short version: I believe I have truncated but otherwise valid AVI/DV files that won't play in any player I've tried (Adobe Premiere, MPC HC, VLC). I assume the stream data is intact but truncated, and the container is damaged. Are the files still recoverable and how would I accomplish this?

Long version: Having finished a recording from a Sony AnyCast workstation with two cameras to a FireWire hard disk drive, I accidentially unplugged the hard disk drive without "disconnecting" (unmounting) it first.

The result was a corrupted ext3 filesystem that would not mount under the Windows Ext2 IFS setup I was using to download the videos, so I connected the hard disk to a Linux laptop and ran e2fsck on it. After this, I was able to list the directory and copy the files, but the files appeared corrupted.

I'm working under the assumption that I've only lost some data at the end of the two video files. (This might prove false as e2fsck might have wrecked havoc on them.) That would mean I have 1,5 hours (x2) of unscathed DV stream. Unfortunately, my layman analysis concludes that these lost bits must have contained some information vital to the container format.

Note that all I have are the files copied to an NTFS volume from the fsck'ed ext3 volume. I no longer have access to the original hard disk drive.

Dear StackExchange, please help me find a way to recover and reconstruct these files! I suspect what I would need is some software that would guess or be told the information usually contained in the container header, copy the stream data and write it into a new, valid container file.

Edit: I believe the format is DV instead of MPEG2.

Edit2: It seems that the broken files do not in fact have an AVI header at all - they should begin with the bytes "RIFF" which they do not. ffprobe does recognize the stream format, though:

[dv @ 0xefb8a0] Estimating duration from bitrate, this may be inaccurate
Input #0, dv, from '120610-1-001.avi':
  Duration: 02:08:36.84, start: 0.000000, bitrate: 28799 kb/s
    Stream #0.0: Video: dvvideo, yuv420p, 720x576, 28800 kb/s, PAR 64:45 DAR 16:9, 25 tbr, 25 tbn, 25 tbc
    Stream #0.1: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s


Edit3: Thanks to Matti "Lumpio-" Virkkunen, we've finally got the video files fixed. He wrote a good 400 lines of C that did exactly what @JEEB suggested: crafted an AVI header and searched for things that look like AVI chunks, copying them over to the output file. A long AVI file needs a new header between every gigabyte or so, and the program simply fixes the broken first section and then copies the following sections as-is. The source code for fixavi.c can be found on Gist.

Please note that details such as codecs, audio sample rate and video frame rate are hard-coded in the source, so if you need to replicate this process, please adapt the source to your needs. I found Alexander Noe's AVI format guide a good reference on the AVI file format.

Edit4: And as fixavi.c does not create an index at the end of the AVI file, I simply used ./fixavi broken.avi recovered_without_index.avi; ffmpeg -i recovered_without_index.avi -acodec copy -vcodec copy recovered_with_index.avi to make one.
First of all, if I remember correctly, the DV format is intra-only, and thus if there is various damage that has happened to your video, there should be a relatively big chance of getting at least something out -- given that the file isn't completely broken in the most relevant of places.

A good way of checking if there's at least some kind of sanity left in the file is indeed by the aforementioned ffmpeg|avconv -i input.avi or ffprobe|avprobe input.avi. If ffmpeg/avconv and/or the related probe application can 'see' the format as well as other information there's at least some hope that the file actually still contains some of the things you're hoping it contains.

As for actual recovery attempts, I would actually first make a copy of the file (backups are generally useful, even if it's a backup of possibly broken data), and try to open it with Avery Lee's VirtualDub, as I remember it having one of the good general AVI reconstruction mechanisms I've seen so far. If you are lucky and there's enough data for VirtualDub to actually do something with the file, it should ask if you want to repair/reconstruct the index and so forth. After that you can set both audio and video to 'Direct stream copy', and just remux with another file name, and see if that works.

Another way to try and salvage it is to use ffmpeg/avconv, and use the stream copying function there (of course this might not work at all in case ffmpeg/avconv or the related probe application cannot see what container/video/audio format the file is). ffmpeg|avconv -i input.avi -vcodec copy -acodec copy out.avi

A yet another way to try and salvage something is by trying out the now-discontinued AVI-Mux GUI. I have not tried it myself, but I have some hazy memories of people fixing their broken AVI files by remuxing with it a few years ago.

Edit:
And of course the so-called final destination would be to see if there are any chunk-like entities in the file, and manually recreating the AVI header and friends before it either via scripting or with a hex editor, looking at the related technical documentation.