Some audio is dialogue between the parties, it may be necessary to make separate audio channels.
Sample code is as follows:
. 1 # ! / Usr / bin / Python the env 2 # - * - Coding: UTF-. 8 - * - . 3 "" " . 4 audio dual channel separation . 5 " "" . 6 Import SYS . 7 Import numpy AS NP . 8 from scipy.io Import wavfile . 9 from Converter Import mp3_to_wav 10 . 11 12 is DEF split_channel (wav_path, left_wav_path, right_wav_path): 13 is "" " 14 channel separator 15 : param wav_path: wav audio path 16 : param left_wav_path: left channel audio path wav 17 :param right_wav_path: 右声道的wav音频路径 18 :return None: 19 """ 20 try: 21 sample_rate, wav_data = wavfile.read(wav_path) 22 left = [] 23 right = [] 24 for item in wav_data: 25 left.append(item[0]) 26 right.append(item[1]) 27 wavfile.write(left_wav_path, sample_rate, np.array(left)) 28 wavfile.write(right_wav_path, sample_rate, np.array(right)) 29 except IOError as e: 30 print('error is %s' % str(e)) 31 except: 32 print('other error', sys.exc_info()) 33 34 35 if __name__ == '__main__': 36 mp3_to_wav('input/test.mp3', 'tmp/tmp.wav') 37 split_channel('tmp/tmp.wav', 'output/left.wav', 'output/right.wav')
Which calls a custom library, converter.py joined the Code:
1 from pydub import AudioSegment 2 3 4 def mp3_to_wav(source, destin): 5 """ 6 mp3 转 wav 7 :param source: 8 :param destin: 9 :return None: 10 """ 11 data = AudioSegment.from_mp3(source) 12 data.export(destin, format='wav')
This code can be generated wav files exemplary two separate channels.
Note: If the source file format is wav file format conversion process can be omitted.