whisper 生成 srt 字幕格式
def _whisper_result_to_srt(result):
text = []
for i, s in enumerate(result['segments']):
text.append(str(i+1))time_start = s['start']
hours, minutes, seconds = int(
time_start/3600), (time_start/60) % 60, (time_start) % 60
timestamp_start = "%02d:%02d:%06.3f" % (hours, minutes, seconds)
timestamp_start = timestamp_start.replace('.', ',')
time_end = s['end']
hours, minutes, seconds = int(
time_end/3600), (time_end/60) % 60, (time_end) % 60
timestamp_end = "%02d:%02d:%06.3f" % (hours, minutes, seconds)
timestamp_end = timestamp_end.replace('.', ',')
text.append(timestamp_start + " --> " + timestamp_end)
text.append(s['text'].strip() + "\n")
return "\n".join(text)
whisperInstance = whisper.load_model('small')
result = whisperInstance.transcribe(mediaFilePath, language='fr')
srt_file = _whisper_result_to_srt(result)
时间比较
pubTime = datetime.datetime(year, month, day, hour=0, minute=0, second=0,microsecond=0, tzinfo=None, *, fold=0)
days = (datetime.datetime.utcnow() - pubTime).days