Repair Corrupted Video Files with FFmpeg?
In this article
Yes, FFmpeg can repair many corrupted video files, especially those with a broken index, incomplete download, or minor container errors. It works by re-muxing (re-packaging) the video and audio streams into a new, clean container, or by re-encoding the damaged parts. However, it cannot fix severe physical damage or missing data that was never recorded.
What Causes Video Corruption?
Video files become corrupted for several common reasons. Understanding the cause helps you decide whether FFmpeg can help. The most frequent culprits are:
- Interrupted recording or transfer: A camera or phone loses power, a USB cable is pulled out, or a download stops mid-way. The file ends abruptly, leaving the index (the part that tells players where each frame is) incomplete or missing.
- Container/Codec mismatch: The video stream is encoded with a codec (e.g., H.264) but the container (e.g., MP4) has metadata that is slightly wrong or the file extension doesn't match the actual format.
- Bitstream errors: Small sections of the video data are corrupted due to bad sectors on a hard drive, memory card errors, or network packet loss. These appear as glitches, blocky artifacts, or freezes.
- Header corruption: The first few bytes of the file (the header) are damaged, so the player doesn't recognize the file as a valid video at all.
How FFmpeg Repairs Files: Re-muxing vs. Re-encoding
FFmpeg has two main strategies for repair. The first, re-muxing, is fast and lossless. It reads the existing video and audio streams and writes them into a new container, rebuilding the index in the process. This fixes files that are structurally broken but whose actual video data is intact. The command is:
ffmpeg -i broken.mp4 -c copy repaired.mp4
The -c copy flag tells FFmpeg to copy the streams without re-encoding. This takes seconds and preserves quality perfectly. It works wonders for files from interrupted downloads or recordings where the data is all there but the index is missing.
The second strategy, re-encoding, is slower and loses some quality. It decodes the video and re-encodes it, which can skip or hide damaged frames. Use this when the video has actual bitstream errors that cause visible glitches. A basic re-encode command is:
ffmpeg -i broken.mp4 -c:v libx264 -crf 18 -c:a aac repaired.mp4
The -crf 18 is a quality setting (lower is better, 18 is visually lossless). This approach can sometimes “heal” a file that re-muxing cannot, because the decoder can handle minor errors and the encoder produces a clean stream.
How to Choose the Right Repair Method
Start with the fastest, lossless method first. If it works, you're done. If not, escalate. Here's a simple decision process:
- Try re-muxing: Run the
-c copycommand. If FFmpeg completes without errors and the output plays, you're done. - Check for errors: If FFmpeg reports errors like
Invalid dataorError while decoding, try re-encoding. - Try re-encoding with error resilience: Add
-err_detect ignore_errand-fflags +genptsto the re-encode command. These flags tell FFmpeg to ignore certain errors and generate missing timestamps. - If all else fails: The file may be too damaged. Try a dedicated repair tool or consider that the data is unrecoverable.
Common Problems and Their Solutions
Even with the right approach, you may hit specific issues. Here are a few common ones and how to handle them:
- “moov atom not found”: This is a classic MP4 error. The moov atom (index) is missing or at the end of the file. Use
-movflags +faststartwhen re-muxing to move the index to the front, which also makes the file stream better online. - Audio and video out of sync: If the audio drifts, try re-encoding both streams and adding
-async 1(for audio) and-vsync 1(for video) to force resync. This is a crude fix but often works for minor drift. - File plays but is truncated: If the video stops early, you can try to salvage the playable part by using the
-toption to specify a duration, but this is more about extracting a partial file than repairing. - Corrupted header: If FFmpeg can't even read the file, try using
ffmpeg -i broken.mp4 -c copy -map 0 output.mp4with the-map 0flag to force it to map all streams. Sometimes this bypasses header issues.
Safety and Limitations: What FFmpeg Can't Fix
FFmpeg is powerful but not magic. It cannot recover video data that was never written to disk. If a file is truncated (e.g., only 50% of the video was recorded), FFmpeg can only salvage the existing 50%. It also cannot fix files with severe physical damage (e.g., a scratched disc) or files that are encrypted or DRM-protected. Additionally, re-encoding always loses some quality, so it should be a last resort. Always work on a copy of the original file, never the original, to avoid making things worse.
Alternatives to FFmpeg
FFmpeg is a command-line tool, which can be intimidating. There are GUI alternatives that use FFmpeg under the hood, such as HandBrake (for re-encoding) or LosslessCut (for lossless cutting and re-muxing). For more advanced repair, commercial tools like Stellar Repair for Video or Wondershare Repairit offer automatic repair with a point-and-click interface. However, they are not free. For most users, FFmpeg is the best first choice because it's free, open-source, and extremely powerful once you learn a few commands.
Frequently Asked Questions
Can FFmpeg repair a video that won't play at all?
Sometimes, yes. If the file's index is corrupt but the data is intact, re-muxing with -c copy can create a playable file. If the header is severely damaged, FFmpeg may not even recognize the file, in which case you'd need a more aggressive tool.
Will re-encoding reduce video quality?
Yes, re-encoding always introduces some generational loss. Using a high-quality setting like -crf 18 minimizes visible loss, but it's not lossless. That's why you should try re-muxing first, which is completely lossless.
How do I know if my file is corrupted or just has a wrong extension?
Try renaming the file to a different extension (e.g., from .mp4 to .mov) and play it. If it plays, it was just a naming issue. If not, use FFmpeg to probe the file: ffmpeg -i file.mp4. If it shows the correct format and streams, it might be repairable.
Is there a way to repair a video without losing the audio?
Yes, re-muxing with -c copy preserves both audio and video streams exactly. Re-encoding also preserves both, but with quality loss. There's no method that repairs only video and keeps audio untouched unless you re-mux.
Can FFmpeg fix a video that has no sound but plays video?
If the audio stream is missing, FFmpeg can't create it from nothing. However, if the audio stream is present but corrupted, re-muxing might fix it. If not, you can extract the video and add a new audio track from another source.
What does the error “Invalid data found when processing input” mean?
This usually means the file header is unreadable or the file is not a valid video file at all. It could also mean the file is truncated. Try using ffmpeg -i to see if it can read any metadata; if not, the file may be beyond repair.