Mp4fixer
Recover damaged/unfinished mp4 files with h264 video
Install / Use
/learn @bookkojot/Mp4fixerREADME
FIX YOUR MP4 FILE FOR FREE WITH PERL OPENSOURCE TOOL WET ULTIMATE SOLUTION
What it is?
This utility is designed to recover broken / deleted / unfinished files mp4 / mov / 3gp, inside of which was h264 video. In principle, it can work with AVI, MKV, M4V and other containers, but sound restoration can be harder.
If you shoot a video on a camera, phone, action camera, record video from the screen or from drones, then it is possible that this utility will be useful to you. If your drone crashes into a tree at high speed and you want to try to pull out the last half second of video, then this utility can also help. Sometimes from nothing you can get additional frames.
What is needed?
To restore the files you need:
- The broken files or images of the card / flash drive / disk where the video was recorded
- A new (SEPARATE) disk with sufficient free space.
- A machine running
LinuxorWindows,perl,ffmpeg/ffprobe/ffplay - To restore aac-sound, you need
faad2(libfaad-dev package) +gcc(to compileaacfixerutility) - Sample of good recorded file. The file must be written with the same settings on the same device as the corrupted file.
How to use it?
- If you have a deleted video / not finalized / there was an emergency shutdown, first get an image from it using the winhex utility or similar. You can also try to recover files, but you can lost few seconds. Linux users can point the card directly, like /dev/sdX, but having a backup when restoring data is always a good idea.
- Take the old video file or shoot a new video that will be recorded under the same conditions as the one being restored. Be sure to record it exactly under the same conditions, including even shaking on the screen (same bitrate), if it was - so we will find exactly what we are looking for. The length of the video is 20 seconds, but if you feed more - it's ok.
- You need to run:
perl fixer.pl <good_file.mp4> <bad_file.mp4> <output_prefix>
How it works?
To begin, intermediate files are created:
*-headers.aac- the file that you will need later to restore the sound*-headers.h264- the video file that will be used to create headers*-nals.txt- temporary file, with reference video packets. Under Windows 7, this file is created long for an unknown reason to me. This file is needed to "learn" the features of the encoding of the sample file*-nals-stat.txtis what we "learned". If something goes wrong - send this file.*-stat.mp4- temporary file, actually a copy of the sample file, only without sound, cropped up to 20 seconds.
At the very end of the work will be created:
*-out-video.h264- restored video*-out-audio.raw- the restored audio. To be precise - just what was between the video.
All files will be created in the current directory, from where you run the script.
You can take any player, for example ffplay and play *-out-video.h264, but with sound it will be a bit more complicated.
About restoring sound:
Different video cameras can write sound differently. At a minimum, it can be in different formats.
- If the sound was in mp3 format, then you can simply rename *-out-audio.raw to file.mp3 and open it with any player.
- If the sound was in PCM format (and its subspecies, such as ULAW), as some Sony video cameras do, then you can easily save it if you convert it to WAV by typing something like:
ffmpeg -f s16le -ar 48000 -ac 2 -i somefile-out-audio.raw -c copy output.wav
Of course, you will need to choose the parameters for your device. 3. If the audio was in AAC format, as the most popular version, then we compile the attached utility:
gcc aac.c -L. -lfaad -lm -o aacfixer
(assuming that you compiled the faad2 library and put the files libfaad.a and neaacdec.h in the current directory)
Now run:
./aacfixer somefile-headers.aac somefile-out-audio.raw
And after a while the files will appear:
<prefix>-pure.wav- is what was decoded<prefix>-pure-adts.aac- similarly, only without recoding
Bruteforce is used for recovering sound, so the operation can be slow.
If something went wrong:
Remember that this code was written for my internal needs in just 3 days and is not required to work in all cases. In addition to these three days, I spent another week writing this text, because if something does not work, then do not worry much. If you do not succeed, create an issue and do the following:
- Upload a reference file somewhere, lasting from 10 to 20 seconds. Use services like GoogleDrive or DropBox. Do not send it to the mail or to services where there are no direct links for download, such as mega.co.nz
- Upload sample of broken file, size not more than 50 megabytes
- Describe the model of the device on which this file was recorded or the name of the programs after which it was created. Was it restored or just copied from the device.
- What happened (the drone flew into a tree, free space ran out, an emergency shutdown)
- On which system did you try to run this utility and what went wrong
The code was written to work on as many platforms as possible, do not depend on binaries and be as simple as possible, so its effectiveness was not the goal. Again, I only spent 3 days writing this code.
Money money money
If for some reason you want to give me money, you can do it through:
- Bitcoin: 1bU17VMyvxYfCN257AiHrPPca1bszuLna
- Ether: 0x39b64f347b7702ddb1f8B06A25575598d624b783
Remember that I am unemployed and I really need money. If you make a money transfer, then I can use my data recovery superpowers and help you with a vengeance. However, the strength depends on the amount of money.
Users of the latest GoPro, Xiao / Xiaomi Yi 4k and some drones
These video cameras are built on Ambarella chipsets and have almost the same stuffing. For example, my Yi4k is built on the Ambarella A9SE chipset and like GoPro, it also writes an additional preview along with the main stream. This preview is used for the mobile application. Inside the firmware there are still rudiments of the video editor, it not visible to the user, probably it will be used in future versions. This leads to the fact that 2 different files are written simultaneously and are mixed on the card itself. And if it is restored as one piece, then at the output we get a lot of broken segments. Do not try to restore such data with regular data recovery software like R-Studio, they will be broken! If you want to try your luck, then take the whole image and experiment on it.
Fortunately, as the owner of such a video camera, I can experiment a lot with it and with different recording modes. And I'm an expert in file systems, so I'm going to release a utility for low-level work with a flash card. Below is a screenshot of this utility, one day it will unmistakably restore files. But not now. While I do not have enough motivation to complete the project, so let it be just a small preview.

