FFmpeg 从帧 OpenCV python 流视频到 rtmp [英] FFmpeg stream video to rtmp from frames OpenCV python

查看:360
本文介绍了FFmpeg 从帧 OpenCV python 流视频到 rtmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 indistural 项目的背景下,我开发了一个实时应用程序来使用 AI 算法检测人员.在本地,我使用带帧的 OPENCV 获取并显示视频.

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.

目标是实现从Opencv的帧到rtmp服务器的流视频

FFmpeg 似乎是一个很好的视角.然而,流媒体往往是从.mp4 或几个.jpg 格式来发布流视频到rtmp 服务器上.

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

谢谢.

推荐答案

首先,ffmpeg 的功能是将流推送到 rtmp 服务器.您可以尝试为 ffmpeg cammand 创建一个子进程,并通过 PIPE 传递您的帧.

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

这篇关于FFmpeg 从帧 OpenCV python 流视频到 rtmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