[英]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()
我不知道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)
在命令行上尝试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-channel的.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中运行它
我认为你试图使它变得比它需要的复杂得多。 如果你不介意使用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
感谢大家花时间阅读和运行我的脚本。 我终于找到了解决方案。 按照此顺序中的步骤操作:
首先,卸载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
然后我的原始脚本将提供所需的解决方案。
使用ffmpeg作为@Marichyasana建议是一个很好的解决方法。 但是python库(例如pyglet,python音频工具等)在读取这些输出文件时遇到了问题。
我希望人们觉得这个解决方案很有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.