【发布时间】:2017-12-26 18:37:33
【问题描述】:
我看到了大量关于将 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')
(...)
在这方面的任何帮助都会很棒!
【问题讨论】:
标签: python ffmpeg subprocess raspberry-pi3