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 into a new container (possibly the same format) without re-encoding. This process repairs the file 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.
Not in the mood for reading? Watch the video instead.
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 detected errors:
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.mp4file. - Once finished, replaces the original video with the fixed version.
- Cleans up by removing the temporary file.
function ffmpeg-fix()
{
INPUT_VIDEO="$1"
# Check if input video is provided and not empty
if [[ -z "$INPUT_VIDEO" ]]; then
echo "Error: Input video file not specified" >&2
return 1
fi
# Check if input video file exists
if [[ ! -f "$INPUT_VIDEO" ]]; then
echo "Error: Input video file does not exist: $INPUT_VIDEO" >&2
return 1
fi
# Extract file extension from input video
EXTENSION="${INPUT_VIDEO##*.}"
TMP_DIR="$HOME/.tmp/ffmpeg"
mkdir -p "$HOME/.tmp/ffmpeg/"
TMP_FULL_PATH=$(mktemp "$TMP_DIR/tmp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
TMP_OUTPUT="$TMP_FULL_PATH.$EXTENSION"
if ffmpeg -err_detect ignore_err -i "$INPUT_VIDEO" -c copy "$TMP_OUTPUT"; then
mv "$TMP_OUTPUT" "$INPUT_VIDEO"
rm -f "$TMP_FULL_PATH"
else
echo "Error: ffmpeg process failed. Original video remains intact and unmodified." >&2
rm -f "$TMP_FULL_PATH"
return 1
fi
}
Once you’ve written the function, you can fix your broken video by simply running:
ffmpeg-fix "video.mp4"
If the fixing process finishes successfully, video.mp4 will be replaced with the fixed version.
If the process fails, the original video remains intact and unmodified.
This Bash function can also be downloaded from my GitHub repo.
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 them in the comments section below.
See you next time!


