72

Yesterday I was filming a scene with my phone but suddenly the application crashed and when I try to open the m4v generated it says that is corrupted.

I have downloaded the video files (around 36MB) on my PC and I am trying to open and repair it. Can you suggest me a way to do it?

FFMPEG info

As suggested in comments I try check what FFMPEG says me about that file with this command

ffmpeg.exe -i VID_20120425_144739.m4v -c:v copy -c:a copy out.mp4 2> log.txt

Here it is the result:

ffmpeg version N-40126-ga4b58fd Copyright (c) 2000-2012 the FFmpeg developers
  built on Apr 26 2012 03:29:50 with gcc 4.6.3
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      51. 47.100 / 51. 47.100
  libavcodec     54. 15.100 / 54. 15.100
  libavformat    54.  3.100 / 54.  3.100
  libavdevice    53.  4.100 / 53.  4.100
  libavfilter     2. 72.100 /  2. 72.100
  libswscale      2.  1.100 /  2.  1.100
  libswresample   0. 11.100 /  0. 11.100
  libpostproc    52.  0.100 / 52.  0.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 003fc940] moov atom not found
VID_20120425_144739.m4v: Invalid data found when processing input

Latest two lines are displayed in red.

sparrowt
  • 2,433
  • 1
  • 24
  • 23
