Search code examples
pythonopencvffmpeg

FFmpeg stream video to rtmp from frames OpenCV python


In the context of indistural project, i developed a real time application to detect person with AI algorithms. In local i get and display videos with OPENCV operating with frames.

The objective is to realise a stream video from frames of Opencv to rtmp server

FFmpeg seems a good perspective. However, often the stream strats from .mp4 or several .jpg to publish stream video on rtmp server.

Thank you.


Solution

  • Firstly ffmpeg is functional for pushing stream to rtmp server. you can try create a subprocess for ffmpeg cammand, and pass your frames through PIPE.

    Here is an simple example code you can try

    import subprocess
    import cv2
    rtmp_url = "rtmp://127.0.0.1:1935/stream/pupils_trace"
    
    # In my mac webcamera is 0, also you can set a video file name instead, for example "/home/user/demo.mp4"
    path = 0
    cap = cv2.VideoCapture(path)
    
    # 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 and params for ffmpeg
    command = ['ffmpeg',
               '-y',
               '-f', 'rawvideo',
               '-vcodec', 'rawvideo',
               '-pix_fmt', 'bgr24',
               '-s', "{}x{}".format(width, height),
               '-r', str(fps),
               '-i', '-',
               '-c:v', 'libx264',
               '-pix_fmt', 'yuv420p',
               '-preset', 'ultrafast',
               '-f', 'flv',
               rtmp_url]
    
    # using subprocess and pipe to fetch frame data
    p = subprocess.Popen(command, stdin=subprocess.PIPE)
    
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            print("frame read failed")
            break
    
        # YOUR CODE FOR PROCESSING FRAME HERE
    
        # write to pipe
        p.stdin.write(frame.tobytes())