将pi的opencv视频用于foutmping for Youtube流媒体

问题描述 投票:2回答:1

这是一个使用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

而我正试图将它管理到foutmpout到Youtube流

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

Piping picam live video to ffmpeg for Youtube streaming.

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 experimental -f flv rtmp: //a.rtmp.youtube.com/live2/[STREAMKEY]

Piping OPENCV raw video to ffmpeg for mp4 file.

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

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

python-3.x opencv ffmpeg raspberry-pi opencv3.1
1个回答
0
投票

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

#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。

我希望它对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.