Drake
  • 5,584
  • 14
  • 53
  • 75
  • My OS is Windows 7 but if necessary I can prepare a virtual machine with Linux-based distro. – Drake Apr 26 '12 at 12:29
  • @slhck Thank you for help, I did what you suggested and updated question. – Drake Apr 26 '12 at 12:42
  • 7
    Ah, that's what I expected. The MOOV atom is a piece of data that's needed to access the raw video streams inside the MP4/M4V container. There are plenty of "video repair" tools online, but I wouldn't trust most of them. You can certainly give them a shot though – just don't pay for anything. What looks promising is [Untrunk](http://vcg.isti.cnr.it/~ponchio/untrunc.php), which seems to be coded for this exact problem (namely a missing MOOV atom). Or [AtomicParsley](http://atomicparsley.sourceforge.net/). Good luck! – slhck Apr 26 '12 at 12:46
  • 1
    How do I fix a broken mp4 with AtomicParsley? – chovy Jul 08 '16 at 01:14
  • @Drake Ive just found [mp4fixer](https://github.com/bookkojot/mp4fixer), maybe you can give it a try. – SebMa Apr 22 '20 at 23:25

6 Answers6

68

It is possible to repair the broken mp4 or m4v file using Untrunc.

For this method you need another video file from the same device which isn't broken.

How to install untrunc

For compilation you need a Linux installation (ideally Ubuntu) and basic ability to use a command line. You can also skip this part and run untrunc via Docker (see the answer below), if you have that.

This is what to do:

  1. Install some pre-requisite libraries with this command:

    sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev

  2. Get the source code for Untrunc from the GitHub repo (choose one method):

    • wget https://github.com/ponchio/untrunc/archive/master.zip && unzip master.zip && cd untrunc-master
    • or git clone https://github.com/ponchio/untrunc.git && cd untrunc
  3. Compile the source code using this command (all one line):

    g++ -o untrunc file.cpp main.cpp track.cpp atom.cpp mp4.cpp -L/usr/local/lib -lavformat -lavcodec -lavutil

    (you can try skipping this step and using the ready-provided executable, but it didn't work for me)

  4. Then you can actually fix the video. You need both the broken video & an example working video.

How to fix the video

Find another working video, ideally from the same camera and preferably at least as long as the broken one; also the same resolution if possible (I believe this may help though is not essential).

Run this command in the folder where you have unzipped and compiled Untrunc but replace the /path/to/... bits with your 2 video files:

./untrunc /path/to/working-video.m4v /path/to/broken-video.m4v

Then it should churn away and hopefully produce a playable file called broken-video_fixed.m4v

That's it you're done!

VLC Media Player should now be able to play the file. However it may be reporting the wrong length information (Untrunc tries to guess/work this out, but doesn't always get it right). To fix this try re-encoding the video through another program.

[Thanks to slhck's comment for the suggestion to try Untrunc.]

[Many thanks to Federico Ponchio for coding Untrunc in the first place, to solve this exact problem.]

sparrowt
  • 2,433
  • 1
  • 24
  • 23
  • Cool, thanks for providing the full instructions here! – slhck May 16 '13 at 16:55
  • This worked for me, however I had to use the most up-to-date code from Github [here](https://github.com/ponchio/untrunc) instead of the link provided. – Alex Jan 28 '15 at 03:49
  • 3
    Thanks @Alex I've updated the link. Amusingly the Readme on github is based on this answer! – sparrowt Jan 30 '15 at 08:19
  • 2
    Would give a windows based solution. as I do not have internet access in my linux distros. – RogUE Jul 09 '15 at 01:01
  • Your method actually works. Thank you so much. :) – Juan Carlos Kuri Pinto Sep 06 '15 at 22:41
  • WORKS! Well done. – Erdinc Ay Dec 23 '15 at 08:13
  • 2
    FWIW this didn't produce any playable videos for me - but running [recover_mp4_to_h264](http://slydiman.me/eng/mmedia/recover_mp4.htm) via wine worked well. – The Compiler Jan 27 '16 at 12:32
  • From arch linux you can install untrunc from the Arch User Repository: https://aur.archlinux.org/packages/untrunc-git/ – Michael Kern Jun 08 '16 at 04:04
  • 1
    Untrunc worked for me, but I had to put both video files in the subdirectory of the hidden .untrunc folder named `untrunc-master`. Then, run: ./untrunc goodfile.mp4 badfile.mp4 It took about 3 minutes for a 2gb file and I only had to resync the audio. Everything else was fine. – whitewings Jul 09 '16 at 03:50
  • Thanks, this worked! Though I did have to `sudo apt-get install unzip g++` in the VM I created to run Untrunc in; looks like those commands aren't on Ubuntu by default. The audio in the repaired file is also way out of sync with the video, with the video seeming to speed up and slow down at random. I guess that's because I was recording using a variable frame rate... http://video.stackexchange.com/q/19052/16308 – Ajedi32 Aug 02 '16 at 15:04
  • Worked also for me. I first tried ignorantly a couple of `dd if=ok-file.mp4 of=/tmp/broken-file.mp4 bs=1056 count=1 conv=notrunc` with increasing block size, with no luck. Then untrunc solved my problem quickly and flawlessly. – Avio Aug 05 '16 at 13:23
  • 1
    You can use the Docker image here too: https://hub.docker.com/r/synctree/untrunc/ this way you don't need to install and compile all the things. A quick run and it solved it for me !! – abourget Sep 28 '17 at 17:18
9

You can install docker (available for all major operating systems) and run:

docker run -v $(pwd):/vol -it synctree/untrunc /vol/GOOD-reference-file.MP4 /vol/BAD-corrupt-file.mdt

The syntax is for Bash under Linux or macOS. Here, GOOD-reference-file.MP4 and BAD-corrupt-file.md5 must both be in your current directory, and the directory will be mounted to /vol in the Docker container.

Read the other comments around for more in-depth explanation.

This was the quickest for me!

slhck
  • 223,558
  • 70
  • 607
  • 592
abourget
  • 191
  • 1
  • 2
  • 1
    Thanks @slhck, the `Dockerfile` is available here if anyone wants to see what this will run before executing it on your computer: https://github.com/synctree/docker-untrunc There is also a `Dockerfile` in the actual `untrunc` repository now - I have not tested either or compared them in detail but mention it here for completeness: https://github.com/ponchio/untrunc/blob/master/Dockerfile – sparrowt Jul 23 '19 at 08:38
9

If someone else stumbles on this, I also tried the app sparrowt was talking about, but it didn't work for me (see some bug-reports I created in the developers repository). Maybe this was because it wasn't a mp4 file but a MOV file ...

What helped for me was this post: http://muzso.hu/2012/11/14/how-to-fix-a-broken-mp4-mov-video-ffmpeg-reports-moov-atom-not-found

They link to a program called "HD Video Repair Utility" (http://grauonline.de/cmsimple2_6/en/?Solutions:HD_Video_Repair_Utility), which is far cheaper than Treasured (http://aeroquartet.com/movierepair/repair). They even pointed out, that there was an earlier version of it (version 1.5) that didn't cost anything (http://nagasoft.cn/download/videorepair1.5.zip)!

I tried repairing the file using the version 1.5 and got it partly working. After re-encoding with ffmpeg (got some errors there), I realized, that a part of the audio was gone. In the logs (gladly the Video Repair Utility has some), there was no further mentioning of audio after a given time. I'll take it as-is ...

SimonSimCity
  • 191
  • 1
  • 3
7

@sparrowt Your current instructions did not work on my recent Ubuntu 16.10 installation. I got the following error while compilining untrunc:

track.cpp: In member function ‘int Codec::getLength(unsigned char*, int)’:
track.cpp:204:40: error: ‘avcodec_alloc_frame’ was not declared in this scope
   AVFrame *frame = avcodec_alloc_frame();

As explained in the untrunc repository, you might need to download libav separately rather than using the version in the Ubuntu repository. The following code worked for me:

sudo apt-get install unzip yasm g++ zlib1g-dev libbz2-dev
cd /tmp/
wget https://github.com/ponchio/untrunc/archive/master.zip
unzip master.zip
cd untrunc-master
wget http://libav.org/releases/libav-12.2.tar.xz
tar xvf libav-12.2.tar.xz
cd libav-12.2
./configure
make
cd ..
g++ -o untrunc file.cpp main.cpp track.cpp atom.cpp mp4.cpp -I./libav-12.2 -L./libav-12.2/libavformat -lavformat -L./libav-12.2/libavcodec -lavcodec -L./libav-12.2/libavresample -lavresample -L./libav-12.2/libavutil -lavutil -lpthread -lz -lbz2
./untrunc /path/to/working-video.mp4 /path/to/broken-video.mp4

This created a file called broken-video_fixed.mp4 that worked for me. Thank you so much for initiating this thread.

Giulio Genovese
  • 171
  • 1
  • 2
  • 1
    Thank you! On the untrunc compilation line I had a few errors along the lines of "undefined reference to `BZ2_bzDecompressInit'", I had to add -lbz2 – Andrew Smart Mar 11 '17 at 08:28
  • It can't handle large files. Just gives up. – Ken Sharp Apr 15 '17 at 21:01
  • Does your libav containt a `h264dec.h`? because `track.cpp` is trying to include it and i cant find it anywhere :/ – x29a Nov 17 '17 at 22:39
  • That dependency was added with commit https://github.com/ponchio/untrunc/commit/63254cacdd79d0a6b26373424a75c7459673d402#diff-cccc67908b2f1e13cf272e8122344d24 on October 10th, 2017. I believe now you have to donwload libav-12.2 rather than libav-0.8.7 – Giulio Genovese Nov 20 '17 at 17:00
  • Finally got this to work on Ubuntu 14.04.5 LTS. Had to run `sudo apt-get install yasm`, `sudo apt-get install libbz2-dev` and, to overcome an undefined reference to 'clock_gettime', add `-lrt` to the end of the `g++` command line (see https://stackoverflow.com/a/2418175/550712). It converted about 2/3 of my video. – Mark Berry Dec 29 '17 at 23:03
  • I had to add `-lX11 -lvdpau` too, to make it compile. – Tobia Feb 22 '19 at 11:53
4

In case untrunc doesn't support the codec inside your file, you can try to fix it with an hex editor:

  1. get another file with the same format, I got a bigger one but probably any length will work
  2. open both files in an hex editor
  3. replace the mdat section in the "ok" file with the mdat section in the "broken" file, without the header (i.e. copy only the bytes after 'mdat')
  4. it would probably already work like that, if not, the 4 bytes before 'mdat' specify the size of the mdat section, the current value will be the old size ("ok" file mdat section size), replace it with the new size ("broken" file mdat section size)

I ended up coding a simple program to do it: https://github.com/golimarrrr/fix-3gp

Both the program and by hand using the hex editor should create the same fixed file

golimar
  • 1,522
  • 1
  • 17
  • 35
  • Your instructions are not straightforward for someone who doesn't know what they're doing. Care to explain further? – RolandiXor Apr 15 '20 at 09:00
  • 1
    These instructions are not very clear. Maybe screenshots or something could work. – RolandiXor Apr 15 '20 at 11:42
  • 1
    For someone who doesn't know what they're doing the best option is to clone or download the linked fix-3gp.c file and compile it with `gcc` – golimar Apr 15 '20 at 13:43
-3

Have you tried to open the file with VLC Player? It is usually quite good at repairing damaged files.

Canadian Luke
  • 24,199
  • 39
  • 117
  • 171
Carlos Manta
  • 68
  • 1
  • 1
  • 3
  • 9
    I think this may apply to AVI files where the index can be built into memory, but a MP4 file an error of "moov atom not found" during ffmpeg processing does not play anything in VLC. – Sun Dec 26 '14 at 07:02
  • 2
    VLC fails to play it as well, in my case (.mov file) – rogerdpack Jan 21 '16 at 00:01