FFMPEG conversion runs forever

1

I'm running web app that is using ffmpeg to convert image + audio to video and sometimes someone happens to upload something that will make ffmpeg run forever on 100% CPU until the whole drive is filled.

It happens when someone uploads some corrupted or weird file, or recently I was able to catch that it also happens when there is image instead of audio (has to do something with -loop)

ffmpeg -loop 1 -r 1 -i image.jpg -i image2.jpg -vcodec libx264 -acodec copy -xerror -preset ultrafast -r 1 -shortest out.mp4

I know I could validate user input, but this should be not happening. What ffmpeg query would prevent this, but also would provide identical file?

I tried adding -stimeout or -timeout but it didn't work.

Robert Vangor

Posted 2017-10-20T11:46:03.270

Reputation: 113

Answers

1

In the given command,

ffmpeg -loop 1 -r 1 -i image.jpg -i image2.jpg -vcodec libx264 -acodec copy -xerror -preset ultrafast -r 1 -shortest out.mp4

since no map assignments are made, ffmpeg maps the first video input to the output. Since the loop option has been set, it is an unending stream. The -shortest has no effect as the 2nd image doesn't get auto-mapped and so its duration (0.04s) has no relevance.

A workaround is to add a finite dummy audio stream, created like so - ffmpeg -f lavfi -t 1 -i anullsrc=cl=mono dummy.m4a

ffmpeg -loop 1 -r 1 -i image.jpg -i image2.jpg -i dummy.m4a -vcodec libx264 -acodec copy -xerror -preset ultrafast -r 1 -shortest out.mp4

This won't tackle the issue of 'weird' files.


There's also a -timelimit seconds e.g. -timelimit 600 to exit ffmpeg after 10 minutes. However, this uses system call setrlimit and AFAICT, isn't available on Windows natively, so I can't test it right now.

Gyan

Posted 2017-10-20T11:46:03.270

Reputation: 21 016