Skip to content

Instantly share code, notes, and snippets.

@duyuxuan
Forked from takidog/main.py
Created October 20, 2021 06:19
Show Gist options
  • Save duyuxuan/9b07b87ce44d195c57533ee3b208664d to your computer and use it in GitHub Desktop.
Save duyuxuan/9b07b87ce44d195c57533ee3b208664d to your computer and use it in GitHub Desktop.
push RTSP stream with ffmpeg by python
I have not found information on the Internet to implement the python push RTSP stream, so I try it.
I think RTMP can get more way to make it.
:)
import cv2
import subprocess as sp
if __name__ == "__main__":
rtsp_server = 'rtsp://example.org:554/...' # push server (output server)
#pull rtsp data, or your cv cap. (input server)
cap = cv2.VideoCapture(
'rtsp://example.org:554/pull from me ')
sizeStr = str(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))) + \
'x' + str(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = int(cap.get(cv2.CAP_PROP_FPS))
command = ['ffmpeg',
'-re',
'-s', sizeStr,
'-r', str(fps), # rtsp fps (from input server)
'-i', '-',
# You can change ffmpeg parameter after this item.
'-pix_fmt', 'yuv420p',
'-r', '30', # output fps
'-g', '50',
'-c:v', 'libx264',
'-b:v', '2M',
'-bufsize', '64M',
'-maxrate', "4M",
'-preset', 'veryfast',
'-rtsp_transport', 'tcp',
'-segment_times', '5',
'-f', 'rtsp',
rtsp_server]
process = sp.Popen(command, stdin=sp.PIPE)
while(cap.isOpened()):
ret, frame = cap.read()
ret2, frame2 = cv2.imencode('.png', frame)
process.stdin.write(frame2.tobytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment