diff options
Diffstat (limited to 'converter/converter.py')
| -rw-r--r-- | converter/converter.py | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/converter/converter.py b/converter/converter.py index d5a1096..bc64f38 100644 --- a/converter/converter.py +++ b/converter/converter.py @@ -8,7 +8,7 @@ import traceback from collections import Counter from datetime import datetime from pathlib import Path -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Tuple from . import audio, chunking, config, extractors from .tts import QwenTTSClient @@ -143,15 +143,22 @@ class AudiobookConverter: """ chapter_files = [] titles = [] + total_chapters = len(sections) for index, section in enumerate(sections, 1): chapter_path = config.CHUNKS_FOLDER / f"chapter_{index:04d}.wav" + title = (section.title or "").strip() or f"Chapter {index}" + print(f"\n{'=' * 50}") + print(f"CHAPTER {index}/{total_chapters}: {title}") + print(f"{'=' * 50}") + logger.info("Converting chapter %d/%d: %s", index, total_chapters, title) if not self._convert_text(section.text, chapter_path, time.time(), - speed=1.0, output_format="wav"): + speed=1.0, output_format="wav", + chapter=(index, total_chapters)): logger.warning("Skipping chapter %d (%s) due to conversion failure", - index, section.title) + index, title) continue chapter_files.append(chapter_path) - titles.append(section.title or f"Chapter {index}") + titles.append(title) if not chapter_files: logger.error("No chapters were successfully converted") @@ -205,8 +212,14 @@ class AudiobookConverter: def _convert_text(self, text: str, output_path: Path, start_time: float, speed: Optional[float] = None, - output_format: Optional[str] = None) -> bool: - """Chunk, synthesize, and assemble ``text`` into ``output_path``.""" + output_format: Optional[str] = None, + chapter: Optional[Tuple[int, int]] = None) -> bool: + """Chunk, synthesize, and assemble ``text`` into ``output_path``. + + When ``chapter`` (a ``(number, total)`` pair) is given, the output is + an intermediate per-chapter file and progress messages are phrased + accordingly instead of implying the whole book is done. + """ if speed is None: speed = self.speed if output_format is None: @@ -246,14 +259,22 @@ class AudiobookConverter: # Combine chunks (only the successful ones) success = audio.combine_chunks(total_chunks, output_path, chunk_results=results, - speed=speed, output_format=output_format) + speed=speed, output_format=output_format, + intermediate=chapter is not None) if success: duration = time.time() - start_time minutes = int(duration // 60) seconds = int(duration % 60) - logger.info("Conversion completed in %dm %ds: %s", minutes, seconds, output_path) - print(f"[SUCCESS] Conversion completed in {minutes}m {seconds}s") + if chapter is not None: + logger.info("Chapter %d/%d converted in %dm %ds (%d/%d chunks)", + chapter[0], chapter[1], minutes, seconds, + successful_chunks, total_chunks) + print(f"[INFO] Chapter {chapter[0]}/{chapter[1]} converted " + f"({successful_chunks}/{total_chunks} chunks)") + else: + logger.info("Conversion completed in %dm %ds: %s", minutes, seconds, output_path) + print(f"[SUCCESS] Conversion completed in {minutes}m {seconds}s") else: logger.error("Failed to combine chunks into final audiobook") |
