How to fix a corrupt MP4 file from OBS with Untrunc

- by

When an MP4 file is recording and something untoward happens, the file isn’t closed correctly and as such is not readable by your computer. It’s unfortunate when this happens, and many will tell you the issues are not fixable. Thankfully I’ve found a tool that can make it happen: it’s called Untrunc and the source code is available from here.

I’ve tried it out on Fedora and it worked great. I’m sure this can be compiled on Windows too, but I have no idea how to do that. Here’s how it worked for me on Fedora 37 though (the documentation leaves out a few steps). In principle we need to:

  • download and unZIP the latest version of untrunc (Code – Download ZIP)
  • download the latest version of libav
  • unZIP libav into the empty libav folder of untrunc
  • compile from source (see steps below)
  • run the command, specifying the broken and a working file from the same app/camera

Compiling untrunc

After extracting both untrunc and libav (which is not included in the untrunc repo), make sure to have development tools installed, then enter the untruc/libav directory and enter the following:

cd untrunc/libav

./configure
make
cd ..

Back in the untrunc directory, enter the following to compile the application:

g++ -o untrunc -I./libav-12.3 file.cpp main.cpp track.cpp atom.cpp codec_*.cpp codecstats.cpp codec.cpp mp4.cpp log.cpp -L./libav-12.3/libavformat -lavformat -L./libav-12.3/libavcodec -lavcodec -L./libav-12.3/libavresample -lavresample -L./libav-12.3/libavutil -lavutil -lpthread -lz

This will take some time. If you see error messages, it’s usually because a required package isn’t installed (like yasm). Install it, then try to compile again. When it’s finished, we’re ready to run the tool.

Using untrunc

Unlike other (less successful) tools, untrunc needs a working video from the same app or camera, as well as the broken file you’d like to fix. The magic in untrunc happens as it tries to find how the file is supposed to look, then make the necessary changes to a brand new fixed file (your original broken version will not be overwritten in this process). Other tools try to guess, usually get it wrong and you’ll end up with terrible sync issues or bad video.

Using untrunc is very simple:

untrunc working-file.mp4 broken-file.mp4 

When successful, this will create a “fixed-file.mp4” in the same directory as the source, or a different directory when specified. Untrunc does not add itself to the shell path, so you must either run it in its original directory and specify the full source/destination path for either file (or add untrunc to $PATH yourself).

That’s it! It works great to bring back OBS files that haven’t been closed after a power cut. Thanks to Federico Ponchio and all contributers who made this marvel possible.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.