ffmpeg 原始视频和音频标准输入

Adr*_*ana 5 python audio video ffmpeg

目前,我使用 ffmpeg 从原始格式的帧开始生成在 OBS 中广播的视频输出。

问题是我们要向其添加音轨,即:每个帧现在都有其像素矩阵及其音频。

我不知道如何让 ffmpeg 接受这个复合信号或者最好的方法是什么。实际上我使用 ffmpeg 的标准输入。我使用这段代码来生成它:

url = "udp://" + udp_address + ":" + str(udp_port)
command = [
        'ffmpeg',
         '-loglevel',
         'error',
         '-re',
         '-y',
         
         # Input
         '-i', '-',
         '-f', 'rawvideo',
         '-vcodec', 'rawvideo',
         '-pix_fmt', 'bgr24',
         '-s', str(size[0]) + 'x' + str(size[1]),
         
         # Output
         '-b:v', '16M',
         '-maxrate', '16M',
         '-bufsize', '16M',
         '-pix_fmt', 'bgr24',
         '-f', 'mpegts', url
 ]

proc = subprocess.Popen(command, stdin=subprocess.PIPE)
proc.stdin.write(frame.tobytes())
Run Code Online (Sandbox Code Playgroud)

向每帧添加音频的最佳方法是什么?谢谢

Dev*_*dse 0

使用 FFMPEG,您可以拥有多个输入,然后使用 -map 标志来选择应使用哪些输入流。

例如,如果您有 1 个视频和 1 个音频片段,您可以使用如下内容:

ffmpeg -i video.mp4 -i audio.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a copy output.mkv
Run Code Online (Sandbox Code Playgroud)

Map 可用于选择不同的输入流。在这种情况下:

  • From the first input, from the video streams, take the first one (0:v:0)
  • From the second input, from the audio streams, take the first one (1:a:0)

在此输入图像描述

To find more information see: https://trac.ffmpeg.org/wiki/Map

-Devedse