Skip to content

Commit ead77fa

Browse files
fcakyonjongwook
andauthored
add srt subtitle export utility (openai#102)
* add srt subtitle export utility * simplifying Co-authored-by: Jong Wook Kim <jongwook@nyu.edu>
1 parent 5485428 commit ead77fa

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

whisper/transcribe.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .audio import SAMPLE_RATE, N_FRAMES, HOP_LENGTH, pad_or_trim, log_mel_spectrogram
1111
from .decoding import DecodingOptions, DecodingResult
1212
from .tokenizer import LANGUAGES, TO_LANGUAGE_CODE, get_tokenizer
13-
from .utils import exact_div, format_timestamp, optional_int, optional_float, str2bool, write_vtt
13+
from .utils import exact_div, format_timestamp, optional_int, optional_float, str2bool, write_vtt, write_srt
1414

1515
if TYPE_CHECKING:
1616
from .model import Whisper
@@ -301,6 +301,10 @@ def cli():
301301
with open(os.path.join(output_dir, audio_basename + ".vtt"), "w", encoding="utf-8") as vtt:
302302
write_vtt(result["segments"], file=vtt)
303303

304+
# save SRT
305+
with open(os.path.join(output_dir, audio_basename + ".srt"), "w", encoding="utf-8") as srt:
306+
write_srt(result["segments"], file=srt)
307+
304308

305309
if __name__ == '__main__':
306310
cli()

whisper/utils.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4647
def 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

Comments
 (0)