Detect corrupted Image Files with ffmpeg

User-uploaded files can be problematic, because you often can not know how exactly the files have been created. Images may look fine in your image viewer or browser, but may break when you try to process it in some form of data pipeline, e.g. creating a video from an image using ffmpeg.

We can tell ffmpeg to try and parse an image file while preventing any results to be saved, listening only for errors like this:

$ ffmpeg -i possibly_corrupted.png -loglevel error -f null -

This could be wrapped in a python script, listening for any stdout (if empty then the image seems to be good). Possible stdout for problems looks like this:

[png @ 0x5590d59d7060] Invalid PNG signature 0x4AE1B53D1749071E.
Error while decoding stream #0:0: Invalid data found when processing input

In this case, simply resaving the file “fixed” the image corruption, allowing it to then successfully be converted in the next step.

$ ffmpeg -i possibly_corrupted.png fixed.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.