python处理mp3音频文件
最近在做英语单词的lrc字幕文件时,有个需求:需要找出mp3中的静默起始时间,网上搜索了相关资料,搞了个python实现如下:
其中 detect_silence(sound,300,-35,1)中的两个参数300,-35分别对应搜索最小时间长度和静音阈值,需根据音频文件选取。
from pydub import AudioSegment
from pydub.silence import detect_silence
import re
sound = AudioSegment.from_file("P1.mp3", format="mp3")
start_end = detect_silence(sound,300,-35,1)
str1 = "\n".join('%s' %id for id in start_end)
str2 = re.findall(r" (.+?)]",str1)
str3 = "\n".join('%s' %id for id in str2)
str4 = []
idd = 0
for sss in str3.split():
idd = int(sss) - idd
m,ms = divmod(float(sss),60000)
s,ms = divmod(float(ms),1000)
ts="%02d:%02d.%03d" % (m,s,ms)
if(idd>1100):
str4.append(ts)
idd = int(sss)
#print(str4)
str5 = "\n".join('[%s]' %id for id in str4)
with open('Q2lrc.txt', 'w') as f:
f.write(str5)
————————————————
版权声明:本文为CSDN博主「at91rm9200」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: