1. 程式人生 > >python處理mp3音訊檔案:搜尋靜音(空白)時間

python處理mp3音訊檔案:搜尋靜音(空白)時間

最近在做英語單詞的lrc字幕檔案時,有個需求:需要找出mp3中的靜默起始時間,網上搜索了相關資料,搞了個python實現如下:

from pydub import AudioSegment from pydub.silence import detect_silence import re

sound = AudioSegment.from_file("Q1.mp3", format="mp3")

start_stop = detect_silence(sound,100,-16,1)

str1 = "\n".join('%s' %id for id in start_stop)

str2 = re.findall(r" (.+?)]",str1)

str3 = "\n".join('%s' %id for id in str2) str4 = [] for sss in str3.split():     m,ms = divmod(float(sss),60000)     s,ms = divmod(float(ms),1000)     ts="%02d:%02d.%03d" % (m,s,ms)     str4.append(ts)

print(str4)

str5 = "\n".join('[%s]' %id for id in str4)

with open('Q1.txt', 'w') as f:         f.write(str5)