简体   繁体   English

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

[英]How to convert mp3 to wav with pyav and python?

I use python and pyav to convert mp3 to wav.我使用 python 和 pyav 将 mp3 转换为 wav。 My code is below: '''我的代码如下:'''

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()
}}

''' but pycharm tell me that Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. ''' 但 pycharm 告诉我,流 0 的数据包中未设置时间戳。这已被弃用,将来将停止工作。 Fix your code to set the timestamps properly Encoder did not produce proper pts, making some up.修复您的代码以正确设置时间戳编码器没有产生正确的点,弥补了一些。

How should I do?我应该怎么做?

Thank you very much.非常感谢。

    
# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM