Spaces:
Running
Running
Fix SRT generation
Browse files
utils.py
CHANGED
@@ -30,7 +30,7 @@ def compression_ratio(text) -> float:
|
|
30 |
return len(text) / len(zlib.compress(text.encode("utf-8")))
|
31 |
|
32 |
|
33 |
-
def format_timestamp(seconds: float, always_include_hours: bool = False):
|
34 |
assert seconds >= 0, "non-negative timestamp expected"
|
35 |
milliseconds = round(seconds * 1000.0)
|
36 |
|
@@ -43,8 +43,8 @@ def format_timestamp(seconds: float, always_include_hours: bool = False):
|
|
43 |
seconds = milliseconds // 1_000
|
44 |
milliseconds -= seconds * 1_000
|
45 |
|
46 |
-
hours_marker = f"{hours}:" if always_include_hours or hours > 0 else ""
|
47 |
-
return f"{hours_marker}{minutes:02d}:{seconds:02d}
|
48 |
|
49 |
|
50 |
def write_txt(transcript: Iterator[dict], file: TextIO):
|
@@ -79,8 +79,8 @@ def write_srt(transcript: Iterator[dict], file: TextIO):
|
|
79 |
# write srt lines
|
80 |
print(
|
81 |
f"{i}\n"
|
82 |
-
f"{format_timestamp(segment['start'], always_include_hours=True)} --> "
|
83 |
-
f"{format_timestamp(segment['end'], always_include_hours=True)}\n"
|
84 |
f"{segment['text'].strip().replace('-->', '->')}\n",
|
85 |
file=file,
|
86 |
flush=True,
|
|
|
30 |
return len(text) / len(zlib.compress(text.encode("utf-8")))
|
31 |
|
32 |
|
33 |
+
def format_timestamp(seconds: float, always_include_hours: bool = False, fractionalSeperator: str = '.'):
|
34 |
assert seconds >= 0, "non-negative timestamp expected"
|
35 |
milliseconds = round(seconds * 1000.0)
|
36 |
|
|
|
43 |
seconds = milliseconds // 1_000
|
44 |
milliseconds -= seconds * 1_000
|
45 |
|
46 |
+
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
47 |
+
return f"{hours_marker}{minutes:02d}:{seconds:02d}{fractionalSeperator}{milliseconds:03d}"
|
48 |
|
49 |
|
50 |
def write_txt(transcript: Iterator[dict], file: TextIO):
|
|
|
79 |
# write srt lines
|
80 |
print(
|
81 |
f"{i}\n"
|
82 |
+
f"{format_timestamp(segment['start'], always_include_hours=True, fractionalSeperator=',')} --> "
|
83 |
+
f"{format_timestamp(segment['end'], always_include_hours=True, fractionalSeperator=',')}\n"
|
84 |
f"{segment['text'].strip().replace('-->', '->')}\n",
|
85 |
file=file,
|
86 |
flush=True,
|