1

我正在尝试使用Pydub库从 mp3 文件中生成带有G. 711 alaw压缩的 wav 文件。wav 文件正在生成,但未重新采样到频率 8 kHz。我试过以下代码:

from_path = '/home/nikhil/Music/m1.mp3' #this is a mp3 file
to_path = '/home/nikhil/Music/m1.wav' #resulted file
from_format = 'mp3'
to_format = 'wav'
params = ["-acodec", "pcm_alaw", "-ar", "8000"]
AudioSegment.from_file(from_path, from_format).export(to_path, format=to_format, parameters=params)

有人能帮我吗?

4

1 回答 1

2

我正在查看导出方法中的代码,我意识到当输出格式为 "wav" 时不使用 ffmpeg

由于 wav 在内部使用,它只是将音频的内存版本直接写入磁盘(这样做是为了使 ffmpeg 成为可选依赖项,如果您只需要 wav 支持,则不需要安装它)。

我有两个想法可以让你解决这个问题:

  1. 使用不同的格式 kwarg,例如“pcm”。我不确定这是否可行,而且我当前的机器上没有 ffmpeg 可供测试,但绝对值得一试。

    from_path = '/home/nikhil/Music/m1.mp3' #this is a mp3 file
    to_path = '/home/nikhil/Music/m1.wav' #resulted file
    from_format = 'mp3'
    to_format = 'pcm'
    params = ["-acodec", "pcm_alaw", "-ar", "8000"]
    AudioSegment.from_file(from_path, from_format).export(to_path, format=to_format, parameters=params)
    
  2. 使用 pydub 的内部机制重新采样到 8kHz:同样,我目前无法正确测试...

    from_path = '/home/nikhil/Music/m1.mp3' #this is a mp3 file
    to_path = '/home/nikhil/Music/m1.wav' #resulted file
    
    seg = AudioSegment.from_mp3(from_path)
    seg = seg.set_frame_rate(8000)
    seg.export(to_path, format="wav")
    
于 2013-12-02T19:56:04.517 回答