Hello,
I’m trying to stream an RTSP feed using GStreamer.
Input : IP camera (RTSP, H.264, 1920*1080)
- `cv2.VideoCapture()`
Write : RTSP (H.264, 1920*1080)
- in `while` loop
- `subprocess.stdin.write(GStreamer pipeline)`
I want to send frames to the MediaMTX RTSP server using a GStreamer pipeline.
The imshow
function is functioning correctly.
But, the ‘write’ function isn’t functioning correctly. It doesn’t work at all
After attempting to connect to the RTSP stream from another machine, the MediaMTX terminal displayed the following message:
Here is the code
import cv2
import subprocess as sp
import psutil
RTSP_URL = {RTSP_source}
RTSP_SERVER_URL = 'rtsp://localhost:8554/mystream'
pipeline_out = (
f'gst-launch-1.0 fdsrc ! queue2 !'
f'h264parse ! nvv4l2decoder !'
f'nvvidconv ! '
f'"video/x-raw(memory:NVMM), format=(string)I420" ! '
f'nvv4l2h264enc bitrate=4000000 ! '
f'rtspclientsink location={RTSP_SERVER_URL}'
)
p_out = sp.Popen(pipeline_out, shell=True, stdin=sp.PIPE, bufsize=10**9)
if __name__=='__main__':
try:
cap = cv2.VideoCapture(RTSP_URL)
i = 0
while True:
ret, frame = cap.read()
if not ret:
print("Failed to read from stream")
cap.release()
break
frame_yuv = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420)
##### Here is the problem #####
p_out.stdin.write(frame_yuv.tobytes())
p_out.stdin.flush() # Flush the buffer
# Print memory usage
process = psutil.Process() # Get the current process
memory_usage = process.memory_info().rss # Get the memory usage in bytes
print(f"Memory usage: {memory_usage} bytes")
print(f"### {i} ###")
cv2.imshow("RTSP", frame)
i = i + 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
pass
except Exception as e:
print(f"An error occured : {e}")
finally:
cap.release()
cv2.destroyAllWindows()
if p_out.poll() is None:
p_out.stdin.close()
p_out.wait()
p_out.kill()```