I am a beginner in Gstreamer, but I managed to build two pipelines to read and save to file a number of camera streams and use them in Python. Currently I am using OpenCV VideoCapture and VideoWriter to do the job, but I need access also to the Gstreamer buffer to retrieve the frame timestamp as I will need to synchronize the captured frames with another process. I was told that once the frame is read in VideoCapture then the timestamp information is lost, so my question is how can I access the frame buffer with the pipeline that I built?
This is the code that I have at the moment, reading and writing streams from four cameras. I used OpenCV because it worked so far but it is not essential, if the conversion to BGR and CV frame can be skipped then I'll be happy too.
import cv2
def gstreamer_in(cam_id):
""" Creates string with gstreamer input pipeline """
gst_in = ('nvarguscamerasrc sensor-id={} ! '
'video/x-raw(memory:NVMM), height=1080, width=1920, framerate=(fraction)30/1, format=NV12 ! '
'nvvidconv ! video/x-raw, height=1080, width=1920, format=BGRx ! '
'videoconvert ! video/x-raw, format=BGR ! '
'appsink').format(cam_id)
return gst_in
def gstreamer_out(cam_id):
""" Creates string with gstreamer output pipeline """
gst_out =('appsrc ! autovideoconvert ! '
'omxh265enc ! video/x-h265, stream-format=byte-stream ! '
'h265parse ! filesink location=video_{}.h265 ').format(cam_id)
return gst_out
n_cams = 4
caps = []
writers=[]
for i in range(0,n_cams):
# Create VideoCapture and writers
caps.append(cv2.VideoCapture(gstreamer_in(i), cv2.CAP_GSTREAMER))
writers.append(cv2.VideoWriter(gstreamer_out(i), cv2.CAP_GSTREAMER, i, 30, (1920, 1080), True))
print("Press q to stop...")
while True:
# Read and write cameras
for i in range(0, n_cams):
ret, frame = caps[i].read()
# read timestamp?
if ret == True:
writers[i].write(frame)
# other process happening here
if cv2.waitKey(1)==ord('q'):
break
# Release
for i in range(0, n_cams):
caps[i].release()
writers[i].release()