How to Fix a Corrupted Video with FFmpeg

Learn how to repair broken or corrupted videos using the ffmpeg command line tool.

Sometimes, after recording a video, downloading one online, or saving a video stream to your storage, the file may end up corrupted, incomplete, or broken. While players like VLC might still play it, some videos refuse to play certain parts or won’t play at all.

Fortunately, you can often fix this with ffmpeg.

‼️ WARNING: Some video files may be damaged beyond repair, and this method might not always work.

The solution is to “mux” the video, which means rewriting the streams to a new container (possibly the same format) without re-encoding. This process repairs the structure and creates a new index. Any missing data will still be missing, but video players will skip over damaged parts instead of stopping or crashing.

To fix a video by muxing, use this simple command:

ffmpeg -i "video-broken.mp4" -c copy "video-fixed.mp4"

Alternatively, you can instruct ffmpeg to ignore errors it detects:

ffmpeg -err_detect ignore_err -i "video-broken.mp4" -c copy "video-fixed.mp4"

This process can be tedious, as you still need to manually remove the broken video file after fixing it. To simplify things, I created a bash function.

Here’s how the function works:

  • Takes the broken video file as its argument.
  • Fixes the video with ffmpeg, saving the output to a temporary .mp4 file.
  • Once finished, replaces the original video with the fixed version.
  • Cleans up by removing the temporary file.
function ffmpeg-fix()
{
    INPUT_VIDEO="$1"
    TMP_DIR="$HOME/.tmp/ffmpeg"

    mkdir -p "$HOME/.tmp/ffmpeg/"
    TMP_FULL_PATH=$(mktemp "$TMP_DIR/tmp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    TMP_OUTPUT="$TMP_FULL_PATH.mp4"

    ffmpeg -err_detect ignore_err -i "$INPUT_VIDEO" -c copy "$TMP_OUTPUT"

    mv "$TMP_OUTPUT" "$INPUT_VIDEO"
    rm "$TMP_FULL_PATH"
}

Once you have written the function, you can fix your broken video by simply executing:

ffmpeg-fix "video.mp4"

I also made a video to demonstrate how I fixed my broken video.

Conclusion

That’s all for today. Hopefully, this trick helps you repair your corrupted video collection.

If you have a better alternative, encounter another issue, or have any questions, feel free to discuss in the comments section below.

See you next time!

References