Log in

View Full Version : Limit the number of cores for encoding


Lan4
28th December 2023, 14:56
I want to limit the number of cores. In total I have 4 cores and 8 threads. I only need 6 threads for video encoding in the background (this is 80% of the computer’s power), and at this time I use the remaining threads for other work. I can change the number of processors in the Task Manager, but I have to do this every time. I wanted to do this automatically.

Now I'm trying this:
start /affinity 3F C:\ffmpeg.exe
A separate cmd window opens, which does not have the usual FFMPEG encoding log. And perhaps not all script options are used. How to do it correctly?

In general, the FFMPEG script looks like this:

FOR %%F IN (*.mp4) DO (
echo LSMASHVideoSource("%%~dpnxF"^) > "%%~dpnxF.avs"
echo SetFilterMTMode("DEFAULT_MT_MODE", 2^) >> "%%~dpnxF.avs"
echo LoadPlugin("D:\ResampleMT.dll"^) Spline36ResizeMT(720, 1280, prefetch=4^) >> "%%~dpnxF.avs"
echo KNLMeansCL(D=1, A=1, h=1.5, device_type="auto"^) >> "%%~dpnxF.avs"
echo DetailSharpen(str=0.3, pow=8, z=6, ldmp=0, mode=1, med=false^) >> "%%~dpnxF.avs"
echo Prefetch(6^) >> "%%~dpnxF.avs"
C:\ffmpeg.exe -benchmark -i "%%~dpnxF.avs" -i "%%~dpnxF" -map 0:v:0 -map 1:a:0 -c:v libx264 -qp 26 -preset medium -g 30 -keyint_min 5 -c:a copy "%%~dpnF_new.mp4"
del "%%~dpnxF.avs"
)
pause

microchip8
28th December 2023, 15:10
you can control the amount of threads in libx264 by using its -x264-params threads=xx option

Lan4
28th December 2023, 15:23
Can I use both -x264-params and standard libx264 options in a script?

It seems to me that the encoding process loads the processor and video card 100% not only with the work of the libx encoder, but also with the work of the Avisynth filters.

microchip8
28th December 2023, 16:58
yes, you can use them

no idea on avisynth

StainlessS
28th December 2023, 19:02
I used to use this years ago [EDIT: Maybe before I joined D9 (2010)], and recently downloaded again (maybe same version, probably not upated for some time).
Allows to always use some Priority, or some Affinity when executing an app.
https://prio.en.softonic.com/

EDIT: Causes Task manager to set defaults for a particular executable, eg x264.exe or ffmpeg.exe.
(So if you set some affinity on x264, and you execute MeGUI which uses/calls x264, then it will use that affinity for the MeGUI or any other, x264)
EDIT: Free for personal use, I believe that it still works for current OS's, but not tried it myself, yet.

EDIT: Task Manager, CTRL / SHIFT / ESC, shortcut.

hellgauss
29th December 2023, 12:09
This is the standard command structure I use for background encoding on a PC which can be 24/7 on (it is windows 10).

start /b /low /wait cmd /c "ffmpeg -y -hide_banner -nostdin -bitexact -i "%%i" -map 0 -vf "removegrain=1,scale=1280:720:flags=bicublin:param0=0.125:param1=0.4375" -sws_flags accurate_rnd -c:v libx264 -level 40 -preset veryslow -crf 18 -tune animation -psy-rd 0.55 -aq-mode 3 -threads 1 -c:a libfdk_aac -rmvol 0.9375 -vbr 4 -c:s copy -disposition +0 -bitexact "%tmdir%\t1_%pref%%%~ni%suff%.mkv" 2>&1 | tee "%lgdir%\full-log_%%~ni.txt""


Remarks:
- Note the -threads option which is sent to libx264. Probably ffmpeg uses also other threads e.g. audio encoding, filters, avisynth etc...
- The cmd /c part can be omitted if the command is somewhat simple. cmd has a very strange quotation handling (see cmd /?) and here it is needed to redirect output to tee.exe, since I need both file and screen outputs. Perhaps you can also do it with start only but I was not able to do it.
- You can tune the start parameters according to your needs.

EDIT: Probably you do not see ffmpeg log since ffmpeg outputs to stderr instead of stdout, and start grabs only stdout. To redirect stderr to stdout use "2>&1"