Use , character instead of . for SRT output. (#197)

The SRT format uses the decimal comma character as the fractional separator rather than the decimal point character. Adjust format_timestamp and write_srt to specify the separator character.

See https://en.wikipedia.org/wiki/SubRip#:~:text=the%20fractional%20separator%20used%20is%20the%20comma%2C%20since%20the%20program%20was%20written%20in%20france.
This commit is contained in:
Caleb McQuillin 2022-09-29 23:44:12 -04:00 committed by GitHub
parent 7cb4cc21bf
commit 60132ade70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,7 @@ def compression_ratio(text) -> float:
return len(text) / len(zlib.compress(text.encode("utf-8"))) return len(text) / len(zlib.compress(text.encode("utf-8")))
def format_timestamp(seconds: float, always_include_hours: bool = False): def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = '.'):
assert seconds >= 0, "non-negative timestamp expected" assert seconds >= 0, "non-negative timestamp expected"
milliseconds = round(seconds * 1000.0) milliseconds = round(seconds * 1000.0)
@ -41,7 +41,7 @@ def format_timestamp(seconds: float, always_include_hours: bool = False):
milliseconds -= seconds * 1_000 milliseconds -= seconds * 1_000
hours_marker = f"{hours}:" if always_include_hours or hours > 0 else "" hours_marker = f"{hours}:" if always_include_hours or hours > 0 else ""
return f"{hours_marker}{minutes:02d}:{seconds:02d}.{milliseconds:03d}" return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
def write_txt(transcript: Iterator[dict], file: TextIO): def write_txt(transcript: Iterator[dict], file: TextIO):
@ -79,8 +79,8 @@ def write_srt(transcript: Iterator[dict], file: TextIO):
# write srt lines # write srt lines
print( print(
f"{i}\n" f"{i}\n"
f"{format_timestamp(segment['start'], always_include_hours=True)} --> " f"{format_timestamp(segment['start'], always_include_hours=True, decimal_marker=',')} --> "
f"{format_timestamp(segment['end'], always_include_hours=True)}\n" f"{format_timestamp(segment['end'], always_include_hours=True, decimal_marker=',')}\n"
f"{segment['text'].strip().replace('-->', '->')}\n", f"{segment['text'].strip().replace('-->', '->')}\n",
file=file, file=file,
flush=True, flush=True,