如何使用 pyav 和 python 将 mp3 转换为 wav?

How to convert mp3 to wav with pyav and python?

我用python和pyav把mp3转成wav。 我的代码如下: '''

def mp3_to_wav(mp3_path, wav_path):
 inp = av.open(mp3_path, 'r')
 out = av.open(wav_path, 'w')
 ostream = out.add_stream("pcm_s16le")

    for frame in inp.decode(audio=0):
        frame.pts = None

        for p in ostream.encode(frame):
            out.mux(p)

    for p in ostream.encode(None):
        out.mux(p)

    out.close()
}}

''' 但是 pycharm 告诉我 流 0 的数据包中的时间戳未设置。这已被弃用,将来将停止工作。修复您的代码以正确设置时间戳 编码器没有产生正确的点,弥补了一些。

我该怎么办?

非常感谢。

    
# noinspection PyUnresolvedReferences
def to_wav(in_path: str, out_path: str = None, sample_rate: int = 16000) -> str:
    """Arbitrary media files to wav"""
    if out_path is None:
        out_path = os.path.splitext(in_path)[0] + '.wav'
    with av.open(in_path) as in_container:
        in_stream = in_container.streams.audio[0]
        with av.open(out_path, 'w', 'wav') as out_container:
            out_stream = out_container.add_stream(
                'pcm_s16le',
                rate=sample_rate,
                layout='mono'
            )
            for frame in in_container.decode(in_stream):
                for packet in out_stream.encode(frame):
                    out_container.mux(packet)

    return out_path