I have tried to extract with following code but its not matching with server timestamp please suggest if there are any particular elements to use for this.
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject
def decode_h264_stream(rtsp_url):
"""Decodes the H.264 stream of an RTSP stream and extracts the timestamp.
Args:
rtsp_url: The URL of the RTSP stream.
Returns:
The timestamp in nanoseconds.
"""
# Initialize GStreamer.
Gst.init(None)
# Create a GStreamer pipeline.
pipeline = Gst.parse_launch(f"rtspsrc location={rtsp_url} ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink name=sink emit-signals=True sync=False")
# Get the appsink element from the pipeline.
appsink = pipeline.get_by_name("sink")
# Set properties to enable signal emission and asynchronous processing.
appsink.set_property("emit-signals", True)
appsink.set_property("sync", False)
# Define a callback function to handle new samples.
def on_new_sample(appsink):
sample = appsink.emit("pull-sample")
if sample:
buffer = sample.get_buffer()
timestamp = buffer.pts # Get the timestamp from the buffer (in nanoseconds)
print("Timestamp:", timestamp)
# Connect the callback function to the "new-sample" signal of the appsink.
appsink.connect("new-sample", on_new_sample)
# Start the GStreamer pipeline.
pipeline.set_state(Gst.State.PLAYING)
# Create a GLib main loop and run it.
loop = GObject.MainLoop()
try:
loop.run()
except KeyboardInterrupt:
pass
# Clean up and stop the pipeline.
pipeline.set_state(Gst.State.NULL)
return
# RTSP URL of the server-side pipeline
rtsp_url = "rtsp://0.0.0.0:8554/hnvfahmo"
# Call the function to decode the H.264 stream and print the timestamps.
decode_h264_stream(rtsp_url)