PiCamera使用ffmpeg流式传输到youtube并每秒捕获图像

时间:2020-10-09 12:18:11

标签: python ffmpeg youtube picamera

我正在使用ffmpeg将Raspberry Pi 4上的Python脚本直播发送给Youtube。

此外,我想检查流中的运动。因此,我想相互比较两个框架。由于这需要大量的计算,因此我想限制自己只能每秒“仅”比较一帧。

然而,camera.capture()命令似乎花费很长时间,因此流一次又一次地挂起。

在流中获取原始帧的最佳方法是什么?

在使用ffmpeg创建流时,是否有更好的方法来检测运动?

#!/usr/bin/env python3
import subprocess
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np

YOUTUBE = 'rtmp://a.rtmp.youtube.com/live2/'
KEY = 'MyYoutubeKey'
stream_cmd = '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 ' + YOUTUBE + KEY

avg = None
stream_pipe = subprocess.Popen(stream_cmd, shell=True, stdin=subprocess.PIPE)
resolution = (1280,720)
with PiCamera(resolution = resolution) as camera:

    rawCapture = PiRGBArray(camera, size = resolution)

    camera.framerate = 25
    camera.vflip = True
    camera.hflip = True
    camera.start_recording(stream_pipe.stdin, format='h264', bitrate = 6000000)
    while True:
        camera.wait_recording(1)

        # this command takes a long time to finish
        frame = camera.capture(stream_pipe.stdin, 'rgb')

        """
        further calculations
        """

        rawCapture.truncate(0)

    camera.stop_recording()
    stream_pipe.stdin.close()
    stream_pipe.wait()

0 个答案:

没有答案
相关问题