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

时间:2017-05-16 02:29:41

标签: python-3.x opencv ffmpeg raspberry-pi opencv3.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

我正在尝试将其传输到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 experimental -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

到目前为止,我没有运气。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分: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。

希望对您有帮助。