簡體   English   中英

FFmpeg stream 視頻從幀 OpenCV Z23EEEB4347BDD265DFC6B7EE9AB3B7 到 rtmp

[英]FFmpeg stream video to rtmp from frames OpenCV python

在工業項目的背景下,我開發了一個實時應用程序來使用 AI 算法檢測人。 在本地,我使用 OPENCV 獲取並顯示使用幀操作的視頻。

目標是實現從 Opencv 幀到 rtmp 服務器的 stream 視頻

FFmpeg 似乎是一個很好的視角。 然而,stream 經常從.mp4 或幾個.jpg 開始在 rtmp 服務器上發布 stream 視頻。

謝謝你。

首先 ffmpeg 用於將 stream 推送到 rtmp 服務器。 您可以嘗試為 ffmpeg cammand 創建一個子進程,並通過 PIPE 傳遞您的幀。

這是您可以嘗試的簡單示例代碼

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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM