Limit the ffmpeg to use as many cores as possible

Previously I found a weird problem which I post on here to ask for some help:

Thanks for their instruction,however I still not figured it out how to set limitation on ffmpeg in cv2.videocapture()function.
I use other way to handle this problem.
After building the opencv with gstreamer support this phenomenon might disappear.Also it has side-effect that the rtsp stream it read is not as fluent as ffmpeg although gstreamer is faster. still want to use the ffmpeg,can anyone give me some advice?

Have you tried setting the number of threads in the VideoCapture constructor as

cap = cv2.VideoCapture(addr, cv2.CAP_FFMPEG, [CAP_PROP_N_THREADS, nThreads ]) 

Thanks for your reply.You mean cap = cv2.VideoCapture(addr,cv2.CAP_FFMPEG, [cv2.CAP_PROP_N_THREADS, 2 ]) ? I tried this but cv2 seems dont’t have CAP_PROP_N_THREADS.
I also Tried like this cap = cv2.VideoCapture(addr,cv2.CAP_FFMPEG, [2, 2 ]) It failed ethier.

You need to build from a commit after this pr was merged

Upgrade to OpenCV 4.7.0.

1 Like

OK, you mean I should download from here GitHub - cudawarped/opencv at ffmpeg_rtsp_low_fps
and build opencv again.But how to check this pr’s opencv version?

Either install the latest pre-built wheel with pip

or build again checking out the 4.7.0 tag

after you have cloned the repository

OKOK.Thanks for your advice. By the way, if I want to limit the ffmepg to use less than 16 cores for example like 4 cores. The only way to do is to rebuild opencv4.7.0 right? Do you know how to change the default setting in opencv 4.7.0?

No. The option to change the number of threads is available at runtime, when you create VideoCapture as

cap = cv2.VideoCapture(addr, cv2.CAP_FFMPEG, [CAP_PROP_N_THREADS, 16]) 

You need to upgrade your version of OpenCV python bindings (build from source or pip install) because this feature was added in OpenCV 4.7.0 (is not available in opencv-python version :

WOW! Thanks a lot. It seems like I am going to the wrong direction! Any way, THANKS!