Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Created January 25, 2020 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tam17aki/11eb1566a2d48b382607d23dddb98891 to your computer and use it in GitHub Desktop.
Save tam17aki/11eb1566a2d48b382607d23dddb98891 to your computer and use it in GitHub Desktop.
from inaSpeechSegmenter import Segmenter
from pydub import AudioSegment
# 入力のwavファイルのパスを指定
input_file = './input/test.wav'
# 出力のwavファイルのフォルダとプレフィックスまで指定
# → ./output/segment0.wav, ./output/segment1.wav, のような出力を想定
output_file = './output/segment'
# 'smn' は入力信号を音声区間(speeech)、音楽区間(music)、
# ノイズ区間(noise)にラベル付けしてくれる
# detect_genderをTrueにすると、音声区間は男性(male) / 女性(female)のラベルに
# 細分化される
seg = Segmenter(vad_engine='smn', detect_gender=False)
# 区間検出実行(たったこれだけでOK)
segmentation = seg(input_file)
# ('区間ラベル', 区間開始時刻(秒), 区間終了時刻(秒))というタプルが
# リスト化されているのが変数 segmentation
# print(segmentation)
# inaSpeechSegmenter単体では分割されたwavを作成してくれないので、
# pydubのAudioSegmentにお世話になる (ありがたいライブラリ)
speech_segment_index = 0
for segment in segmentation:
# segmentはタプル
# タプルの第1要素が区間のラベル
segment_label = segment[0]
if (segment_label == 'speech'): # 音声区間
# 区間の開始時刻の単位を秒からミリ秒に変換
start_time = segment[1] * 1000
end_time = segment[2] * 1000
# 分割結果をwavに出力
newAudio = AudioSegment.from_wav(input_file)
newAudio = newAudio[start_time:end_time]
output_file = output_file + str(speech_segment_index) + '.wav'
newAudio.export(output_file, format="wav")
speech_segment_index += 1
del newAudio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment