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
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