How can I check the integrity of a video file (avi, mpeg, mp4...)?

130

68

This title could be somewhat misleading, so let me explain ...

I'm downloading a video file ... mpeg, avi - being one of the popular formats. Now, if I am downloading it, and the download breaks in the middle of the uhm ... download, then, for example, Windows Media Player will give out some error and refuse to play it (although the file is, let's say, 98% complete). But, players like KMPlayer, or MediaPlayer Classic will play it up until that point (as the matter of fact, they can play it while it is being downloaded as well).

So, I'm interested, ... without using any means of download (download managers and alike) to secure the file is completely downloaded, how can one verify whether the video file is downloaded whole, and that it is complete ?

Rook

Posted 2010-01-25T12:54:46.033

Reputation: 21 622

Incomplete MPEG should play with no problems. AVI has indexes for quick seeking at the end of the file, which some players will use and some don't require or will ignore. – bobince – 2010-01-25T13:37:52.860

7Incomplete MPEG should play with no problems. @bobince, the question was/is how to check for corruption, not how to play an incomplete file. Even if a format supports playing incomplete files, it should still be possible to check for corruption if the file does not conform to the format specifications. For example, you can still read an incomplete plain-text file, but you can still detect if the file has problems like if it ends in the middle of a sentence or there is a chunk of obviously missing information in the middle. – Synetech – 2014-01-03T18:05:32.213

Answers

137

You can use a feature in ffmpeg video converter: if you will specify it to recode video to nothing it will just read input file and report any errors that will appear. This is very fast process because video frames are just being read, checked and silently dropped.

Example command line: (for Linux)

ffmpeg -v error -i file.avi -f null - 2>error.log

