Saving images on a loop in python using Gstreamer

I am attempting to use gstreamer and cv2 in order to live stream from two 4096x3040 cameras.
Below is the code used to set up the video as well as save the file to an external hardrive. I need to save the image taken on each loop, which occurs every second.

gstreamer_0 = ( " nvarguscamerasrc"
                        " sensor-id=0"
                        " ! video/x-raw(memory:NVMM), width=(int)4056, height=(int)3040,format=(string)NV12, framerate=(fraction)15/1"
                        " ! nvvidconv flip-method=0"
                        " ! videoconvert"
                        " ! video/x-raw, format=(string)BGR"
                        " ! appsink drop=1")

cap0 = cv2.VideoCapture(gstreamer_0,cv2.CAP_GSTREAMER)
while True:
## Read
            success0, img0 =
## Save
          ## rest of code in loop

The issue arises as while I’m able to read and write the images each second the CPU gets extremely hot, and cooling is an issue for my use.
From my understanding of this forum post below I believe that I cannot use python and cv2 to use the GPU and CUDA.

I have little experience with C/C++ and am wary to attempt to code in it. However using

gst-launch .... jpegenc ! filesink location = filename.jpg  

takes too long to complete. Is there anyway to launch a gstreamer pipeline on GPU and save files when request. Or is there a way to use pyCuda to use cv2 and gstreamer on the GPU

Hi, billyt2011

GStreamer actually offers some bindings that can help implement your video recording use case: Python GStreamer Tutorial. In general, for your use case it seems it would be more optimal to just write a plain GStreamer script using its Python bindings (putting aside OpenCV, you actually do not need it).

Also there are hardware accelerated GStreamer elements that you could use to take advantage of other processors and reduce load on the CPU. For example, for your use case, you could try to use nvjpegenc.

Jafet Chaves,
Embedded SW Engineer at RidgeRun
Contact us:
Developers wiki:

Using opencv would not be efficient for 2 4K video streams on Nano.
Note that here you are saving all frames using CPU codec.
Also note that the amount of GB to be stored by second would have to be ok for the disk you’re saving into. Better use a NVME SSD than a cheap SD card.
Also note that it will result very fast into a huge disk space. Better use an external disk so that it doesn’t mess with your linux system. Alternately, you may use a video codec reducing size vs quality.
You may better explain your use case for better advice.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.