Created
March 14, 2016 11:14
-
-
Save wkcn/16c671e4a0684efdf285 to your computer and use it in GitHub Desktop.
一个简单的英语音频分割
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
from pydub import AudioSegment | |
from pydub.silence import split_on_silence | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pyaudio | |
import os | |
file_folder = 'D:/Mirai/English/3L英语第二册/' | |
def Divide(file_name): | |
id = int(file_name.split('/')[-1].split('_')[-2]) | |
out_name = '3L_Unit_%.3d' % id | |
sound = AudioSegment.from_mp3(file_name) | |
data = np.fromstring(sound.raw_data,np.int16) | |
#plt.plot(data) | |
#plt.show() | |
chunks = split_on_silence(sound, min_silence_len = 500, silence_thresh = -6000) # 小于-8000时无法分割 | |
''' | |
p = pyaudio.PyAudio() | |
stream = p.open(format = p.get_format_from_width(sound.sample_width), | |
channels = sound.channels, | |
rate = sound.frame_rate, | |
output = True) | |
''' | |
silence = AudioSegment.silent(duration=1500) | |
os.system('mkdir ' + out_name) | |
for i in range(1,len(chunks)): | |
#cdata = chunks[i].raw_data | |
#stream.write(cdata) | |
name = out_name + '//' + out_name + '_%.2d' %i + '.mp3' | |
u = chunks[i] + silence | |
u.export(name, format = 'mp3') | |
#raw_input("next %d"% i) | |
''' | |
stream.close() | |
p.terminate() | |
''' | |
for w in os.listdir(file_folder): | |
Divide(file_folder + w) | |
print 'ok',w |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment