From 22803754afdbb7ab3e8275803b23b7868c774433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 4 Dec 2025 16:07:38 +0100 Subject: [PATCH] transcode.py: add config.json alternative to CLI args --- transcode.py | 64 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/transcode.py b/transcode.py index 5b455a5..52a9830 100644 --- a/transcode.py +++ b/transcode.py @@ -11,6 +11,7 @@ import sys import subprocess import os import logging +import json from pathlib import Path from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler @@ -53,6 +54,24 @@ def setup_logging(): logging.info(f"Logging started. Verbose logs at: {log_file}") +def load_config(): + """ + Loads configuration from config.json in the config directory. + Returns a dictionary of config values. + """ + config_path = get_config_dir() / "config.json" + if not config_path.exists(): + return {} + + try: + with open(config_path, 'r') as f: + config = json.load(f) + logging.debug(f"Loaded config from {config_path}: {config}") + return config + except Exception as e: + logging.error(f"Failed to load config file: {e}") + return {} + def get_video_codec(input_path): """ Uses ffprobe to determine the codec of the first video stream. @@ -205,20 +224,28 @@ class NewFileHandler(FileSystemEventHandler): def main(): setup_logging() + config = load_config() parser = argparse.ArgumentParser(description="Nvidia AV1 Transcoder & Watcher") - group = parser.add_mutually_exclusive_group(required=True) - group.add_argument("--input", type=str, help="Single file to transcode") - group.add_argument("--watch-dir", type=str, help="Directory to monitor for new files") - - parser.add_argument("--output-dir", type=str, help="Output directory (Required for --watch-dir)") + parser.add_argument("--input", type=str, help="Single file to transcode (Overrides watch mode)") + parser.add_argument("--watch-dir", type=str, help="Directory to monitor for new files") + parser.add_argument("--output-dir", type=str, help="Output directory") parser.add_argument("--no-skip-av1", action="store_true", help="Force transcoding even if input is already AV1") - args = parser.parse_args() + # Set defaults from config + # We accept both hyphenated and underscore keys from JSON for user convenience + default_watch = config.get("watch-dir") or config.get("watch_dir") + default_output = config.get("output-dir") or config.get("output_dir") + default_no_skip = config.get("no-skip-av1") or config.get("no_skip_av1") or False - # Determine skip policy - skip_av1 = not args.no_skip_av1 + parser.set_defaults( + watch_dir=default_watch, + output_dir=default_output, + no_skip_av1=default_no_skip + ) + + args = parser.parse_args() # --- Mode 1: Single File --- if args.input: @@ -234,12 +261,13 @@ def main(): else: output_path = input_path.parent / (input_path.stem + "_av1.mp4") - transcode_file(input_path, output_path, skip_av1=skip_av1) + transcode_file(input_path, output_path, skip_av1=not args.no_skip_av1) + return # --- Mode 2: Watch Directory --- - elif args.watch_dir: + if args.watch_dir: if not args.output_dir: - logging.critical("--output-dir is required when using --watch-dir") + logging.critical("Output directory is not specified in CLI (--output-dir) or Config.") sys.exit(1) watch_dir = Path(args.watch_dir) @@ -253,13 +281,15 @@ def main(): logging.info(f"Monitoring {watch_dir}...") logging.info(f"Outputting to {output_dir}") - if skip_av1: - logging.info("Policy: Skipping files that are already AV1.") - else: + + if args.no_skip_av1: logging.info("Policy: Force transcoding all files (including AV1).") + else: + logging.info("Policy: Skipping files that are already AV1.") + logging.info("Press Ctrl+C to stop.") - event_handler = NewFileHandler(output_dir, skip_av1=skip_av1) + event_handler = NewFileHandler(output_dir, skip_av1=not args.no_skip_av1) observer = Observer() observer.schedule(event_handler, str(watch_dir), recursive=False) observer.start() @@ -271,6 +301,10 @@ def main(): logging.info("Stopping observer...") observer.stop() observer.join() + else: + logging.critical("No operation mode selected.") + logging.critical("Please provide --input for single file OR configure watch-dir via CLI or Config.") + sys.exit(1) if __name__ == "__main__": main() \ No newline at end of file