MP4 is the most widely used media format (technically container) in the world. It was created in 2003 and is still used everywhere.
Too bad it sucks.
MP4 corruptions are very common, and happen whenever a video does not finish encoding. This can happen if a device that's recording loses power (like if your computer dies while recording with OBS),
or if an encoder dies before finishing (for example, if FFmpeg dies before it can finish encoding an MP4).
Regardless of the reason, a solution is needed. Up until a few days ago, I thought it was impossible without incredible manual repair efforts and the skills required to carry them out.
Fortunately, I was recommended an incredible piece of software that can fix corrupted MP4s in a lot of cases.
Fixing corrupted MP4s with Untrunc
Untrunc is a piece of software which can rebuild the information required to make MP4 files work based on the information stored in another video
from the same source. It is available as a piece of software you can compile, or as a free web service.
I imagine the website will be easier for most people, but in my case, I just compiled the software and ran it on my own machine, so I can't vouch for the site.
However you do it, this is the software you need to use.
The website is self-explanatory, and the software is very simple, but it needs to be compiled and run in the command line, so it may be too complicated for some people.
It also doesn't appear to run on Windows, so you'll need a Linux or Mac machine if you want to use the software on your own computer.
Find the file you want to fix, and then another video from the same source. If this was a physical medium like a camera, choose a video that has the same resolution and settings from the same camera.
If it's from a recording software like OBS, follow the same procedure and find a file with the same resolution and as many similarities as possible.
In both cases, choosing a video with similar length is recommended.
This is the part where you hope you're in luck. In both of the cases I tried this, it was successful and recovered everything. The first was a livestream
I recorded with youtube-dl, but was killed before finishing the MP4 file it was building. In that
case, the reference video I used was a piece of the stream built out of the last available stream segments that were still hanging around. The size
difference was very significant: ~5 seconds of sample vs. over 2 hours of stream to recover, but it worked nonetheless. The other video I recovered
was a recording I did with OBS in 2018 that I kept around in case I found a solution to fix it. I'm glad I did! I used an OBS recording from around the
same time, which was still much shorter than the corrupt recording, but it also worked. It's also worth noting that this file was in the MOV container,
which is a little different from MP4, but uses the same underlying codec (MPEG-4). Here's some more information on QuickTime (MOV)
files from Mozilla MDN.
Fixing broken MP4 containers with VLC
The files you get out of untrunc (assuming it worked for you) work with some players and are generally ok, but will have some problems. For example,
the livestream I recovered ended up playing with MPV and VLC, but in other players (and also with FFmpeg), it was treated as if it was only 30 seconds
long. This wasn't true, since the stream was actually over 2 hours long. FFmpeg was no help in correcting this problem. Fortunately, before this I had
figured out how to correct broken MP4 containers with VLC. VLC is actually not a very good media player in terms of its decoder, and notoriously so.
In fact, it's known for messing up videos when being played or converted with it. MPV is a much better player since it's powered by FFmpeg, but since
FFmpeg can't understand everything and is sometimes not very forgiving with video and container issues, VLC can be helpful.
You can use VLC to losslessly repair a video's container by going into and clicking .
Next, add the file you need to repair with , and then click .
After having done that, you're at the point where you need to select the settings to use. Use Video - H.264 + MP3 (MP4), and then click the
wrench next to it to change the specific settings for that codec.
Here are the settings you need to set:
Video codec settings:
Keep original video track checked
Audio codec settings:
Keep original audio track checked
Now, click , select a destination for the corrected file, and press . The process should be fairly quick
since it's not encoding any video, only copying the existing data to a new, fixed container.
Of course, in VLC's infinite stupidity, it will try to loop the process, and will ask if you want to "replace" the file after it's done. What you need
to do here is either close that dialog quickly and press the close button, or kill VLC from the task manager. Whichever way you choose, it's safe to
force kill VLC as long as the process already completed successfully.
You're saved! (I hope)
If all went well, you have a completely clean video file that works properly and has all the video that was originally supposed to be in it.
If not, well I hope this will be useful to you in the future if you run into an issue like this again and need to scramble for a solution.
The best solutions are preventative though, so if you can avoid it, DO NOT USE MP4 OR MOV. In OBS, there are other formats to choose from, and I believe
that the program even warns you about using MP4. Just don't open yourself up to these types of problems. If you're using hardware that can't be changed,
Written by Termer, unless otherwise noted.
Subscribe to the RSS feed to get posts in your feed reader.