Spaces:
Running
Running
jhj0517
commited on
Commit
·
e76bbe3
1
Parent(s):
482e6f7
Add lrc writer
Browse files
modules/utils/subtitle_manager.py
CHANGED
|
@@ -280,6 +280,42 @@ class WriteSRT(SubtitlesWriter):
|
|
| 280 |
return segments
|
| 281 |
|
| 282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
class WriteTSV(ResultWriter):
|
| 284 |
"""
|
| 285 |
Write a transcript to a file in TSV (tab-separated values) format containing lines like:
|
|
|
|
| 280 |
return segments
|
| 281 |
|
| 282 |
|
| 283 |
+
class WriteLRC(SubtitlesWriter):
|
| 284 |
+
extension: str = "lrc"
|
| 285 |
+
always_include_hours: bool = False
|
| 286 |
+
decimal_marker: str = "."
|
| 287 |
+
|
| 288 |
+
def write_result(
|
| 289 |
+
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
| 290 |
+
):
|
| 291 |
+
for i, (start, end, text) in enumerate(
|
| 292 |
+
self.iterate_result(result, options, **kwargs), start=1
|
| 293 |
+
):
|
| 294 |
+
print(f"[{start}]{text}[{end}]\n", file=file, flush=True)
|
| 295 |
+
|
| 296 |
+
def to_segments(self, file_path: str) -> List[Segment]:
|
| 297 |
+
segments = []
|
| 298 |
+
|
| 299 |
+
blocks = read_file(file_path).split('\n')
|
| 300 |
+
|
| 301 |
+
for block in blocks:
|
| 302 |
+
if block.strip() != '':
|
| 303 |
+
lines = block.strip()
|
| 304 |
+
pattern = r'(\[.*?\])'
|
| 305 |
+
parts = re.split(pattern, lines)
|
| 306 |
+
start_str, sentence, end_str = [part.strip() for part in parts if part]
|
| 307 |
+
start_str, end_str = start_str.replace("[", "").replace("]", ""), end_str.replace("[", "").replace("]", "")
|
| 308 |
+
start, end = time_str_to_seconds(start_str, self.decimal_marker), time_str_to_seconds(end_str, self.decimal_marker)
|
| 309 |
+
|
| 310 |
+
segments.append(Segment(
|
| 311 |
+
start=start,
|
| 312 |
+
end=end,
|
| 313 |
+
text=sentence
|
| 314 |
+
))
|
| 315 |
+
|
| 316 |
+
return segments
|
| 317 |
+
|
| 318 |
+
|
| 319 |
class WriteTSV(ResultWriter):
|
| 320 |
"""
|
| 321 |
Write a transcript to a file in TSV (tab-separated values) format containing lines like:
|