我不想使用其它应用程序(如sox) - 我想在纯Python中完成此操作。安装所需的Python库是可以的。
我维护一个开源库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。
wave
。打开源文件和目标文件,读取样本,对通道进行平均处理,然后将其写出。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)