Fix Corrupted MP4 File Header with FFprobe

A corrupted MP4 file header prevents media players from reading audio and video streams, rendering the entire video unplayable. This guide explains how to diagnose corrupted MP4 headers using ffprobe, extract critical stream parameters from healthy reference files, and rebuild the missing container structure using FFmpeg tools to restore your video.


Understanding the Role of FFprobe

While ffprobe is a diagnostic and stream-analysis tool rather than a file editor, it is critical for repairing damaged headers. An MP4 header relies on index data—primarily the moov atom—which contains the codec details, timestamps, and layout of the audio and video streams.

You use ffprobe to:

  1. Identify the exact failure point in the damaged file.
  2. Analyze a working reference file recorded on the same device to extract missing container parameters.
  3. Validate the repaired file to ensure the new header is compliant and readable.

Step 1: Diagnose the Corrupted MP4 Header

Run ffprobe against your damaged file to determine whether the header is missing, misplaced, or completely unreadable.

ffprobe -v error -show_format -show_streams corrupted.mp4
  • Output Analysis:
    • "moov atom not found": The video recording was interrupted before the index header was written to the disk.
    • "Invalid data found when processing input": The header exists but contains corrupted metadata bytes.

Step 2: Analyze a Working Reference File

If the moov atom is missing, you must supply the container structure from a healthy video recorded with the exact same device, app, resolution, and frame rate.

Use ffprobe to inspect the technical parameters of the reference file:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,width,height,pix_fmt,r_frame_rate reference.mp4

Note the output values for:

  • codec_name (e.g., h264, hevc)
  • width and height (e.g., 1920x1080)
  • pix_fmt (e.g., yuv420p)

Step 3: Repair the Corrupted Header

Depending on the diagnosis from Step 1, use one of the following methods to rebuild the header.

Method A: Relocate and Rebuild the Header (Minor Corruption)

If ffprobe detects stream data but fails on random access, the header may simply be located at the end of the file or contain fragmented indices. Re-mux the file using FFmpeg with the faststart flag:

ffmpeg -err_detect ignore_err -i corrupted.mp4 -c copy -movflags faststart repaired.mp4
  • -err_detect ignore_err: Forces the demuxer to bypass minor syntax errors in the header.
  • -c copy: Copies the raw video and audio streams without re-encoding, preserving 100% quality.
  • -movflags faststart: Moves the moov atom to the very beginning of the MP4 file.

Method B: Reconstruct a Missing moov Atom

If ffprobe returns moov atom not found, the file contains raw media packets without an index. Use the open-source utility untrunc alongside ffprobe to graft the reference header onto the broken stream:

untrunc reference.mp4 corrupted.mp4

untrunc uses the stream layout analyzed from reference.mp4 to parse the raw byte stream inside corrupted.mp4 and synthesize a new, fully indexed header.


Step 4: Verify the Repaired Header with FFprobe

Once the repair process completes, verify that the MP4 header is valid and that both streams are correctly recognized:

ffprobe -v error -show_entries format=duration,size,bit_rate -show_streams repaired.mp4

If the command executes without printing errors and displays the duration, video dimensions, and audio tracks, the header has been successfully restored.