subtitle_utils¶
subtitle_utils
¶
Subtitle file parsing and serialization utilities.
Supports SRT, VTT (WebVTT), ASS, and SSA formats. Each format has a
parse/serialize pair. The unified parse_subtitle / serialize_subtitle
dispatchers select the correct pair based on file extension.
SubtitleEntry
dataclass
¶
A single subtitle cue with timing metadata and translatable text.
| 属性 | デスクリプション |
|---|---|
index |
Sequential position (0-based).
タイプ:
|
start |
Start timestamp as the original raw string.
タイプ:
|
end |
End timestamp as the original raw string.
タイプ:
|
text |
Translatable text (override tags stripped for ASS/SSA).
タイプ:
|
raw_text |
Original text before tag stripping (ASS/SSA only).
タイプ:
|
metadata |
Format-specific extra data (cue id, cue settings, etc.).
タイプ:
|
mirror_ass_alignment_for_rtl
¶
Mirrors ASS/SSA alignment codes left↔right for an RTL target.
Flips both the per-line override tags (\an1 ↔ \an3 etc.)
and the V4+ Style table's Alignment column. Centre alignments
(\an2/5/8, legacy \a2/6/10) are untouched.
The function is a string-level rewrite — it doesn't validate ASS
structure, so an unrelated Style: row outside [V4+ Styles]
won't be touched (the column count would be wrong) but a malformed
file won't crash either.
ソースコード位置: src/utils/subtitle_utils.py
is_subtitle_format
¶
parse_srt
¶
Parses an SRT file into subtitle entries.
| 引数 | デスクリプション |
|---|---|
content
|
Raw SRT file content.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
list[SubtitleEntry]
|
Tuple of (entries, None). The second element is always |
None
|
because SRT needs no extra data for serialization. |
ソースコード位置: src/utils/subtitle_utils.py
serialize_srt
¶
Reconstructs an SRT file from subtitle entries.
| 引数 | デスクリプション |
|---|---|
entries
|
Subtitle entries with (possibly translated) text.
タイプ:
|
_format_data
|
Unused — present for dispatcher signature consistency.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Complete SRT file content. |
ソースコード位置: src/utils/subtitle_utils.py
_is_vtt_header_block
¶
Returns True if text is a VTT header/meta block (WEBVTT, NOTE, STYLE).
parse_vtt
¶
Parses a WebVTT file into subtitle entries.
Preserves the WEBVTT header, NOTE comments, and STYLE blocks in header so they can be restored during serialization.
| 引数 | デスクリプション |
|---|---|
content
|
Raw VTT file content.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
list[SubtitleEntry]
|
Tuple of (entries, header). header includes everything |
str
|
before the first cue (WEBVTT line, NOTEs, STYLEs). |
ソースコード位置: src/utils/subtitle_utils.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
serialize_vtt
¶
Reconstructs a WebVTT file from entries and the original header.
| 引数 | デスクリプション |
|---|---|
entries
|
Subtitle entries with (possibly translated) text.
タイプ:
|
header
|
Original WEBVTT header block (with NOTEs/STYLEs).
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Complete VTT file content. |
ソースコード位置: src/utils/subtitle_utils.py
_strip_ass_tags
¶
Strips ASS/SSA override tags, preserving visible text.
Tags like {\b1}, {\i1}, {\pos(320,240)} are removed.
| 引数 | デスクリプション |
|---|---|
text
|
Raw ASS dialogue text.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Text with override tags removed. |
ソースコード位置: src/utils/subtitle_utils.py
_restore_ass_tags
¶
Restores leading ASS override tags from original onto translated.
Mid-text tags cannot be reliably repositioned after translation, so only contiguous leading tags are restored.
| 引数 | デスクリプション |
|---|---|
original
|
Original text with override tags.
タイプ:
|
translated
|
Translated text without tags.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Translated text prefixed with the original's leading tags. |
ソースコード位置: src/utils/subtitle_utils.py
parse_ass
¶
Parses an ASS/SSA file into subtitle entries.
Only Dialogue: lines in the [Events] section are treated as
translatable. All other content (sections, comments, styles) is
preserved verbatim in preserved_lines for later serialization.
| 引数 | デスクリプション |
|---|---|
content
|
Raw ASS/SSA file content.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
list[SubtitleEntry]
|
Tuple of (entries, preserved_lines). Dialogue text positions |
list[str]
|
in preserved_lines are replaced with |
tuple[list[SubtitleEntry], list[str]]
|
where N is the entry index. |
ソースコード位置: src/utils/subtitle_utils.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
serialize_ass
¶
Reconstructs an ASS/SSA file by injecting translated text.
Replaces __SUB_N__ placeholders in preserved_lines with the
translated text for each entry, restoring any leading override tags
from the original.
| 引数 | デスクリプション |
|---|---|
entries
|
Subtitle entries with translated text.
タイプ:
|
preserved_lines
|
Lines with placeholders from
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Complete ASS/SSA file content. |
ソースコード位置: src/utils/subtitle_utils.py
parse_subtitle
¶
Dispatches to the format-specific subtitle parser.
| 引数 | デスクリプション |
|---|---|
content
|
Raw file content.
タイプ:
|
suffix
|
Lowercase file extension (e.g.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
list[SubtitleEntry]
|
Tuple of (entries, format_data) where format_data is |
object
|
whatever the format-specific serializer needs. |
| 発生 | デスクリプション |
|---|---|
ValueError
|
If the extension is not a supported subtitle format. |
ソースコード位置: src/utils/subtitle_utils.py
serialize_subtitle
¶
Dispatches to the format-specific subtitle serializer.
| 引数 | デスクリプション |
|---|---|
entries
|
Subtitle entries with translated text.
タイプ:
|
format_data
|
Format-specific data from
タイプ:
|
suffix
|
Lowercase file extension.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Complete file content. |
| 発生 | デスクリプション |
|---|---|
ValueError
|
If the extension is not a supported subtitle format. |