Limiting CPU usage and server load with ffmpeg

New challenge: you’re on a shared hosting with lowendbox. You’re required to keep low server load (very low). How to do an ffmpeg video encoding?

By limiting threads and cpu.

To make a long story short:

nice -19 cpulimit -l 30 -- ffmpeg -f image2pipe -r 5 -c:v mjpeg -i - -y -r 15 -s hd720 -vcodec libx264 -threads 1 /tmp/yourvideo.mp4

Longer story: from stdin I get a bunch of JPG images to encode as video, in order to generate a time-lapse.

cpulimit is a tool used to lower CPU usage. It sends SIGSTOP and SIGCONT signal to a process in order to keep a limited resource occupation. It can accept a PID or a process name as parameter, and you can limit usage even of a running process. Of course, as you do for nice, you can run a command restricted from the beginning. That’s what we’re doing up there.

Sadly the first time I tried cpulimit with ffmpeg it didn’t seem to work very well, server load was still rising up. Then I found a nice option of ffmpeg-threads. That options limits the threads (cpu) usage of the encoding command. Beware where you put that option: in short, should be the last one.

By mixing cpulimit and -threads ffmpeg option I managed to encode 720 JPEGs into an mp4 without passing the 1 load threshold (on a 4 core VPS). Of course doing this way it will take much longer (it normally took ~3 minutes at full cpu power, now running for more than 10!) to encode, but at least you won’t get complaints from your hosting provider.

Un pensiero su “Limiting CPU usage and server load with ffmpeg

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Solve : *
14 − 2 =


Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.