pythongstreamerrtspip-camera

Recording script based on gstreamer doesn’t work with Dahua camera


I have the following python script that records video from an rtsp camera:

# initialize GStreamer
Gst.init(None)

# calculate pipeline string         
pipeline_str = ...
pipeline_str += f"! filesink location={full_path}.mkv"

# build the pipeline
pipeline = Gst.parse_launch(pipeline_str)                            

# start playing
pipeline.set_state(Gst.State.PLAYING)

# sleep for duration seconds
time.sleep(opt.duration)

print("Send EoS")
Gst.Element.send_event(pipeline, Gst.Event.new_eos())

# wait until EOS or error
bus = pipeline.get_bus()
msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS)

# free resources
print("Switch to NULL state")
pipeline.set_state(Gst.State.NULL)

Above script works well with Hikvision camera. Sample pipeline string for Hikvision camera:

rtspsrc location=rtsp://192.168.1.20/Streaming/Channels/1/ user-id=admin user-pw=mypass ! application/x-rtp, media=video, encoding-name=H264 ! queue ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw(memory:NVMM),width=1280,height=720 ! nvv4l2h265enc bitrate=2000000 ! h265parse ! matroskamux ! filesink location=/home/nvidia/recordings/20240109_2044.mkv

For Dahua camera I am using the following pipeline string (it’s the same string only location part differs):

rtspsrc location=rtsp://admin:pass@192.168.1.2:554/cam/realmonitor?channel=1/subtype=0 user-id=admin user-pw=pass ! application/x-rtp, media=video, encoding-name=H264 ! queue ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw(memory:NVMM),width=1280,height=720 ! nvv4l2h265enc bitrate=2000000 ! h265parse ! matroskamux ! filesink location=/home/nvidia/recordings/20240109_2047.mkv

All I get with second pipeline is an empty file of size 1KB. Any idea what might be wrong?

Please note that Dahua camera playback has been tested with OpenCV. Using following lines I can playback the video stream from Dahua camera with no issues:

# RTSP stream URL
rtsp_url = "rtsp://admin:pass@192.168.1.2:554/cam/realmonitor?channel=1&subtype=0"

# Create a VideoCapture object
cap = cv2.VideoCapture(rtsp_url)

UPDATE:

I have managed to narrow down the issue. If, when using Dahua camera, I substitute nvv4l2decoder with decodebin then the script works ok.

Any idea why nvv4l2decoder cannot handle a stream coming from a Dahua camera?


Solution

  • After upgrading the camera firmware the pipeline mentioned in the question worked. I am just adding this as an answer here to help anyone facing a similar issue with a Dahua camera.