diff options
Diffstat (limited to 'audiobook.py')
| -rwxr-xr-x | audiobook.py | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/audiobook.py b/audiobook.py index 97171ac..03ba7c9 100755 --- a/audiobook.py +++ b/audiobook.py @@ -55,8 +55,8 @@ from converter.converter import ( def convert(backend: str = None, voice: str = None, clone: str = None, transcription: str = None, no_transcription: bool = False, - language: str = None, speed: float = 1.0, single_file: bool = False, - output_format: str = None, debug: bool = False, + language: str = None, speed: float = None, single_file: bool = False, + output_format: str = None, debug: bool = None, model_id: str = None, instructions: str = None, request_options: dict = None, input_dir: Path = None, output_dir: Path = None, api_url: str = None, @@ -67,8 +67,10 @@ def convert(backend: str = None, voice: str = None, clone: str = None, Returns the process exit code (0 on success, 1 on failure, 130 on Ctrl-C). BACKEND defaults to config.BACKEND, OUTPUT_FORMAT to config.AUDIO_FORMAT. LANGUAGE is already-normalized where required. - INPUT_DIR/OUTPUT_DIR override the default input/ and output/ folders - when given. API_URL, when given, overrides the configured server URL + INPUT_DIR/OUTPUT_DIR override the configured INPUT_DIR/OUTPUT_DIR + folders when given (relative paths resolve against the project + root); SPEED/DEBUG default to the config.SPEED/config.DEBUG + settings. API_URL, when given, overrides the configured server URL for the selected backend (used by the hub's "[remote]" entries and --api-url). @@ -81,11 +83,15 @@ def convert(backend: str = None, voice: str = None, clone: str = None, """ backend = backend or config.BACKEND output_format = output_format or config.AUDIO_FORMAT + speed = config.SPEED if speed is None else speed + debug = config.DEBUG if debug is None else debug + input_dir = config.INPUT_DIR if input_dir is None else input_dir + output_dir = config.OUTPUT_DIR if output_dir is None else output_dir request_options = request_options or {} - if input_dir is not None: - _converter_mod.BOOKS_FOLDER = Path(input_dir) - if output_dir is not None: - _converter_mod.AUDIOBOOKS_FOLDER = Path(output_dir) + _converter_mod.BOOKS_FOLDER = _converter_mod.resolve_dir( + input_dir, "input") + _converter_mod.AUDIOBOOKS_FOLDER = _converter_mod.resolve_dir( + output_dir, "output") # With a progress callback the run view owns the screen: keep log # records in the file only and let the events carry the state. setup_logging(debug=debug, console=progress is None) @@ -222,8 +228,10 @@ Examples: "the LANGUAGE setting in app/converter/config.py (English).") ) parser.add_argument( - "--speed", type=float, default=1.0, - help="Playback speed factor for the final audiobook (1.0 = normal). Pitch-preserving." + "--speed", type=float, default=None, + help=("Playback speed factor for the final audiobook (1.0 = normal). " + "Pitch-preserving. Defaults to the SPEED setting in " + "app/converter/config.py.") ) parser.add_argument( "--format", choices=list(AUDIO_FORMATS), default=config.AUDIO_FORMAT, @@ -232,11 +240,15 @@ Examples: parser.add_argument( "--input", type=Path, metavar="DIR", default=None, help=("Directory containing the source books (.txt/.pdf/.epub). " - "Defaults to ./input.") + "Defaults to the INPUT_DIR setting in " + "app/converter/config.py (./input); relative paths resolve " + "against the project root.") ) parser.add_argument( "--output", type=Path, metavar="DIR", default=None, - help="Directory to write finished audiobooks to. Defaults to ./output." + help=("Directory to write finished audiobooks to. Defaults to the " + "OUTPUT_DIR setting in app/converter/config.py (./output); " + "relative paths resolve against the project root.") ) parser.add_argument( "--single-file", action="store_true", @@ -271,7 +283,8 @@ Examples: "--debug", action="store_true", help=("Troubleshooting mode: dump each chunk's raw audio and the exact text " "sent for it under the debug/ folder (organized per book and chapter), " - "and log every TTS request and response to the console and log file.") + "and log every TTS request and response to the console and log file. " + "Forces the DEBUG setting in app/converter/config.py on for this run.") ) parser.add_argument( "--model", type=str, default=None, metavar="ID", @@ -314,8 +327,15 @@ Examples: args = parser.parse_args() - if args.speed <= 0: - parser.error(f"--speed must be a positive number (got {args.speed:g})") + # An explicit --speed overrides the config SPEED setting; either way + # the value must be a positive number. + speed = args.speed if args.speed is not None else config.SPEED + try: + bad_speed = not isinstance(speed, (int, float)) or speed <= 0 + except TypeError: + bad_speed = True + if bad_speed: + parser.error(f"--speed must be a positive number (got {speed!r})") if args.input is not None and not args.input.is_dir(): parser.error(f"--input: no such directory: {args.input}") @@ -405,7 +425,7 @@ Examples: backend=args.backend, voice=args.voice, clone=args.clone, transcription=args.transcription, no_transcription=args.no_transcription, language=args.language, speed=args.speed, single_file=args.single_file, - output_format=args.format, debug=args.debug, + output_format=args.format, debug=args.debug or None, model_id=args.model, instructions=args.instructions, request_options=request_options, input_dir=args.input, output_dir=args.output, |
