46

I am calling FFMpeg inside a C# Windows Forms application. Since it uses so much CPU (always above 90%), none of my threads can continue working. Is there a way to limit this CPU usage?

I've tried to set Process.PriorityClass to PriorityClass.BelowNormal but this totally blocked the ffmpeg process.

I am sure there is a way to do this since I see a lot of programs that utilize ffmpeg.

Please help.

Mert Sevinc
  • 929
  • 1
  • 8
  • 25

3 Answers3

56

You can't limit FFMpeg to a percentage of CPU use, but you can set the -threads parameter on your FFMpeg call, if you have 4 cores try set it to -threads 2 that should limit you to around 50% CPU.

Another solution might be to lower the priority on your FFMpeg process, to something lower than your applications.

Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
  • 1
    So you are suggesting that I should lower the priority of it, if my CPU has a single core. Is this really the best way to do this? – Mert Sevinc Dec 30 '10 at 20:50
  • 2
    @Mert, yes for a single core CPU without hyper threading, lowering the priority would be the way to go. Media encoding is a CPU intensive task, and if you are looking for any kind of scalability you should consider not running running FFMpeg on the same machine as your application, in that case a client/server solution would be something you might want to look into. I hope it answered your question. – Jesper Fyhr Knudsen Dec 31 '10 at 09:05
  • 2
    It's really not the best answer, since there are codecs (like for example x265 encoding) that will use more CPU power, even with a setting -threads 1. They do not translate the ffmpeg setting into actual number of threads, they just use it as a guideline. – Cray May 18 '20 at 16:06
  • @JesperFyhrKnudsen the solution is to limit the ffmpeg process and all of its threads to a certain processor execution affinity (limiting to some number of cores) (Sysinternals Process Explorer does this), on the OS level. There are tools that can limit execution on OS level too. The "incorrect" was referring to "-threads 2 that should limit you to around 50% CPU". If you try it, you can see that setting will lead to a much higher CPU usage on some codecs than just 2 cores. "threads" argument in ffmpeg does not have literal meaning, it does not strictly limit threads. – Cray Jun 03 '20 at 08:44
  • I'd like to add, that if you're running multiple instances of FFMpeg, say 4 or more, this is not a viable solution. Limiting affinity to 3 out of 4 CPU cores in task manager will be far more suitable. – gimmegimme Jul 03 '21 at 20:42
31

Just for peoples who try to find solutions for using in terminal (bash, zsh, or on servers)...

nice -n 20 cpulimit -l 60 -i ffmpeg -threads 1 -i in.avi out.mp4

nice is program used for setting priority. Read man nice to know what -n argument mean at your system. On macOS 20 is lowest and -20 is highest.

cpulimit is open source utility used to control cpu usage (Linux/OS X/FreeBSD).

On MacOS this need sudo.

Eugene
  • 547
  • 4
  • 11
2

Check this answer: https://superuser.com/a/214572/458727

I used Battle Encoder Shirasé to throttle down FFMPEG. BES is open-source, so watching its code can help to get an idea.