Walk through how to repair corrupt video streams in Linux
- User
Walk through how to repair corrupt video streams in Linux
Assistant
To repair corrupt video streams in Linux, follow these steps, starting with the least invasive methods:
1. Analyze Corruption with
ffprobeIdentify where the corruption occurs using
ffprobe(part of FFmpeg):ffprobe -v error -select_streams v:0 -show_entries frame=pkt_pts_time -of csv=p=0 corrupted_video.mp4- Purpose: Lists timestamps of valid frames. Gaps indicate corruption.
2. Remux the Video with FFmpeg
Re-package the video into a new container without re-encoding:
ffmpeg -i corrupted_video.mp4 -c copy -f mp4 fixed_video.mp4- Use Case: Fixes minor container/header issues.
- Flags:
-c copy: Copies streams without re-encoding.-f mp4: Explicitly sets the output format.
3. Fix MP4 Metadata with
MP4BoxRepair MP4 structure (e.g., missing
moovatom):# Install GPAC tools sudo apt install gpac # Debian/Ubuntu # Attempt repair MP4Box -fix corrupted_video.mp4 -o fixed_video.mp4- Use Case: Structural issues like unplayable files due to missing headers.
4. Use
untruncfor Truncated FilesFix files cut short during download (requires a reference video):
# Install untrunc (compile from source or use a package) git clone https://github.com/ponchio/untrunc.git cd untrunc && make # Repair using a working reference file ./untrunc reference_video.mp4 corrupted_video.mp4 -o fixed_video.mp4- Use Case: Incomplete downloads or truncated files.
5. Re-encode the Video Stream
If corruption persists, re-encode to bypass damaged frames:
ffmpeg -i corrupted_video.mp4 -c:v libx264 -preset medium -crf 23 -c:a copy fixed_video.mp4- Flags:
-c:v libx264: Re-encodes video to H.264.-crf 23: Balances quality/size (lower = better).-c:a copy: Copies audio without re-encoding.
6. Extract and Rebuild Streams
If audio/video streams are partially intact:
# Extract audio ffmpeg -i corrupted_video.mp4 -vn -acodec copy audio.aac # Extract video (with error handling) ffmpeg -i corrupted_video.mp4 -c:v libx264 -vf "fps=30" video_fixed.mp4 # Merge streams ffmpeg -i video_fixed.mp4 -i audio.aac -c copy fixed_video.mp4- Use Case: When one stream (audio/video) is intact.
Additional Tips
- Transport Streams (TS): Use
-muxrateand-pcr_periodin FFmpeg for TS files. - Hex Editing: For advanced users, inspect headers with
hexdumpand manually fix atom positions (e.g., movemoovto the start). - GUI Tools: Try
avidemuxorHandBrakefor interactive repair.
Limitations
- Data Loss: Corruption may result in missing segments or artifacts.
- Codec-Specific Issues: Some codecs (e.g., HEVC) require specialized tools.
If these steps fail, the file may be irreparably damaged. Always keep backups of critical files.
- User
ffprobe produces
Invalid data found when processing input Assistant
If
ffprobereturnsInvalid data found when processing input, the file is likely severely corrupted or not a valid video stream. Here's how to diagnose and fix it:
Step 1: Confirm the File is a Video
Run
file corrupted_video.mp4to check if the file is actually a video:file corrupted_video.mp4- Expected Output: Should mention a video format (e.g.,
MP4,MPEG-4,AVC). - If Not: The file is not a video (e.g., incomplete download, wrong file).
Step 2: Check for Truncation or Headers
For MP4/MOV files, ensure the
moovatom (metadata) exists and is properly placed:# Install a hex editor sudo apt install hexedit # Debian/Ubuntu # Open the file in hex mode hexedit corrupted_video.mp4- Search for
6D6F6F76(hex for "moov"). If missing or near the end, the file is truncated.
Step 3: Fix with
ffmpeg(Even ifffprobeFails)Try remuxing or re-encoding:
ffmpeg -i corrupted_video.mp4 -c copy -f mp4 fixed_video.mp4- If this fails, force re-encoding:
ffmpeg -i corrupted_video.mp4 -c:v libx264 -c:a aac -strict experimental fixed_video.mp4
Step 4: Use
MP4Boxfor Structural RepairsIf the file is an MP4:
MP4Box -fix corrupted_video.mp4 -o fixed_video.mp4- If
MP4Boxalso fails, try rebuilding headers:MP4Box -add corrupted_video.mp4:force -new fixed_video.mp4
Step 5: Repair with
untrunc(If File is Truncated)If the file was cut off during download:
- Install
untrunc(compile from GitHub). - Repair using a reference file (a working video with the same codec/format):
./untrunc reference.mp4 corrupted_video.mp4 -o fixed_video.mp4
Step 6: Extract salvageable data
If the file is partially readable:
# Extract audio ffmpeg -i corrupted_video.mp4 -vn -acodec copy audio.aac # Extract video frames (skip errors) ffmpeg -i corrupted_video.mp4 -c:v libx264 -vf "fps=30" -max_delay 5000000 video_fixed.mp4 # Merge ffmpeg -i video_fixed.mp4 -i audio.aac -c copy fixed_video.mp4
Step 7: Advanced Recovery
For raw/fragmented streams (e.g., CCTV, dashcam):
# Use `foremost` to carve raw video data sudo apt install foremost foremost -t mp4 -i /path/to/corrupted_file- Recovers fragments into
/outputdirectory.
If All Else Fails
- Check for backups or redownload the file.
- Use data recovery tools like
testdiskorPhotoRecif the file was deleted.
Let me know if you need help with a specific error message or file type!
- Expected Output: Should mention a video format (e.g.,
- User
I asked for a walkthrough, meaning one step at a time
Assistant
Got it! Here's a step-by-step walkthrough to repair a corrupt video file in Linux, starting with the simplest checks and escalating to advanced fixes:
Step 1: Confirm the File is a Video
First, verify the file isn’t mislabeled or corrupted (e.g., a text file renamed as
.mp4):file your_video.mp4- Expected Output: Should mention a video format (e.g.,
MP4,MPEG-4,AVC video). - If Output is "data" or "ASCII": The file is not a video (e.g., incomplete download). Delete and redownload it.
Step 2: Check for Truncation (Incomplete Download)
If the file was interrupted during download (common cause of "Invalid data" errors):
# Check file size (compare to expected size) ls -lh your_video.mp4- If the size is much smaller than expected (e.g., 10MB instead of 1GB), the file is truncated. Proceed to Step 5.
Step 3: Fix Container/Header Issues with
ffmpeg(Remux)Re-package the video into a new container without re-encoding (fixes minor corruption):
ffmpeg -i your_video.mp4 -c copy -f mp4 fixed_video.mp4- Flags:
-c copy: Copies audio/video streams without altering them.-f mp4: Explicitly sets the output format (MP4).
- If this fails: Proceed to Step 4.
Step 4: Repair MP4 Metadata with
MP4BoxIf the file is an MP4/MOV and has missing
moovatom (metadata):# Install GPAC tools (if not installed) sudo apt install gpac # Debian/Ubuntu # OR sudo dnf install gpac # Fedora # Fix the MP4 structure MP4Box -fix your_video.mp4 -o fixed_video.mp4- If
MP4Boxfails: Try forcing a rebuild:MP4Box -add your_video.mp4:force -new fixed_video.mp4
Step 5: Fix Truncated Files with
untruncIf the file was cut off mid-download (common for large videos):
-
Install
untrunc(requires a working reference video of the same type):# Clone and compile (requires git and make) git clone https://github.com/ponchio/untrunc.git cd untrunc && make -
Repair using a reference file (a working video with the same codec/format):
./untrunc reference_video.mp4 your_video.mp4 -o fixed_video.mp4
Step 6: Re-encode the Video (Last Resort)
If the file is partially readable but has corrupted frames:
ffmpeg -i your_video.mp4 -c:v libx264 -preset medium -crf 23 -c:a aac -strict experimental fixed_video.mp4- Flags:
-c:v libx264: Re-encodes video to H.264 (widely compatible).-crf 23: Balances quality/size (lower = better quality).-c:a aac: Re-encodes audio to AAC (default for MP4).
Step 7: Extract Salvageable Data (Partial Corruption)
If only parts of the video/audio are corrupted:
# Extract audio (if audio is intact) ffmpeg -i your_video.mp4 -vn -acodec copy audio.aac # Extract video (skip errors, force 30 FPS) ffmpeg -i your_video.mp4 -c:v libx264 -vf "fps=30" -max_delay 5000000 video_fixed.mp4 # Merge the fixed streams ffmpeg -i video_fixed.mp4 -i audio.aac -c copy fixed_video.mp4Step 8: Advanced Recovery (Fragmented Files)
For raw/fragmented streams (e.g., dashcams, CCTV):
# Install `foremost` (recovers file fragments) sudo apt install foremost # Debian/Ubuntu # OR sudo dnf install foremost # Fedora # Carve the file for MP4 fragments foremost -t mp4 -i /path/to/your_video.mp4- Check the
/outputfolder for recovered fragments.
If All Steps Fail
- The file is likely irreparably corrupted.
- Check for backups or redownload the file.
Let me know if you hit a specific error at any step—I’ll help troubleshoot! 😊
- Expected Output: Should mention a video format (e.g.,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.