-v error means a certain level of verbosity (to show some errors that are normally hidden because they don't affect playability a much).

You will get a full error log with some generic information about file ffmpeg will output, so this will probably require your attention, through filters can be written to perform batch check of similar files.

FFmpeg is also available for Windows here. The command line will be almost identical with an exception of stderr redirect:

ffmpeg.exe -v error -i file.avi -f null - >error.log 2>&1

whitequark

Posted 2010-01-25T12:54:46.033

Reputation: 14 146

1Seems to take about as long as watching the movie unfortunately, but seems like it works – MattPark – 2015-03-28T03:27:09.110

Example error from file with the end chopped off: [mov,mp4,m4a,3gp,3g2,mj2 @ 0x161a180] moov atom not found video-head.mp4: Invalid data found when processing input – StackAbstraction – 2015-09-07T23:09:02.190

3If, as the OP states, "download breaks in the middle of the uhm ... download", it's very likely that all streams will be disrupted, so you can detect this error by restricting decoding to just one audio track (using -map), which will significantly speed up operation: ffmpeg -v error -i in.mkv -map 0:1 -f null … If there is an error, it'll log the byte position, which you can use to truncate the file (a megabyte or two earlier to be safe) and then resume downloading. – Lumi – 2015-10-25T10:49:04.043

adding the -map 0:1 is way faster, but results in a lot of Output file #0 does not contain any stream – MattPark – 2017-03-13T02:15:52.013

5It took maybe 4 1/2 minutes for me to test a 1GB 1hour long .mkv file, on an oldish hex core processor. All cores were maxed during the test. I hex edited a copy of the file and added some blocks of 00 to simulate damage. The error log reported the errors correctly. Files without errors returned an empty error.log – TripleAntigen – 2017-09-14T00:08:37.487

How can I include the file name in the error msg? I get e.g. this: [dca @ 0x561d03371a00] Packet too short for EXSS frame [dca @ 0x561d03371a00] No valid DCA sub-stream found – kerner1000 – 2019-02-10T12:48:11.650

@mente How are you achieving this with ffprobe? – Mark Walsh – 2019-10-03T11:22:08.833

5Same speed as if it would been converting. – Somebody – 2013-03-26T00:17:39.470

9Another option is ffprobe that comes in package with ffmpeg. It doesn't do any conversion but simply reads metadata info from file. Therefore it will detect errors in metadata but won't find any problems within file itself. Personally using ffprobe to make fast verification of uploaded before processing further – mente – 2013-10-01T07:41:29.643

14

I liked idea of using ffmpeg -f null above, but I'd actually like to automate process of using that output. In particular, common scenario I have with my music video collection is that I have few clips which have same resolution, and I'd like to diff verification logs for those files to remove ones broken the most.

Unfortunately, ffmpeg so far doesn't have a way to disable its interactive mode, which outputs noise for this case of usage. I ended up hacking simple wrapper script to do filtering:

#!/usr/bin/env python
import sys
import os
import re

t = os.popen('ffmpeg -v 5 -i "%s" -f null - 2>&1' % sys.argv[1]).read()
t = re.sub(r"frame=.+?\r", "", t)
t = re.sub(r"\[(.+?) @ 0x.+?\]", "[\\1]", t)
print t

Example output:

[mpeg1video]ac-tex damaged at 21 17
[mpeg1video]Warning MVs not available
[mpeg1video]concealing 22 DC, 22 AC, 22 MV errors
[mpeg1video]Warning MVs not available
[mpeg1video]concealing 22 DC, 22 AC, 22 MV errors
[mpeg1video]ac-tex damaged at 13 9

pfalcon

Posted 2010-01-25T12:54:46.033

Reputation: 644

1if you make it so that it replaces broken parts with good parts, that would be even better – akostadinov – 2017-01-09T09:18:32.473

I'm getting an error using this : – Akshit Rewari – 2017-07-16T14:54:14.570

2Traceback (most recent call last): File "F:\filecheck.py", line 6, in <module> t = os.popen('ffmpeg -v 5 -i "%s" -f null - 2>&1' % sys.argv[1]).read() IndexError: list index out of range – Akshit Rewari – 2017-07-16T14:54:19.517

list index out of range means no file name was supplied at the command line. So the proper usage would be e.g. script.py filename.mp4 or perhaps python script.py filename.mp4. – Anaksunaman – 2020-01-17T20:10:59.367

1

For windows, You can 'batch' check integrity for videos on current folder and all subfolders with this bat file:

checkvideo.bat

@echo off

set "filtro=%1"
if [%filtro%]==[] (
    set "filtro=*.mp4"
    )

for /R %%a in (%filtro%) do call :doWork "%%a"

    PAUSE
    exit /B

:doWork
    C:\ffmpeg\bin\ffmpeg.exe -v error -i %1 -f null - > "%~1.log" 2>&1

Use:

checkvideo.bat [filter]

If you don't give one filter, will get '*.mp4'.

Samples: checkvideo.bat checkvideo.bat *.avi

Setup: Download FFmpeg for Windows from here: https://ffmpeg.zeranoe.com/builds/ and unzip them Change C:\ffmpeg\bin\ in the bat file for the path where you have unzipped ffmpeg Put checkvideo.bat on a folder included in the Path or add his folder to Path environment variable

Juan Antonio Tubío

Posted 2010-01-25T12:54:46.033

Reputation: 251

Juan Antonio Tubío's answer is working for me to check video integrity. But how can I read log file and detect time where video is corrupted. Log example. [h264 @ 00000173649787c0] cabac decode of qscale diff failed at 64 7 [h264 @ 00000173649787c0] error while decoding MB 64 7, bytestream 38638 – J.Duck – 2018-03-20T14:36:01.443

0

MediaInfo is a great tool for getting info about any video file you care to throw at it. This may be able to highlight the info you want.

Another tool is GSpot but it hasn't been updated since 2007.

Try giving each one a known good and known bad file and compare the results.

I used to use GSpot until it stopped being updated, then switched to MediaInfo

Shevek

Posted 2010-01-25T12:54:46.033

Reputation: 15 408

1I have some old version of GSpot on my machine ... but it is a tool for something completely different (determining the code/decode...). It has nothing to do with this. I'll check out mediainfo. – Rook – 2010-01-25T13:18:23.980

GSpot will report if file size or frame count do not match the expected from the index (somewhere on the left hand side, it's been a while since I used it!), also later versions added much functionality. – Shevek – 2010-01-25T14:45:18.383

mediainfo can recursively scan directories: to get a report of all files at any level starting from the current folder simply run mediainfo *. – ccpizza – 2016-06-12T22:00:44.353

3mediainfo apparently does not check for completeness. I took a 33 MB webm video file and truncated it to the half. mediainfo still had nearly the same output for both, no apparent sign of corruption. After truncateing to 1 MB there still was no clear sign of corruption in the output. Only if you had a deeper look at the output, there was some contradictory information. But these parts depend on the format of the video file, so you cannot automate this detection. – Tino – 2016-12-19T23:54:27.127

4mediainfo does not read anything but metadata. This will not check the validity of your file in any way whatsoever. – DanielSmedegaardBuus – 2017-12-08T14:13:47.730

0

The issue with the other answer using ffmpeg to recode to null format is that it takes really a long time. Especially, if you want to check multiple files in a directory.

A quick way would be to generate thumbnails for all the videos, and see where thumbnail generation fails.

find . -iname "*.mp4" | while read -r line; do 
  line=`echo "$line" | sed -r 's/^\W+//g'`; 
  echo 'HERE IT IS ==>' "$line"; 
  if ffmpeg -i "$line" -t 2 -r 0.5 %d.jpg; 
    then echo "DONE for" "$line"; 
  else echo "FAILED for" "$line" >>error.log; 
  fi; 
done;

This method turned out to be much FASTER than other methods.

However, there is a CAVEAT. This method can yield wrong results, because sometimes thumbnail can be generated even for corrupt files. E.g. if the video file is corrupted only at the end, this method will fail.

shivams

Posted 2010-01-25T12:54:46.033

Reputation: 1 269

0

Easier version

for file in *.mp4; do ffmpeg -v error -i "$file" -f null - >error.log 2>&1; print "$file"; done

This will print the file name as they are being processed.

error.log will contain the errors.

SpaceDog

Posted 2010-01-25T12:54:46.033

Reputation: 1 318

0

fluent-ffmpeg

var ffmpeg = require('fluent-ffmpeg');
var ff = new ffmpeg();

ff.on('start', function(commandLine) {
  // on start, you can verify the command line to be used
})
.on('progress', function(data) {
  // do something with progress data if you like
})
.on('end', function() {
  // do something when complete
})
.on('error', function(err, stdout, stderr) {
  // handle error conditions
  console.log('Error: ' + err.message)
  console.log('ffmpeg output:\n' + stdout)
  console.log('ffmpeg stderr:\n' + stderr)
})
.addInput('path\to\file')
.addInputOption('-xerror')
.addInputOption('-v error')
.output('-')
.outputOptions('-f null')
.run();

Reference:

https://stackoverflow.com/questions/43349360/how-to-check-for-corrupted-webm-video-using-node-js-and-fluent-ffmpeg

Christoff Erasmus

Posted 2010-01-25T12:54:46.033

Reputation: 133