[FFmpeg-user] How can I reduce the cpu usage of ffmpeg?

voip at gmx.ca voip at gmx.ca
Sun Jan 11 02:07:57 CET 2015

On Saturday, January 10, 2015 at 1:44 PM, "Nicholas Robbins" <nickrobbins-at-yahoo.com at ffmpeg.org> wrote:

> Put something like the following line in your bash script right before the ffmpeg invocation:

> ( sleep 10s ; cpulimit -z -e ffmpeg -l 95 ) &

> That should start and not wait to finish (a timer of 10 seconds and then run the cuplimit command) and then continue on with your script.

> Does that make sense?

Sure does, and looks like the easiest way to do it in a shell script.  Thanks!

On Saturday, January 10, 2015 at 2:07 PM, "Moritz Barsnick" <barsnick at gmx.net> wrote:

> I would have suggested that too, or something like suggested here:
> http://stackoverflow.com/a/8510197
> though I think there's an error in there.

> $ ffmpeg ... & (sleep 2 && cpulimit -z -e ffmpeg -l 95)

> Note that I believe the example given in the link requires a double-'&'
> after sleep. I use brackets just to go sure.

I liked your idea also but it didn't make quite as much sense to me as the first solution.  My problem is the use of the & character separating the ffmpeg command from the other stuff.  I always THOUGHT that a single & causes a task to run in the background, and while that's fine for cpulimit, it's not fine for ffmpeg because as I mentioned there are a couple other cleanup tasks that need to run after ffmpeg is finished, and if ffmpeg goes into the background, those tasks will run immediately.  With the usage that Nicholas suggested, there is no way that can happen.

I'm a little confused as to why in the first example the sleep command is followed by a ; while in yours it's followed by && - two different ways to indicate the same thing?

> Even better: cpulimit accepts a PID. If ffmpeg doesn't do crazy fork
> magic (which I believe it doesn't), you should be able to tell cpulimit
> to pick up ffmpeg's pid. In a Bourne shell, when you background a
> process, this PID is stored in "$!".

> $ ffmpeg ... &
> $ cpulimit -l 95 -p $!

Nice to know but again backgrounding ffmpeg is not an option in this particular case.  Thanks for the responses!

More information about the ffmpeg-user mailing list