Export to srt file #93
jooni22
started this conversation in
Show and tell
Replies: 3 comments 1 reply
-
Thank you, this is useful. People will be able to find this issue by searching for "srt". |
Beta Was this translation helpful? Give feedback.
1 reply
-
let's combine it with a progress bar from #80 import faster_whisper
import math
from tqdm import tqdm
model = faster_whisper.WhisperModel("large-v2", device="cuda")
def convert_to_hms(seconds: float) -> str:
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
milliseconds = math.floor((seconds % 1) * 1000)
output = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02},{milliseconds:03}"
return output
def convert_seg(segment: faster_whisper.transcribe.Segment) -> str:
return f"{convert_to_hms(segment.start)} --> {convert_to_hms(segment.end)}\n{segment.text.lstrip()}\n\n"
segments, info = model.transcribe("file.mp4")
full_txt = []
timestamps = 0.0 # for progress bar
with tqdm(total=info.duration, unit=" audio seconds") as pbar:
for i, segment in enumerate(segments, start=1):
full_txt.append(f"{i}\n{convert_seg(segment)}")
pbar.update(segment.end - timestamps)
timestamps = segment.end
if timestamps < info.duration: # silence at the end of the audio
pbar.update(info.duration - timestamps)
with open("file.srt", mode="w", encoding="UTF-8") as f:
f.writelines(full_txt) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another method using the pysubs2 library: from faster_whisper import WhisperModel
import pysubs2
model = WhisperModel(model_size = 'large-v2')
segments, _ = model.transcribe(audio='audio.mp3')
# to use pysubs2, the argument must be a segment list-of-dicts
results= []
for s in segments:
segment_dict = {'start':s.start,'end':s.end,'text':s.text}
results.append(segment_dict)
subs = pysubs2.load_from_whisper(results)
#save srt file
subs.save(file_name+'.srt')
#save ass file
subs.save(file_name+'.ass') |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, really great implementation, congratulations. The only thing I missed was saving to a format such as srt or vtt, I decided to add such a function for myself. I'm not a programmer, that's why I threw everything in as code that runs your implementation. Maybe someday someone will ask about it then you will have a link to the finished solution, or you can always add it to the README. You can close the issue right away, there is no reason for it to be open, your repository lacks a "Discussion" section. Thanks a lot, regards.
Beta Was this translation helpful? Give feedback.
All reactions