VideoWriter.write() very slow

Hi,

I’m using opencv in python on ubuntu to record a couple of IP cameras and save the video in a file but the issue I’m encountering is that the write method is very slow for 1080p 25fps, it can barely handle about 12 fps while looking at a white wall and if I film something with more movement it takes up to 0.15s for a single frame.
Is there a way to accelerate the encoding by using hardware acceleration?
Note: I’m running this on a Jetson Xavier.

def test():
	global threads_running
	global url

	capture = cv2.VideoCapture(url)

	fourcc = cv2.VideoWriter_fourcc(*"mp4v")
	fps = capture.get(cv2.CAP_PROP_FPS)
	width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
	height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
	frameSize = (int(width), int(height))

	if threads_running:
		read = True
		writer = cv2.VideoWriter("test.mp4", fourcc, fps, frameSize)

		if writer.isOpened():
			while read and capture.isOpened() and threads_running:
				
				previousTime = time.time()
				read, frame = capture.read()
				frameTime = time.time() - previousTime
				print(f"read  | max fps: {int(1 / frameTime)} frame time: {frameTime}")

				previousTime = time.time()
				writer.write(frame)
				frameTime = time.time() - previousTime
				print(f"write | max fps: {int(1 / frameTime)} frame time: {frameTime}")

			writer.release()
		else:
			print("failed to open video writer")

		capture.release()

If you compile enable the cuda modules and your building from the tip of the master branch you could try with cudacodec::VideoReader and cudacodec::VideoWriter.

The Nvidia spec for the Jetson only mentions support for h264/5 which is odd for a volta chip so I am not 100% sure if it will support cudacodec which relies on the Nvidia Video Codec SDK.

I believe that recent OpenCV has hardware-accelerated decoding and encoding. if there’s a regular linux on that nvidia device, I’d expect hardware acceleration to be available to standard system APIs, which OpenCV (or ffmpeg) would use.

The VideoReader does not use harware-acceleration by default but can be enabled with a few parameters, I was hoping VideoWriter is the same.

As far as I am aware cv.VideoWriter should be accelerated for some codecs. That said I have had limited success with hardware acceleration myself.

I ran a quick comparisson between CPU transcoding (I couldn’t get hardware acceleration to work for the codecs I was using) and GPU transcoding with cv.cudacodec. The performance increase was around 9x on my setup (Mobile RTX 3070 Ti vs i7-12700H), it might be worth a try if its supported on your system.

See here for the comparisson.