How to fix a video with wrong/corrupted duration using ffmpeg

Hojjat
2 min readMay 21, 2018

--

Recently, I received a video captured by a Logitech webcam that was around an hour long, but I was not able to play it in any video player. Windows said that the file length is: 512409374:29:18 long!

I tried some tricks to fix it. I tried to re encode it in Handbrake, or use ffmpeg to convert it to something else. But nothing worked. Here is my solution.

  • Convert the video to still images
  • Convert the still images back to a video

Here are my steps:

First we need to extract all the frames into JPEG images. At least that’s how I did it.

.\ffmpeg.exe -i ‘.\Video 36.wmv’ -an -f image2 filename%03d.jpg

In my case 99,739 images were created. Remember this number. Then we need to extract the audio.

.\ffmpeg.exe -i ‘.\Video 36.wmv’ audio.mp3

Then we figure out the exact duration of the audio.

.\ffprobe.exe -i .\audio.mp3 -show_entries format=duration -v quiet -print_format json

In my case the output is:

{
"format": {
"duration": "3323.603000"
}
}

Now we need to calculate the frame rate of the output video so that the audio and the video match. To do that we need to devide 99739 by 3323.603 which will give us: 30.0093. This is the frame rate for our output video. It is important to use the exact number.

.\ffmpeg.exe -framerate 30.0093 -f image2 -i .\filename%3d.jpg -codec copy ouput_noaudio.mkv

Now, we need to mix the audio and the video together and create the final video:

.\ffmpeg.exe -i .\output_noaudio.mkv -i .\audio.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mkv

--

--

Hojjat

Publishing how I fixed some software/AI/PhD/Robotics/Linux related stuff…