python - 如何在Python中将内存中的WebM音频文件转换为mp3音频文件?

标签 python audio ffmpeg pyav

我正在尝试将 WebM 格式的音频文件转换为 MP3 文件。由于服务器限制,我需要将这两个文件保留在内存中。我尝试使用 PyAv 来做到这一点,但在处理流后我无法接收类似 Python 文件的对象。 我的尝试:

    with av.open(webm_file, 'r') as inp:
        f = SpooledTemporaryFile(mode="w+b")
        with av.open(f, 'w', format="mpeg") as out:
            out_stream = out.add_stream("mp3")
            for frame in inp.decode(audio=0):
                frame.pts = None
                for packets in out_stream.encode(frame):
                    out.mux(packets)
            for packets in out_stream.encode(None):
                out.mux(packets)

webm_file 是 SpooledTemporaryFile 类型,但我无法像文件一样获取 outfile,有人可以帮忙吗?

最佳答案

format="mpeg" 替换为 format="mp3"

mpeg应用MPEG-1节目流,我们要创建mp3容器格式。
mpeg 容器支持 mp3 编解码器作为音频流,但它不是我们正在寻找的容器...

我想用“mp3”替换“mpeg”可以解决这个问题。


我不知道如何测试f = SpooledTemporaryFile(mode="w+b"),所以我改用输入和输出文件。

首先创建包含合成音频和视频的 WebM 文件(使用 FFmpeg CLI)进行测试:

ffmpeg -y -f lavfi -i testrc=size=192x108:rate=1 -f lavfi -i 正弦=频率=100 -f lavfi -i 正弦=频率=500 -map_channel 1.0.0 -map_channel 2.0.0 -vcodec libvpx-vp9 -crf 32 -acodec libopus -b:a 96K -ar 48000 -ac 2 -t 10s input.webm


使用 PyAV 将音频 channel 从 opus 编解码器转码为 output.mp3 文件中的 mp3 编解码器:

import av
#from tempfile import SpooledTemporaryFile

# Build input file for testing:
# ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1 -f lavfi -i sine=frequency=500 -f lavfi -i sine=frequency=800 -map_channel 1.0.0 -map_channel 2.0.0 -vcodec libvpx-vp9 -crf 32 -acodec libopus -b:a 96K -ar 48000 -ac 2 -t 10s input.webm

webm_file = "input.webm"
mp3_file = "output.mp3"

with av.open(webm_file, 'r') as inp:
    #f = SpooledTemporaryFile(mode="w+b")
    f = mp3_file
    with av.open(f, 'w', format="mp3") as out:  # Open file, setting format to mp3
        out_stream = out.add_stream("mp3")
        for frame in inp.decode(audio=0):
            frame.pts = None
            for packets in out_stream.encode(frame):
                out.mux(packets)
        for packets in out_stream.encode(None):
            out.mux(packets)

MediaInfo 工具的输出:

General
Complete name                            : C:\Tmp\output.mp3
Format                                   : MPEG Audio
File size                                : 157 KiB
Duration                                 : 10 s 32 ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 128 kb/s
Writing library                          : LAME3.100

Audio
Format                                   : MPEG Audio
Format version                           : Version 1
Format profile                           : Layer 3
Format settings                          : Joint stereo / MS Stereo
Duration                                 : 10 s 32 ms
Bit rate mode                            : Variable
Bit rate                                 : 128 kb/s
Channel(s)                               : 2 channels
Sampling rate                            : 48.0 kHz
Frame rate                               : 41.667 FPS (1152 SPF)
Compression mode                         : Lossy
Stream size                              : 157 KiB (100%)
Writing library                          : LAME3.100

关于python - 如何在Python中将内存中的WebM音频文件转换为mp3音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72679106/

相关文章:

python - 关于karatsuba乘法的问题

python - 设置并选择要浏览其图像的画廊

python - 音频流 Python 上的 Google 流式语音识别

actionscript-3 - AS3 NetStream AppendBytes寻求问题

python - 如何使用numpy的hstack?

javascript - 用Java暂停音频

android - AS3 Shoutcast 流在 AIR 3.2 中不起作用

bash - 如何在 macOS 中永久使 ffmpeg 可从 bin/bash 中识别

Android NDK : Aborting. 停止

python - 如何使用 openpyxl 循环遍历 Excel 工作表的行?