# VideoWriter.write() very slow

**URL:** <https://forum.opencv.org/t/videowriter-write-very-slow/10776>\
**Category:** Python\
**Tags:** performance, videoio, nvidia\
**Created:** [November 2, 2022, 1:22pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776 "2022-11-02T13:22:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PseudoCoffee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pseudocoffee/32/6385_2.png) [@PseudoCoffee](https://forum.opencv.org/u/PseudoCoffee)\
**Post date:** [November 2, 2022, 1:22pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776/1 "2022-11-02T13:22:13Z")

</div>

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.

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [November 2, 2022, 1:39pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776/2 "2022-11-02T13:39:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 2, 2022, 6:34pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776/3 "2022-11-02T18:34:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PseudoCoffee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pseudocoffee/32/6385_2.png) [@PseudoCoffee](https://forum.opencv.org/u/PseudoCoffee)\
**Post date:** [November 2, 2022, 10:31pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776/4 "2022-11-02T22:31:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [November 3, 2022, 4:08pm UTC](https://forum.opencv.org/t/videowriter-write-very-slow/10776/5 "2022-11-03T16:08:31Z")

</div>

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](https://github.com/cudawarped/opencv-experiments/blob/master/nbs/cudacodec.ipynb) for the comparisson.
