将 pi 的 opencv 视频传输到 ffmpeg 以进行 Youtube 流媒体播放

2024-01-20

这是一个使用 OpenCV 读取 picam 的小型 python3 脚本:

#picamStream.py

import sys, os
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (960, 540)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(960, 540))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    image = frame.array

    # ---------------------------------
    # .
    # Opencv image processing goes here
    # .
    # ---------------------------------

    os.write(1, image.tostring())

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

# end

我正在尝试将其通过管道传输到 ffmpeg 到 Youtube 流

我的理解是,我需要引用下面两个命令才能以某种方式提出一个新的 ffmpeg 命令。

将 picam 实时视频传输到 ffmpeg 以进行 Youtube 流式传输。

raspivid -o - -t 0 -vf -hf -w 960 -h 540 -fps 25 -b 1000000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict 实验 -f flv rtmp: //a.rtmp.youtube.com/live2/[STREAMKEY]

将 OPENCV 原始视频传输到 ffmpeg 以生成 mp4 文件。

python3 picamStream.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 960x540 -framerate 30 -i - foo.mp4

到目前为止我还没有运气。谁能帮我这个?


这是我在树莓派中使用的程序。

#main.py

import subprocess 
import cv2

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

command = ['ffmpeg',
            '-f', 'rawvideo',
            '-pix_fmt', 'bgr24',
            '-s','640x480',
            '-i','-',
            '-ar', '44100',
            '-ac', '2',
            '-acodec', 'pcm_s16le',
            '-f', 's16le',
            '-ac', '2',
            '-i','/dev/zero',   
            '-acodec','aac',
            '-ab','128k',
            '-strict','experimental',
            '-vcodec','h264',
            '-pix_fmt','yuv420p',
            '-g', '50',
            '-vb','1000k',
            '-profile:v', 'baseline',
            '-preset', 'ultrafast',
            '-r', '30',
            '-f', 'flv', 
            'rtmp://a.rtmp.youtube.com/live2/[STREAMKEY]']

pipe = subprocess.Popen(command, stdin=subprocess.PIPE)

while True:
    _, frame = cap.read()
    pipe.stdin.write(frame.tostring())

pipe.kill()
cap.release()

Youtube 需要音频源,因此使用 -i /dev/zero。

我希望它对你有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 pi 的 opencv 视频传输到 ffmpeg 以进行 Youtube 流媒体播放 的相关文章

随机推荐