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



下面的脚本播放原始文件ok。我试图以显而易见的方式将每个频道分开,但它不起作用。

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是多余的。如果使用它来调查此问题,请确保文件位于resources文件夹中注册的文件夹中。要做到这一点:

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

我不知道wavio代码出了什么问题,但以下是如何使用标准Python模块wave(wavio也使用它(和numpy:来分离WAV通道

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)

我认为您正试图使它比需要的复杂得多。如果您不介意使用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

在命令行上尝试ffmpeg:

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

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

我在你的档案上试过了,似乎有效。

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

方法1(使用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........)

方法2(使用sox(

从这里安装sox

然后试试这个:

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

同样,您可以使用os.system在python中运行此操作

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

首先,卸载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音频工具等(在读取回这些输出文件时存在问题。

我希望人们发现这个解决方案有帮助!

最新更新