Jan 12

Fix a corrupted MP4, not with ffmpeg but untrunc

Category: Linux   — Published by goeszen on January 12, 2023 at 10:02 pm

So you've downloaded a streaming video somewhere, probably with youtube-dl. Or you ripped a live stream from a site like Twitch or trovo.live. Or you've downloaded a mp4 video from somewhere else. And all these downloads have in common that at one point you suddenly, out of some impulse, or due to a network error, some hiccup on the remote end or whatever - you simply stopped, killed or canceled the download - without waiting for the download to finish! What you end up with once you try to play this video is what you see above: a corrupted mp4, unplayable and unseekable.

Now, how do you fix such a corrupted mp4? Is there hope? Well, yes, there is.

Your first solution might be to turn to the swiss army knife of video helpers, ffmpeg. And try a fresh remux of the corrupted mp4, rewriting the file to fix it. With a command like:

$ ffmpeg -fflags +genpts -i corrupted.mp4 -codec copy repaired.mp4

But that doesn't work. While ffmpeg normally munges *everything* - this time, it doesn't. The error, something like: "[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5607cfbee800] moov atom not found". Aw, shoot!

The problem is: for players to interpret the mp4 stream correctly, we need the "moov" atom. As it seems, players can't just "guess" what the contents of an mp4 container are. And more annoyingly, this important MOOV atom is usually placed at the END of a video file. No!

You might have come across this moov atom, with the ffmpeg option "-movflags faststart". This option would move the MOOV atom from the end of the file to the beginning - to enable a "fast start" for players. But, if you run this option on such a corrupted incomplete mp4, well, you guessed it, the atom isn't there! And ffmpeg can't just magically fix it by moving something that isn't there to the beginning.

Also, you can't just grab it from another file. At least not easily. And you can't just concatenate a working mp4 to the beginning or the end of a corrupted file. No, no.

Untrunc to the rescue

Before you go down the rabbit hole of all the costly, malware infected, commercial or subscription based options you usually get when you enter "fix corrupted mp4" or "ffmpeg moov atom not found" into a search engine - let me say to you: there's an open-source free and WORKING tool to do the job!

Untrunc (short for "untruncate") is a tool that tries to do this one job right. Get it from the untrunc repo on GitHub.

Untrunc will guess what your truncated file might need as moov atom and repair the file. There's only one catch: it needs a "template file", a reference file which had the same or very similar specs. For a downloaded stream, this might be no problem. Often times you have other segments of such a stream available or you can record another one. Or you have a file from the same camera. Or another file from the same resource.

Compiling untrunc is purportedly a bit difficult. But if you're on Ubuntu, you don't need to compile from source as there's a snap available from snapcraft.io. Just do:

$ snap find untrunc

will give you options. I went with the forked version by Michael Peeters, michaelp-anthwlock-untrunc. In case you use the snap version and have problems reading the video file, it might be because of the snap sandbox and permissions. Try relocating the problematic files to your home-folder, as snaps don't work across file-system boundaries without issues.

Leave a Reply

=