Repairing corrupt video streams in Linux can be a delicate process, but we'll go through it step by step. Here's a structured approach:
Step 1: Assess the Corruption
First, check if the file is partially playable or completely broken:
bash
ffplay -i corrupt_video.mp4
- If it plays partially, note where it fails.
- If it errors immediately, the header/metadata may be damaged.
Step 2: Attempt to Copy the Stream (Lossless)
Use ffmpeg to remux the video without re-encoding (fastest method):
bash
ffmpeg -i corrupt_video.mp4 -c copy -map 0 repaired.mp4
- If this works, the issue was minor (e.g., container corruption).
- If it fails, proceed to Step 3.
Step 3: Rebuild the Index (For AVI/MP4)
If the file is an AVI or MP4, try rebuilding the index:
bash
mencoder -forceidx -oac copy -ovc copy corrupt_video.avi -o repaired.avi
or for MP4:
bash
ffmpeg -i corrupt_video.mp4 -c copy -f mp4 -movflags +faststart repaired.mp4
Step 4: Extract Raw Streams (If Container Damaged)
If the container is corrupt, extract raw streams:
bash
ffmpeg -i corrupt_video.mp4 -map 0:v -c:v copy raw_video.h264 -map 0:a -c:a copy raw_audio.aac
Then repackage:
bash
ffmpeg -i raw_video.h264 -i raw_audio.aac -c copy final_repaired.mp4
Step 5: Re-encode (Last Resort)
If corruption persists, re-encode (quality loss):
bash
ffmpeg -i corrupt_video.mp4 -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k repaired.mp4
- Adjust
-crf (lower = better quality, 18–28 is typical).
Step 6: Advanced Tools (If Still Broken)
For severe corruption:
- Use
truncate to salvage parts (if file is truncated):
bash
truncate -s +1M corrupt_video.mp4 # Adds 1MB of null bytes (may help).
- Try
recover_mp4 (for MP4-specific fixes):
bash
recover_mp4 corrupt_video.mp4 -o repaired.mp4
Step 7: Verify the Output
Check the repaired file:
bash
ffplay repaired.mp4
Notes:
- Backup the original file before attempting repairs.
- For interleaved corruption (e.g., dropped frames), tools like
dvr-scan or avidemux may help manually cut bad sections.
Let me know if you hit a specific error, and we can refine the approach!