-
-
Save duyuxuan/9b07b87ce44d195c57533ee3b208664d to your computer and use it in GitHub Desktop.
push RTSP stream with ffmpeg by python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
:) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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