Skip to main content
 首页 » 编程设计

python之如何使用子进程将 Picamera 视频传输到 FFMPEG (Python)

2024年10月01日8mengfanrong

我看到大量关于将 raspivid 流直接传输到 FFMPEG 以进行编码、混合和重新流式传输的信息,但这些用例主要来自 bash;类似于:

raspivid -n -w 480 -h 320 -b 300000 -fps 15 -t 0 -o - | ffmpeg -i - -f mpegts udp://192.168.1.2:8090ffmpeg

我希望利用 Picamera 库的功能,这样我就可以在使用 FFMPEG 进行流式处理的同时使用 OpenCV 和类似的方法进行并发处理。但我无法弄清楚如何正确打开 FFMPEG 作为子进程并将视频数据传输给它。我已经看到很多尝试,unanswered posts , 和人 claiming to have done it ,但它们似乎都不适用于我的 Pi。

我是否应该使用 Picamera 创建一个视频缓冲区并将该原始视频通过管道传输到 FFMPEG?我可以使用 camera.capture_continuous() 并将我用于 OpenCV 计算的 bgr24 图像传递给 FFMPEG 吗?

我尝试了各种变体,我不确定我是否只是误解了如何使用子进程模块 FFMPEG,或者我只是缺少一些设置。我知道原始流不会有任何元数据,但我不完全确定我需要为 FFMPEG 提供哪些设置才能理解我提供的内容。

我有一个 Wowza 服务器,我最终会流式传输到它,但我目前正在通过流式传输到笔记本电脑上的 VLC 服务器进行测试。我目前试过这个:

import subprocess as sp 
import picamera 
import picamera.array 
import numpy as np 
 
npimage = np.empty( 
        (480, 640, 3), 
        dtype=np.uint8) 
with picamera.PiCamera() as camera: 
    camera.resolution = (640, 480) 
    camera.framerate = 24 
 
    camera.start_recording('/dev/null', format='h264') 
    command = [ 
        'ffmpeg', 
        '-y', 
        '-f', 'rawvideo', 
        '-video_size', '640x480', 
        '-pix_fmt', 'bgr24', 
        '-framerate', '24', 
        '-an', 
        '-i', '-', 
        '-f', 'mpegts', 'udp://192.168.1.54:1234'] 
    pipe = sp.Popen(command, stdin=sp.PIPE, 
                    stdout=sp.PIPE, stderr=sp.PIPE, bufsize=10**8) 
    if pipe.returncode != 0: 
        output, error = pipe.communicate() 
        print('Pipe failed: %d %s %s' % (pipe.returncode, output, error)) 
        raise sp.CalledProcessError(pipe.returncode, command) 
 
    while True: 
        camera.wait_recording(0) 
        for i, image in enumerate( 
                        camera.capture_continuous( 
                            npimage, 
                            format='bgr24', 
                            use_video_port=True)): 
            pipe.stdout.write(npimage.tostring()) 
    camera.stop_recording() 

我还尝试将流写入类似文件的对象,该对象仅创建 FFMPEG 子进程并写入其标准输入(初始化 picam 时可以为 camera.start_recording() 提供这样的对象):

class PipeClass(): 
    """Start pipes and load ffmpeg.""" 
 
    def __init__(self): 
        """Create FFMPEG subprocess.""" 
        self.size = 0 
        command = [ 
            'ffmpeg', 
            '-f', 'rawvideo', 
            '-s', '640x480', 
            '-r', '24', 
            '-i', '-', 
            '-an', 
            '-f', 'mpegts', 'udp://192.168.1.54:1234'] 
 
        self.pipe = sp.Popen(command, stdin=sp.PIPE, 
                         stdout=sp.PIPE, stderr=sp.PIPE) 
 
        if self.pipe.returncode != 0: 
            raise sp.CalledProcessError(self.pipe.returncode, command) 
 
    def write(self, s): 
        """Write to the pipe.""" 
        self.pipe.stdin.write(s) 
 
    def flush(self): 
        """Flush pipe.""" 
        print("Flushed") 
 
usage: 
(...) 
with picamera.PiCamera() as camera: 
    p = PipeClass() 
    camera.start_recording(p, format='h264') 
(...) 

任何对此的帮助都会很棒!

请您参考如下方法:

我已经能够使用类似以下内容将 PiCamera 输出流式传输到 ffmpeg:

import picamera 
import subprocess 
 
# start the ffmpeg process with a pipe for stdin 
# I'm just copying to a file, but you could stream to somewhere else 
ffmpeg = subprocess.Popen([ 
    'ffmpeg', '-i', '-', 
    '-vcodec', 'copy', 
    '-an', '/home/pi/test.mpg', 
    ], stdin=subprocess.PIPE) 
 
# initialize the camera 
camera = picamera.PiCamera(resolution=(800, 480), framerate=25) 
 
# start recording to ffmpeg's stdin 
camera.start_recording(ffmpeg.stdin, format='h264', bitrate=2000000) 

或者这不是您要找的东西?