【问题标题】:Pipe OpenCV and PyAudio to ffmpeg streaming youtube rtmp from python通过管道将 OpenCV 和 PyAudio 从 python 传输到 ffmpeg 流 youtube rtmp
【发布时间】:2021-10-02 06:26:34
【问题描述】:

如何通过管道将 openCV 和 PyAudio 从 python 传输到 ffmpeg 流 youtube rtmp。 错误信息显示如下: 没有这样的过滤器:“管道:1” 管道:1:无效参数

这是我的代码:

导入模块

import cv2
import subprocess
import pyaudio

音频

p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

# Stream Audio data here
# data = stream.read(CHUNK)

视频

rtmp = r'rtmp://a.rtmp.youtube.com/live2/key'

cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = 30

命令参数在这里

command = ['ffmpeg',
           '-y',
           '-f', 'rawvideo',
           '-pixel_format', 'bgr24',
           '-video_size', "{}x{}".format(width, height),
           '-framerate', str(fps),
           '-i', 'pipe:0',
           '-re',
           '-f', 'lavfi',
           '-i', 'pipe:1',
           '-c:v', 'libx264',
           '-c:a', 'aac',
           '-vf', 'format=yuv420p',
           '-f', 'flv',
           rtmp]

创建子进程到ffmpeg命令

pipe = subprocess.Popen(command, shell=False, stdin=subprocess.PIPE
)
while cap.isOpened():
    success, frame = cap.read()
    if success:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        pipe.stdin.write(frame.tostring())
        pipe.stdin.write(stream.read(CHUNK))

音频停止

stream.stop_stream()
stream.close()
p.terminate()

视频停止

cap.release()
pipe.terminate()

谢谢

【问题讨论】:

  • 首先在 shell 上手动尝试命令并从那里尝试/检查,要容易得多...
  • 根据documentationpipe:[number]数字是管道的文件描述符对应的数字(例如0代表stdin1 用于 stdout2 用于 stderr)。我不认为你可以使用:'-i', 'pipe:1'.
  • 谢谢,所以我弄错了 pipe:1。如果我想进行两个输入(视频和音频),我需要将 0 用于标准输入(视频)并为第二个输入(音频)创建另一个管道。如果这段代码在windows基础上运行,如何创建命名管道和管道中的流字节?

标签: python opencv ffmpeg stream pyaudio


【解决方案1】:

我已经成功地使用 CreateNamePipe 来创建视频并使用标准输入来创建音频。

PIPE_NAME = r'\\.\pipe\VideoPipe'

video_pipe = win32pipe.CreateNamedPipe(
    PIPE_NAME,
    win32pipe.PIPE_ACCESS_OUTBOUND,
    win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT,
    1, 65536, 65536,
    0,
    None)

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2020-10-27
    • 2016-03-14
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多