Quickly check the integrity of video files inside a directory with ffmpeg

Try this command:

find . -name "*.mp4" -exec ffmpeg -v error -i "{}" -map 0:1 -f null - 2>error.log \;

The find command finds all files in the current directory recursively which correspond to the given pattern: "*.mp4". After -exec comes the command you want to run for all files. "{}" substitutes the name of the current file, where the quotes allow spaces in file names. The command has to end with \;.

EDIT:

The code above generates only one log file, which makes it impossible to know which file was broken. To instead create separate .log files for each of the .mp4 files, you can use the {} symbol one more time:

find . -name "*.mp4" -exec sh -c "ffmpeg -v error -i '{}' -map 0:1 -f null - 2>'{}.log'" \;

I recently met with the same problem, but I think there's a simple hack to find out which files are broken:

find -name "*.mp4" -exec sh -c "echo '{}' >> errors.log; ffmpeg -v error -i '{}' -map 0:1 -f null - 2>> errors.log" \;

This way the errors follow the filename (and path).


The issue with the other answers using ffmpeg to recode to null format is that they take a long time. 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.


A batch file for Windows systems, assuming that ffmpeg is somewhere in your path:

@echo off
for /r %%f in (*.mp4) do (
    ffmpeg -hide_banner -v error -i "%%f" -map 0:1 -f null - 2>out.txt
    if errorlevel 1 (
        type out.txt
        echo   %%f
    )
)
if exist out.txt del out.txt 1>nul 2>nul
pause

This will list the error followed by the indented file name only for those files in error.

Tags:

Ffmpeg

Video