@@ -27,7 +27,7 @@ def compression_ratio(text) -> float:
2727 return len (text ) / len (zlib .compress (text .encode ("utf-8" )))
2828
2929
30- def format_timestamp (seconds : float ):
30+ def format_timestamp (seconds : float , always_include_hours : bool = False ):
3131 assert seconds >= 0 , "non-negative timestamp expected"
3232 milliseconds = round (seconds * 1000.0 )
3333
@@ -40,7 +40,8 @@ def format_timestamp(seconds: float):
4040 seconds = milliseconds // 1_000
4141 milliseconds -= seconds * 1_000
4242
43- return (f"{ hours } :" if hours > 0 else "" ) + f"{ minutes :02d} :{ seconds :02d} .{ milliseconds :03d} "
43+ hours_marker = f"{ hours } :" if always_include_hours or hours > 0 else ""
44+ return f"{ hours_marker } { minutes :02d} :{ seconds :02d} .{ milliseconds :03d} "
4445
4546
4647def write_vtt (transcript : Iterator [dict ], file : TextIO ):
@@ -52,3 +53,30 @@ def write_vtt(transcript: Iterator[dict], file: TextIO):
5253 file = file ,
5354 flush = True ,
5455 )
56+
57+
58+ def write_srt (transcript : Iterator [dict ], file : TextIO ):
59+ """
60+ Write a transcript to a file in SRT format.
61+
62+ Example usage:
63+ from pathlib import Path
64+ from whisper.utils import write_srt
65+
66+ result = transcribe(model, audio_path, temperature=temperature, **args)
67+
68+ # save SRT
69+ audio_basename = Path(audio_path).stem
70+ with open(Path(output_dir) / (audio_basename + ".srt"), "w", encoding="utf-8") as srt:
71+ write_srt(result["segments"], file=srt)
72+ """
73+ for i , segment in enumerate (transcript , start = 1 ):
74+ # write srt lines
75+ print (
76+ f"{ i } \n "
77+ f"{ format_timestamp (segment ['start' ], always_include_hours = True )} --> "
78+ f"{ format_timestamp (segment ['end' ], always_include_hours = True )} \n "
79+ f"{ segment ['text' ].strip ().replace ('-->' , '->' )} \n " ,
80+ file = file ,
81+ flush = True ,
82+ )
0 commit comments