将 OpenCV 和 PyAudio 通过管道传输到来自 python 的 ffmpeg 流式传输 youtube rtmp

Pipe OpenCV and PyAudio to ffmpeg streaming youtube rtmp from python

如何将 openCV 和 PyAudio 通过管道传输到来自 python 的 ffmpeg 流式传输 youtube rtmp。 错误信息显示如下: 没有这样的过滤器:'pipe:1' pipe: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()

谢谢

我已成功使用 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)