mirror of
https://github.com/denizsafak/abogen.git
synced 2026-11-19 01:00:54 +01:00
Merge pull request #205 from MohammadHijjawi97/fix-output-layout-unique-base
fix(output): use collision-free merged path so repeat conversions don't overwrite
This commit is contained in:
5 files changed
+133
-16
No files matched your search
@@ -1,3 +1,7 @@
|
||||
# Unreleased
|
||||
- Fixed repeat conversions overwriting the previous audiobook and its subtitles. The merged output now gets a `_2`, `_3`, ... suffix when a file with the same name already exists, issue mentioned in [#203](https://github.com/denizsafak/abogen/issues/203).
|
||||
- When no original filename is set, the merged output is now named after the source file (e.g. `my_novel.wav`) instead of `output.wav`.
|
||||
|
||||
# 1.3.0
|
||||
- Special thanks to [@jeremiahsb](https://github.com/jeremiahsb) for his [massive contribution](https://github.com/denizsafak/abogen/pull/120) (>55k lines!) that brought the Web UI, EPUB 3 pipeline, and core architectural improvements to life.
|
||||
- Added an EPUB 3 packaging pipeline that builds media-overlay EPUBs from generated audio and chunk metadata.
|
||||
|
||||
@@ -24,6 +24,7 @@ from abogen.application.conversion_ports import (
|
||||
VoiceResolver,
|
||||
)
|
||||
from abogen.application.conversion_result import ConversionResult
|
||||
from abogen.application.output_layout_service import resolve_merged_path
|
||||
from abogen.domain.audio_sink import open_audio_sink
|
||||
from abogen.domain.conversion_engine import (
|
||||
SegmentStats,
|
||||
@@ -229,7 +230,7 @@ def execute_conversion(
|
||||
audio_sink: Optional[AudioSink] = None
|
||||
audio_path = None
|
||||
if merge_chapters:
|
||||
audio_path = output_layout.audio_dir / f"{_base_name(request)}{request.output_format.dot_ext}"
|
||||
audio_path = resolve_merged_path(output_layout, request)
|
||||
meta = plan.metadata if plan.metadata else None
|
||||
audio_sink = stack.enter_context(
|
||||
open_audio_sink(
|
||||
@@ -622,15 +623,6 @@ def _resolve_voice(
|
||||
)
|
||||
|
||||
|
||||
def _base_name(request: Any) -> str:
|
||||
"""Get base name for output file."""
|
||||
from abogen.domain.output_paths import sanitize_output_stem
|
||||
|
||||
if request.original_filename:
|
||||
return sanitize_output_stem(request.original_filename)
|
||||
return "output"
|
||||
|
||||
|
||||
def _format_heading(title: str, index: int, request: Any) -> str:
|
||||
"""Format chapter heading for TTS."""
|
||||
from abogen.domain.chapter_titles import format_spoken_chapter_title
|
||||
|
||||
@@ -54,12 +54,6 @@ def resolve_output_layout(request: ConversionRequest) -> OutputLayout:
|
||||
else:
|
||||
base_name = "output"
|
||||
|
||||
# Find unique output path
|
||||
allowed_exts = {request.output_format, SubtitleFormat.SRT, SubtitleFormat.ASS, "vtt", "mp4", OutputFormat.M4B}
|
||||
unique_base = resolve_unique_path(
|
||||
parent_dir, base_name, "", allowed_extensions=allowed_exts
|
||||
)
|
||||
|
||||
# Resolve project layout
|
||||
project_root = None
|
||||
audio_dir = parent_dir
|
||||
@@ -73,8 +67,15 @@ def resolve_output_layout(request: ConversionRequest) -> OutputLayout:
|
||||
base_dir=parent_dir,
|
||||
)
|
||||
|
||||
# Find unique output path so repeat conversions don't overwrite prior outputs
|
||||
allowed_exts = {request.output_format, SubtitleFormat.SRT, SubtitleFormat.ASS, "vtt", "mp4", OutputFormat.M4B}
|
||||
unique_base = resolve_unique_path(
|
||||
audio_dir, base_name, "", allowed_extensions=allowed_exts
|
||||
)
|
||||
|
||||
return OutputLayout(
|
||||
parent_dir=parent_dir,
|
||||
merged_path=Path(f"{unique_base}{request.output_format.dot_ext}"),
|
||||
project_root=project_root,
|
||||
audio_dir=audio_dir,
|
||||
subtitle_dir=subtitle_dir,
|
||||
@@ -95,6 +96,8 @@ def resolve_merged_path(
|
||||
Returns:
|
||||
Path to the merged output file
|
||||
"""
|
||||
if layout.merged_path is not None:
|
||||
return layout.merged_path
|
||||
base_name = sanitize_output_stem(
|
||||
request.original_filename or "output"
|
||||
)
|
||||
|
||||
@@ -379,6 +379,29 @@ class TestOutputLayoutService:
|
||||
assert path.name == "book.wav"
|
||||
assert path.parent == Path(tmpdir)
|
||||
|
||||
def test_resolve_merged_path_avoids_overwriting_existing_output(self):
|
||||
"""Repeat conversions get a unique merged path instead of overwriting."""
|
||||
from abogen.application.output_layout_service import (
|
||||
resolve_merged_path,
|
||||
resolve_output_layout,
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
existing = Path(tmpdir) / "book.wav"
|
||||
existing.write_bytes(b"previous output")
|
||||
req = ConversionRequest(
|
||||
direct_text="Hello",
|
||||
voice="M1",
|
||||
save=SaveConfig(mode="custom_folder", output_folder=Path(tmpdir)),
|
||||
original_filename="book.wav",
|
||||
output_format="wav",
|
||||
)
|
||||
layout = resolve_output_layout(req)
|
||||
path = resolve_merged_path(layout, req)
|
||||
|
||||
assert path == Path(tmpdir) / "book_2.wav"
|
||||
assert layout.merged_path == path
|
||||
|
||||
def test_resolve_chapter_path(self):
|
||||
"""Resolve chapter output path."""
|
||||
from abogen.application.output_layout_service import resolve_chapter_path
|
||||
|
||||
@@ -505,6 +505,101 @@ class TestExecuteConversion:
|
||||
assert result.metadata["author"] == "Author"
|
||||
|
||||
|
||||
class TestRepeatConversionOutputPath:
|
||||
"""Repeat conversions must not overwrite an existing merged output."""
|
||||
|
||||
@staticmethod
|
||||
def _run(req: ConversionRequest):
|
||||
from abogen.application.conversion_planner import build_conversion_plan
|
||||
|
||||
plan = build_conversion_plan(req)
|
||||
return execute_conversion(
|
||||
plan,
|
||||
FakeEvents(),
|
||||
FakePipelineProvider(),
|
||||
FakeVoiceResolver(),
|
||||
TTSContext(),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _request(output_dir: Path, **save_kwargs: Any) -> ConversionRequest:
|
||||
return ConversionRequest(
|
||||
direct_text="Hello world",
|
||||
voice="M1",
|
||||
save=SaveConfig(
|
||||
mode="custom_folder", output_folder=output_dir, **save_kwargs
|
||||
),
|
||||
original_filename="book.txt",
|
||||
output_format="wav",
|
||||
)
|
||||
|
||||
def test_existing_output_is_not_overwritten(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
existing = Path(tmpdir) / "book.wav"
|
||||
existing.write_bytes(b"previous output")
|
||||
|
||||
result = self._run(self._request(Path(tmpdir)))
|
||||
|
||||
assert result.audio_path == Path(tmpdir) / "book_2.wav"
|
||||
assert result.audio_path.exists()
|
||||
assert existing.read_bytes() == b"previous output"
|
||||
|
||||
def test_third_conversion_gets_suffix_3(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
first = Path(tmpdir) / "book.wav"
|
||||
second = Path(tmpdir) / "book_2.wav"
|
||||
first.write_bytes(b"first output")
|
||||
second.write_bytes(b"second output")
|
||||
|
||||
result = self._run(self._request(Path(tmpdir)))
|
||||
|
||||
assert result.audio_path == Path(tmpdir) / "book_3.wav"
|
||||
assert first.read_bytes() == b"first output"
|
||||
assert second.read_bytes() == b"second output"
|
||||
|
||||
def test_project_mode_checks_audio_subfolder(self, monkeypatch):
|
||||
import functools
|
||||
|
||||
from abogen.application import output_layout_service
|
||||
from abogen.domain.output_paths import resolve_project_layout
|
||||
|
||||
# Pin the project folder timestamp so both runs share one project folder.
|
||||
monkeypatch.setattr(
|
||||
output_layout_service,
|
||||
"resolve_project_layout",
|
||||
functools.partial(
|
||||
resolve_project_layout, timestamp_fn=lambda: "20260101_000000"
|
||||
),
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
req = self._request(Path(tmpdir), save_as_project=True)
|
||||
|
||||
first = self._run(req)
|
||||
first_bytes = first.audio_path.read_bytes()
|
||||
second = self._run(req)
|
||||
|
||||
assert first.audio_path.parent.name == "audio"
|
||||
assert first.audio_path.name == "book.wav"
|
||||
assert second.audio_path == first.audio_path.with_name("book_2.wav")
|
||||
assert first.audio_path.read_bytes() == first_bytes
|
||||
|
||||
def test_fallback_name_uses_source_stem(self):
|
||||
"""Without original_filename the merged file is named after the source."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
source = Path(tmpdir) / "my_novel.txt"
|
||||
source.write_text("Hello world", encoding="utf-8")
|
||||
req = ConversionRequest(
|
||||
source_path=source,
|
||||
voice="M1",
|
||||
save=SaveConfig(mode="save_next_to_input"),
|
||||
output_format="wav",
|
||||
)
|
||||
|
||||
result = self._run(req)
|
||||
|
||||
assert result.audio_path == Path(tmpdir) / "my_novel.wav"
|
||||
|
||||
|
||||
class TestHeadingDedup:
|
||||
"""Tests for heading dedup in executor."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user