如何使用 wavio 将双通道 wav 文件的每个通道分成两个不同的文件?还是另一个图书馆?

How do you separate each channel of a two channel wav file into two different files using wavio? or another library?

下面的脚本可以正常播放原文件。 我尝试以明显的方式将每个通道分开,但它不起作用。

import os
import wavio
import numpy
import pyglet

file_name = "guitarup_full.wav"

# I get the file !
File = wavio.read(file_name)
rate = File.rate
# it looks good
print File.data.shape
print rate
# and then the channels:
channel_1 = File.data[:,0]
channel_2 = File.data[:,1]
wavio.write("guitar_channel_1.wav", channel_1, rate )
wavio.write("guitar_channel_2.wav", channel_2, rate )

# now we try to play:
source =  pyglet.resource.media(file_name, streaming=False)
source_ch1 =  pyglet.resource.media("guitar_channel_1.wav", streaming=False)
source_ch2 =  pyglet.resource.media("guitar_channel_2.wav", streaming=False)

#uncomment the one you want to listen.
source.play()
#source_ch1.play()
#source_ch2.play()

pyglet.app.run()

第一个听起来像吉他,第二个和第三个像高斯噪声。 有人可以告诉我它有什么问题吗?

我使用的音频文件是: https://www.freesounds.info/global/wav.php?fileid=11

数据的形状是:(88471, 2) 比率是:44100

此外,如果我在另一个播放器中播放该文件,我也会得到相同的结果:高斯噪声。

注: 对于这个问题,使用 pyglet 是多余的。如果您使用它来调查此问题,请确保文件位于在资源文件夹中注册的文件夹中。为此:

pyglet.resource.path.append("your_sounds_location")
pyglet.resource.reindex()

在命令行上尝试 ffmpeg:

将立体声输入中的每个通道输出到单独的单声道文件:

ffmpeg -i stereo.wav -map_channel 0.0.0 left.wav -map_channel 0.0.1 right.wav

我在你的文件上试过了,它似乎有效。

如果您需要它在 python 程序中,只需使用 os.system(cmd)

我不知道 wavio 代码有什么问题,但这是使用标准 Python 模块 wave 分离 WAV 通道的方法(wavio 也使用它) 和 numpy:

import wave
import numpy as np

def save_wav_channel(fn, wav, channel):
    '''
    Take Wave_read object as an input and save one of its
    channels into a separate .wav file.
    '''
    # Read data
    nch   = wav.getnchannels()
    depth = wav.getsampwidth()
    wav.setpos(0)
    sdata = wav.readframes(wav.getnframes())

    # Extract channel data (24-bit data not supported)
    typ = { 1: np.uint8, 2: np.uint16, 4: np.uint32 }.get(depth)
    if not typ:
        raise ValueError("sample width {} not supported".format(depth))
    if channel >= nch:
        raise ValueError("cannot extract channel {} out of {}".format(channel+1, nch))
    print ("Extracting channel {} out of {} channels, {}-bit depth".format(channel+1, nch, depth*8))
    data = np.fromstring(sdata, dtype=typ)
    ch_data = data[channel::nch]

    # Save channel to a separate file
    outwav = wave.open(fn, 'w')
    outwav.setparams(wav.getparams())
    outwav.setnchannels(1)
    outwav.writeframes(ch_data.tostring())
    outwav.close()

wav = wave.open(WAV_FILENAME)
save_wav_channel('ch1.wav', wav, 0)
save_wav_channel('ch2.wav', wav, 1)

感谢大家花时间阅读和 运行 我的脚本。我终于找到了解决办法。按照以下顺序执行步骤:

首先,通过以下方式卸载 wavio:

pip uninstall wavio

其次,也卸载numpy:

pip uninstall numpy

最后,再次安装wavio。 numpy 将作为依赖项安装:

pip install wavio

numpy 将作为依赖项安装。

Installing collected packages: numpy, wavio
Successfully installed numpy-1.14.5 wavio-0.0.4

然后我的原始脚本将提供所需的解决方案。

@Marichyasana 建议使用 ffmpeg 是一个很好的解决方法。但是 python 库(例如 pyglet、python 音频工具等)在读取这些输出文件时遇到问题。

我希望这个解决方案对大家有帮助!

方法一 (使用 ffmpeg)

您可以像这样使用 ffmpeg 拆分 n 声道 .wav:

ffmpeg -i input.wav -map 0:1 1.wav -map 0:2 2.wav -map 0:3 3.wav........

您可以 运行 这是 python 使用:

import os
os.system(ffmpeg -i 14ch.mov -map 0:1 1.wav -map 0:2 2.wav -map 0:3 3.wav........)

方法二(使用sox)

here

安装 sox

然后试试这个:

sox infile.wav outfile_left.wav remix 1
sox infile.wav outfile_right.wav remix 2

您可以再次在 python 中使用 os.system

运行

我认为您试图让它变得比需要的复杂得多。如果您不介意使用 scipy,那么将频道分开为:

from scipy.io import wavfile

fs, data = wavfile.read('guitarup_full.wav')            # reading the file

wavfile.write('guitar_channel_1.wav', fs, data[:, 0])   # saving first column which corresponds to channel 1
wavfile.write('guitar_channel_2.wav', fs, data[:, 1])   # saving second column which corresponds to channel 2