如何在Python中将立体声WAV文件转换为单声道?

28

我不想使用其它应用程序(如sox) - 我想在纯Python中完成此操作。安装所需的Python库是可以的。

3个回答

59

我维护一个开源库pydub,它使这件事相当简单。

from pydub import AudioSegment
sound = AudioSegment.from_wav("/path/to/file.wav")
sound = sound.set_channels(1)
sound.export("/output/path.wav", format="wav")

提醒一点:它使用ffmpeg来处理音频格式转换,但如果您只使用wav格式,则可以纯粹使用Python。


当然应该使用set_channels(1)将立体声转换为单声道。谢谢! - brennanyoung
我喜欢这个包的简单易用性。在单声道输出上尝试了使用Google语音API。与使用scipy分离通道相比,使用pydub转录效果较差。虽然没有进行系统化测试。 - yangliu2

11
如果WAV文件是PCM编码的话,您可以使用wave。打开源文件和目标文件,读取样本,对通道进行平均处理,然后将其写出。

1
似乎我的尝试中无法安装它,但我能够让Jiaaro的pydub工作。 - Shane
不需要安装任何东西。它随附于Python中。 - Ignacio Vazquez-Abrams
您不需要设置参数。它们将从文件中读取。 - Ignacio Vazquez-Abrams

0
我能想到的最简单的方法是使用PyTorch的mean函数,就像下面的示例一样。
import torch
import torchaudio

def stereo_to_mono_convertor(signal):
    # If there is more than 1 channel in your audio
    if signal.shape[0] > 1:
        # Do a mean of all channels and keep it in one channel
        signal = torch.mean(signal, dim=0, keepdim=True)
    return signal

# Load audio as tensor
waveform, sr = torchaudio.load('audio.wav')
# Convert it to mono channel
waveform = stereo_to_mono_convertor(waveform)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接