Other tools:
I decided to look for other utilities and compete with them. Yes, it's been written more for fun, but if I can compete with the professional tools?
I wrote a primitive script that read random pieces from a file and then combined them together. You can also try it, it's called montage.pl, because it practically allows you to video editing in binary form! Of course, after such a "video editing", the files will be unusable, but is it possible to restore them?
To my great surprise, many commercial utilities could not do anything, they could not even explain what they not like, and open source, such as untrunc, required some effort and patching to simply compile them, but still did not work. But let's not talk about the bad. Then there will be those things that gave at least some result:
- mp4repair.org - data recovery service. Usually I ignore such services, but this service surprised me. It's not only able to show previews for the broken file, but did not even require a reference file for this, and everything was done right in the browser! However, the price is high. Unfortunately, they told me that it is better not to restore my test file, that it will be very expensive. This really disappointed me. I tried to correspond with technical support, but unfortunately, I did not achieve anything, so I can not recommend them.
- http://slydiman.me/eng/mmedia/recover_mp4.htm - the result is almost like mine, only in the mp4 container, which in theory, should better recover the synchronization of audio and video. I could not play the result in VLC, but ffplay showed me an almost similar picture that I have. However, the author does not post new versions and seems to have decided to monetize his project. I knew about this project before writing my own. Not open source, which was the reason for creating my project.
Unfortunately, as we see, the choice is not great. And with the publication of this project, the choice has become wider!
Contact the author
If for some reason you want to write to me personally, then write to the mail formp4review@airmail.cc This is not my primary email, so do not expect an early reply. You can create an issue with the text "check mail".
If your question concerns the production of video in general, then it is better to ask it here: https://video.stackexchange.com is a small but very good video production community
Greetings:
A huge thanks to the participants stackoverflow: szatmary, Mulvya, VC.One. Slydiman, for not implementing my feature requests and not publishing source code. And also to sufferers with SJCAM from our conference, which motivated me to do this!
License:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any lat
