Tuesday 26 December 2017

How to open and repair an m4v or mp4 video file?


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.



Answer



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.]


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...