61

It happened me many times that I could end up in corrupted video files:

  • video downloaded from official sources but with torrent and for some reason not all the pieces are there in .mkv, .ts or .mp4
  • records in .ts format where at a certain point the data is missing
  • copied videos from an old hard drive with damaged sectors

When I play this videos with VLC or other players they plays till a certain point and then instantly exits. Sometimes I can try to skip the bad part but I could meet another bad sector and have an insta-quit again.

Question is:

Can I fix this problems (I suppose stream and keyframes related) with ffmpeg and maybe in a -c copy fashion so I haven't to recompress everything?

What I want to achieve is a full lenght video where the missing parts are skipped, I prefer a glitch rather than having my videos crash.

1

3 Answers 3

67

If parts of the file reside on physically bad sectors, or for whatever reason, the OS cannot serve the whole file to FFmpeg, then naturally FFmpeg can't do anything about that. You should get a utility which can ignore those portions and write the salvageable parts to a new file, like ddrescue.

Now, if the file protocol is not the cause of errors, you can try

ffmpeg -err_detect ignore_err -i video.mkv -c copy video_fixed.mkv
8
  • 1
    This works, but fails without -c copy. Surprising that it doesn't work during recode.
    – intuited
    Commented May 26, 2017 at 18:52
  • 3
    Even with that, I'm still getting Invalid data found when processing input -- and ffmpeg gives up.
    – Mikhail T.
    Commented Apr 13, 2019 at 18:16
  • Nice solution... if the moov atom (on a MP4 file) is still present somewhere. What if it's not even there? Commented Oct 22, 2019 at 16:52
  • 6
    Not worked: moov atom not found video.mp4: Invalid data found when processing input
    – 57ar7up
    Commented Jun 25, 2022 at 5:35
  • 1
    @aggregate1166877 it creates a new file, so it can't destroy anything...
    – user924
    Commented Jun 6 at 8:30
7

In the search of a better answer a possible way could be:

ffmpeg -i video.mkv -c copy video_fixed.mkv

However I'm pretty sure there are some extra options that can make the result even better..

5
  • 1
    Works for me every time I have some kind of issue with the meta data, timestamps, or container info. And it's very fast, usually faster than 400x playspeed.
    – user3643
    Commented Jun 16, 2018 at 0:03
  • This command saved my butt, to be frankly.
    – neverMind9
    Commented Aug 3, 2018 at 19:52
  • What does it even do it looks like a copy | paste to me? Commented Feb 24 at 22:33
  • @RicardoBohner do you mean the answer or the command? in case of the answer it was originally posted by me 1 day before the selected one, regarding the command instead it does a copy but also reading the data and checking for integrity fixes the stream. Commented Feb 27 at 22:44
  • I meant the command, and I didn't know that ffmpeg did integrity fixes when using the copy codec...thanks for clarification. Commented Feb 28 at 0:39
3

I've tested lot of solutions and software. I love cli commands, so using ffmpeg you can fix your video file with no hassle. Try the next command if you came here searching some useful fix:

ffmpeg -vcodec mpeg4 -b:v 7561k -qscale:v 2 -acodec aac -ac 2 -async 1 -strict experimental ./video_fixed.mp4 -threads 0 -i damage_file.mp4

Good luck 😃!

3
  • 1
    This will probably work, but the file will be HUGE! Better so just use H264 again, like: ffmpeg.exe -i "input.mp4" -c:v libx264 -crf 22 -preset slow output.mp4
    – kett
    Commented Sep 27, 2020 at 22:40
  • this is the only thing that worked. Can you please explain the flags? why does it work and how can it be optimized
    – AK_
    Commented Nov 29, 2022 at 17:34
  • I had a video which I composed of 3 separate videos with -f concat and -safe 0, however, while the result played in VLC, in Davinci I could only see the video up to the first merge point (audio was still fine, though). After that Davinci showed a red media offline message. I first tried simply ffmpeg -c copy to fix the video but that did not work, however this re-encoding worked.
    – Henno
    Commented Dec 7, 2022 at 15:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.