How to Check a Video File for Corruption with FFmpeg
To check whether a video file is corrupted, run ffmpeg -v error -i video -f null - to decode the whole file and confirm that not a single error line is printed. This catches files from downloads or copies that stopped partway, and videos that freeze or show block noise during playback, without having to watch them to find out.
In my test environment, I made three kinds of broken files from a normal MP4 and checked how each one looked to ffprobe and to ffmpeg.
The test environment was Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). The original file, good.mp4, is a 320×240, 30 fps, 10-second MP4 with H.264 video and AAC audio (300 frames), made with lavfi's testsrc2 and sine. Keyframes are every 2 seconds. I made the broken files with Python. trunc.mp4 keeps only the first half of good.mp4. trunc_fs.mp4 keeps only the first half of good_fs.mp4, which was re-created with -movflags +faststart. damaged.mp4 is the faststart file with 10,000 bytes in the middle overwritten with random data. I did not test MKV, WebM, or other container formats, codecs other than H.264 and AAC, or files that were actually corrupted by a recording or a transfer.
ffprobe alone does not catch corruption in the middle
First, I used ffprobe to show the duration of each of the four files.
ffprobe -v error -show_entries format=duration -of default=nw=1 trunc.mp4
| File | ffprobe result | Exit code |
|---|---|---|
| good.mp4 | duration=10.000000 |
0 |
| trunc.mp4 | moov atom not found |
1 |
| trunc_fs.mp4 | duration=10.000000 |
0 |
| damaged.mp4 | duration=10.000000 |
0 |
Only trunc.mp4 produced an error.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x…] moov atom not found
trunc.mp4: Invalid data found when processing input
@ 0x… replaces a memory address that changes on every run. In this output I also removed the name of the working folder that came before the filename on the second line.
The FFmpeg documentation on writing MP4 files says that an MP4 keeps the information about all packets in one place, usually written at the end of the file. The same section says that a normal MP4 cannot be decoded until writing has fully finished. In good.mp4, that information is in a part called moov, which is at the end of the file. trunc.mp4, which keeps only the first half, lost moov, so it could not be opened at all.
On the other hand, the file where +faststart moved moov to the front still showed the original 10 seconds in ffprobe, even without its second half. damaged.mp4, whose contents I overwrote, behaved the same way. A duration shown by ffprobe does not mean the file can be decoded to the end.
Decode to the end and check for errors
To check the contents, decode the whole file with ffmpeg. -f null - tells it to discard the decoded output instead of writing it anywhere (null format). -v error shows only errors (log level documentation says error shows all errors, including recoverable ones).
ffmpeg -v error -i damaged.mp4 -f null -
For the good good.mp4, nothing was printed. All three broken files produced errors.
| File | Error lines | Exit code |
|---|---|---|
| good.mp4 | 0 | 0 |
| trunc.mp4 | 4 | 183 |
| trunc_fs.mp4 | 6 | 0 |
| damaged.mp4 | 51 | 0 |
For trunc_fs.mp4, FFmpeg reported partial file, which means the file ends before it should.
[NULL @ 0x…] Invalid NAL unit size (1095 > 948).
[NULL @ 0x…] missing picture in access unit with size 952
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x…] stream 1, offset 0x434c1: partial file
[h264 @ 0x…] Invalid NAL unit size (1095 > 948).
[h264 @ 0x…] Error splitting the input into NAL units.
[vist#0:0/h264 @ 0x…] Decoding error: Invalid data found when processing input
For damaged.mp4, errors came from both the video (h264) and the audio (aac). These are the first 8 lines.
[NULL @ 0x…] Invalid NAL unit size (1536810320 > 1753).
[NULL @ 0x…] missing picture in access unit with size 1757
[aac @ 0x…] channel element 2.1 is not allocated
[aist#0:1/aac @ 0x…] Error submitting packet to decoder: Invalid data found when processing input
[h264 @ 0x…] Invalid NAL unit size (1536810320 > 1753).
[h264 @ 0x…] Error splitting the input into NAL units.
[NULL @ 0x…] Invalid NAL unit size (638368895 > 1527).
[NULL @ 0x…] missing picture in access unit with size 1531
Even when errors appeared during decoding, the ffmpeg exit code was 0. trunc_fs.mp4 and damaged.mp4 both exited with 0. Only trunc.mp4, which could not be opened, exited with 183. If you check files in a script, decide based on whether the error output is empty, not on the exit code.
Adding -xerror does not make the exit code reliable
ffmpeg has an option, -xerror, that stops and exits when an error occurs (official documentation says "Stop and exit on error").
ffmpeg -v error -xerror -i damaged.mp4 -f null -
With -xerror, both trunc_fs.mp4 and damaged.mp4 stopped decoding partway. trunc_fs.mp4 stopped at frame 138 (4.71 seconds) with exit code 183. damaged.mp4 stopped at frame 128 (4.23 seconds), but its exit code stayed 0. I ran each file three times, and the frame where decoding stopped and the exit code were the same every time.
If you only want to know whether a file is broken, -xerror lets you stop early. However, in FFmpeg 6.1.1 the exit code still could not be used to tell whether the file was broken.
Check for truncation by counting frames
Adding -count_frames to ffprobe counts the frames in each stream (ffprobe options). The count appears in nb_read_frames. Comparing it with nb_frames, which is shown normally, shows how many frames are missing.
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_frames,nb_read_frames -of default=nw=1 trunc_fs.mp4
| File | nb_frames | nb_read_frames | Timestamp of the last readable frame |
|---|---|---|---|
| good.mp4 | 300 | 300 | 9.97 seconds |
| trunc_fs.mp4 | 300 | 144 | 4.87 seconds |
| damaged.mp4 | 300 | 294 | 9.97 seconds |
trunc_fs.mp4, which keeps only the first half, could read only 144 of its 300 frames, and it ended before reaching 5 seconds. damaged.mp4 could be read to the end, but 6 frames were missing.
The timestamps come from -show_entries frame=pts_time for all frames, looking at the last line.
If you have the original, framemd5 shows the damaged range
If you have a known-good copy of the file, you can compare per-frame MD5 hashes made with framemd5. This tells you exactly where the damage starts and ends. I used -map 0:v to select only the video.
ffmpeg -v quiet -i good.mp4 -map 0:v -f framemd5 good.txt
ffmpeg -v quiet -i damaged.mp4 -map 0:v -f framemd5 damaged.txt
I compared the two files with a Python script. The frames that did not match were frames 129 to 179 (4.30 to 5.97 seconds), 51 frames in total. Six of those frames were missing from damaged.mp4 because they could not be decoded.
The keyframes in good.mp4 are at 0, 2, 4, 6, and 8 seconds. The damaged range began after the keyframe at 4 seconds, and from the next keyframe at 6.00 seconds the frames matched the original again.

When I extracted and compared the frame at 5.00 seconds, the damaged file showed block corruption around the diagonal band and down the lower half of the screen. ffmpeg printed errors but kept decoding, and it wrote out these broken frames as well.
Checking many files at once
To check every video in a folder, I wrote a script that decodes each file one at a time and prints OK or NG depending on whether the error output is empty. -nostdin stops ffmpeg from accepting keyboard input from standard input (-stdin and -nostdin). I added it because the script runs ffmpeg non-interactively.
#!/bin/bash
# Decode each video argument to the end and report whether any errors were printed
for f in "$@"; do
err=$(ffmpeg -nostdin -v error -i "$f" -f null - 2>&1)
if [ -z "$err" ]; then
echo "OK $f"
else
echo "NG $f(エラー $(printf '%s\n' "$err" | wc -l) 行)"
fi
done
$ bash check.sh src/*.mp4
NG src/damaged.mp4(エラー 51 行)
OK src/good.mp4
OK src/good_fs.mp4
NG src/trunc.mp4(エラー 4 行)
NG src/trunc_fs.mp4(エラー 6 行)
The three broken files were marked NG. The two normal files, which differ only in whether they use faststart, were marked OK.
The number of error lines is only a rough measure of how badly a file is damaged. When the same message repeats, FFmpeg combines it into a single line, such as Last message repeated 2 times (logging documentation says that adding repeat to the log level, as in -loglevel repeat+error, prints every message without combining them). One such line appeared among the 51 lines for damaged.mp4.
When you check many long videos, adding -xerror lets you stop decoding a broken file early.


