【问题标题】:Pipe and OpenCV to FFmpeg with audio streaming RTMP in Python使用 Python 中的音频流 RTMP 将管道和 OpenCV 传输到 FFmpeg
【发布时间】:2022-01-25 00:24:01
【问题描述】:

我正在尝试使用音频流式传输 FFmpeg。
我将在下面显示我的代码:


导入模块

import subprocess as sp

创建变量

rtmpUrl = "rtmp://a.rtmp.youtube.com/live2/key"
camera_path = "BigBuckBunny.mp4"
cap = cv.VideoCapture(camera_path)

# Get video information
fps = int(cap.get(cv.CAP_PROP_FPS))
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))

命令参数

# ffmpeg command
command = ['ffmpeg',
        '-y',
        '-f', 'rawvideo',
        '-vcodec','rawvideo',
        '-pix_fmt', 'bgr24',
        '-s', "{}x{}".format(width, height),
        '-r', str(fps),
        '-i', '-',
        '-c:v', 'libx264',
        '-pix_fmt', 'yuv420p',
        '-preset', 'ultrafast',
        '-f', 'flv', 
        rtmpUrl]

为 ffmpeg 命令创建子进程

# Pipeline configuration
p = sp.Popen(command, stdin=sp.PIPE)

发送帧到 RTMP 服务器

# read webcamera
while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret:
        print("Opening camera is failed")
        break

    # write to pipe
    p.stdin.write(frame.tobytes())

我希望您能帮助我通过 FFmpeg 通过 RTMP 进行带音频的直播。谢谢!

【问题讨论】:

  • OpenCV 只提供视频。添加 mp4 作为 ffmpeg 的直接输入,即-vn -i BigBuckBunny.mp4

标签: python opencv audio ffmpeg stream


【解决方案1】:

假设您确实需要对视频使用 OpenCV,您必须按照 Gyan 的评论将音频直接添加到 FFmpeg,因为 OpenCV 不支持音频。

-re 参数可能是实时流式传输所必需的。


为了测试,我将 RTMP URL 从 YouTube 修改为 localhost。
FFplay 子进程用于抓流(用于测试)。

完整的代码示例:

import subprocess as sp
import cv2

#rtmpUrl = "rtmp://a.rtmp.youtube.com/live2/key"
rtmp_url = "rtmp://127.0.0.1:1935/live/test"  # Use localhost for testing
camera_path = "BigBuckBunny.mp4"
cap = cv2.VideoCapture(camera_path)

# Get video information
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Start the TCP server first, before the sending client (for testing).
ffplay_process = sp.Popen(['ffplay', '-listen', '1', '-i', rtmp_url])  # Use FFplay sub-process for receiving the RTMP video.

# ffmpeg command
# OpenCV does not support audio.
command = ['ffmpeg',
        '-y',
        '-re', # '-re' is requiered when streaming in "real-time"
        '-f', 'rawvideo',
        #'-thread_queue_size', '1024',  # May help https://stackoverflow.com/questions/61723571/correct-usage-of-thread-queue-size-in-ffmpeg
        '-vcodec','rawvideo',
        '-pix_fmt', 'bgr24',
        '-s', "{}x{}".format(width, height),
        '-r', str(fps),
        '-i', '-',
        '-vn', '-i', camera_path,  # Get the audio stream without using OpenCV
        '-c:v', 'libx264',
        '-pix_fmt', 'yuv420p',
        '-preset', 'ultrafast',
        # '-c:a', 'aac',  # Select audio codec
        '-bufsize', '64M',  # Buffering is probably required
        '-f', 'flv', 
        rtmp_url]

# Pipeline configuration
p = sp.Popen(command, stdin=sp.PIPE)

# read webcamera
while (cap.isOpened()):
    ret, frame = cap.read()
    if not ret:
        print("End of input file")
        break

    # write to pipe
    p.stdin.write(frame.tobytes())

p.stdin.close()  # Close stdin pipe
p.wait()

ffplay_process.kill()  # Forcefully close FFplay sub-process

【讨论】:

  • 非常感谢,但我还有一个问题。如何在 FFmpeg 中获取麦克风声音?
  • 请做一些研究。您必须先识别设备。之后在'-i'之后添加设备
  • 您好,我遇到了一个问题,即声音的输出速度比图像帧快,反之亦然,请问如何解决?
  • 我认为“-re”应该可以解决它。尝试在每个“-i”之前添加两次“-re”。如果它不起作用,您可以发布一个新问题。
  • "Microphone Array (Intel® Smart Sound Technology for Digital Microphones)" 这是我的音频设备的名称;如何将其添加到命令中?
猜你喜欢
  • 2021-10-02
  • 2020-10-27
  • 2020-01-03
  • 2013-12-11
  • 2016-03-14
  • 2013-01-11
  • 2016-02-26
  • 2018-07-14
  • 2016-05-22
相关资源
最近更新 更多