feat: per-chapter subtitle writer in executor

This commit is contained in:
Artem Akymenko
2026-07-24 19:17:37 +03:00
parent a1241ee9ca
commit 2f83d10a1e
+45 -10
View File
@@ -206,6 +206,26 @@ def execute_conversion(
) )
result.chapter_paths.append(chapter_path) result.chapter_paths.append(chapter_path)
# Per-chapter subtitle writer
chapter_subtitle_writer: Optional[SubtitleWriter] = None
if chapter_dir and request.subtitle_mode != SubtitleMode.DISABLED and chapter_sink:
from abogen.infrastructure.subtitle_writer import resolve_subtitle_format
chapter_filename = sanitize_filename_for_chapter(chapter.title, chapter_idx)
subtitle_ext, _ = resolve_subtitle_format(
request.subtitle_format, request.subtitle_mode
)
chapter_subtitle_path = chapter_dir / f"{chapter_filename}.{subtitle_ext}"
chapter_subtitle_writer = make_subtitle_writer(
chapter_subtitle_path,
request.subtitle_format,
request.subtitle_mode,
max_words=request.max_subtitle_words,
)
if chapter_subtitle_writer:
chapter_subtitle_writer.open()
result.subtitle_paths.append(chapter_subtitle_writer.path)
# Intro delay before first chapter # Intro delay before first chapter
if not intro_emitted and plan.intro and plan.intro.enabled: if not intro_emitted and plan.intro and plan.intro.enabled:
# Intro will be emitted with first chapter # Intro will be emitted with first chapter
@@ -281,16 +301,27 @@ def execute_conversion(
) )
# Process subtitles # Process subtitles
if subtitle_writer and audio_sink and accumulated_tokens: if audio_sink and accumulated_tokens:
process_and_write_subtitles( if subtitle_writer:
accumulated_tokens, process_and_write_subtitles(
subtitle_writer, accumulated_tokens,
subtitle_mode=request.subtitle_mode, subtitle_writer,
max_subtitle_words=request.max_subtitle_words, subtitle_mode=request.subtitle_mode,
lang_code=request.language, max_subtitle_words=request.max_subtitle_words,
use_spacy_segmentation=use_spacy, lang_code=request.language,
fallback_end_time=stats.current_time, use_spacy_segmentation=use_spacy,
) fallback_end_time=stats.current_time,
)
if chapter_subtitle_writer:
process_and_write_subtitles(
accumulated_tokens,
chapter_subtitle_writer,
subtitle_mode=request.subtitle_mode,
max_subtitle_words=request.max_subtitle_words,
lang_code=request.language,
use_spacy_segmentation=use_spacy,
fallback_end_time=stats.current_time,
)
# Record chunk marker # Record chunk marker
if segment.source in ("chunk", "voice_marker"): if segment.source in ("chunk", "voice_marker"):
@@ -319,6 +350,10 @@ def execute_conversion(
if chapter_sink: if chapter_sink:
chapter_sink.close() chapter_sink.close()
# Close chapter subtitle writer
if chapter_subtitle_writer:
chapter_subtitle_writer.close()
# Add chapter marker # Add chapter marker
result.chapter_markers.append({ result.chapter_markers.append({
"chapter_index": chapter_idx - 1, "chapter_index": chapter_idx - 1,