I want to read an rtsp video source, add overlay text and push it to the RTMP endpoint.I am using Videocapture to read the video source and python subprocess to write the frames back to RTMP endpoint. I referred this FFmpeg stream video to rtmp from frames OpenCV python - Stack Overflow
import sys
import subprocess
import cv2
import ffmpeg
rtmp_url = "rtmp://127.0.0.1:1935/live/test"
path = 0
cap = cv2.VideoCapture("rtsp://10.0.1.7/media.sdp")
# gather video info to ffmpeg
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
command = ['ffmpeg', '-i', '-', "-c", "copy", '-f', 'flv', rtmp_url]
p = subprocess.Popen(command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
font = cv2.FONT_HERSHEY_SIMPLEX
while cap.isOpened():
ret, frame = cap.read()
cv2.putText(frame, 'TEXT ON VIDEO', (50, 50), font, 1, (0, 255, 255), 2, cv2.LINE_4)
cv2.imshow('video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if not ret:
print("frame read failed")
break
try:
p.stdin.write(frame.tobytes())
except Exception as e:
print (e)
cap.release()
p.stdin.close()
p.stderr.close()
p.wait()
The python script returns “[Errno 32] Broken pipe”. Running the ffmpeg command in the terminal works fine.
ffmpeg -i rtsp://10.0.1.7/media.sdp -c copy -f flv
rtmp://127.0.0.1:1935/live/test
The above command works fine, and I can push the input stream to RTMP endpoint. But I can’t write processed frame to subprocess which has ffmpeg running.
Please let me know if I miss anything.