transcode.py: add config.json alternative to CLI args
This commit is contained in:
+49
-15
@@ -11,6 +11,7 @@ import sys
|
|||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
@@ -53,6 +54,24 @@ def setup_logging():
|
|||||||
|
|
||||||
logging.info(f"Logging started. Verbose logs at: {log_file}")
|
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):
|
def get_video_codec(input_path):
|
||||||
"""
|
"""
|
||||||
Uses ffprobe to determine the codec of the first video stream.
|
Uses ffprobe to determine the codec of the first video stream.
|
||||||
@@ -205,20 +224,28 @@ class NewFileHandler(FileSystemEventHandler):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
config = load_config()
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="Nvidia AV1 Transcoder & Watcher")
|
parser = argparse.ArgumentParser(description="Nvidia AV1 Transcoder & Watcher")
|
||||||
|
|
||||||
group = parser.add_mutually_exclusive_group(required=True)
|
parser.add_argument("--input", type=str, help="Single file to transcode (Overrides watch mode)")
|
||||||
group.add_argument("--input", type=str, help="Single file to transcode")
|
parser.add_argument("--watch-dir", type=str, help="Directory to monitor for new files")
|
||||||
group.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("--output-dir", type=str, help="Output directory (Required for --watch-dir)")
|
|
||||||
parser.add_argument("--no-skip-av1", action="store_true", help="Force transcoding even if input is already AV1")
|
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
|
parser.set_defaults(
|
||||||
skip_av1 = not args.no_skip_av1
|
watch_dir=default_watch,
|
||||||
|
output_dir=default_output,
|
||||||
|
no_skip_av1=default_no_skip
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
# --- Mode 1: Single File ---
|
# --- Mode 1: Single File ---
|
||||||
if args.input:
|
if args.input:
|
||||||
@@ -234,12 +261,13 @@ def main():
|
|||||||
else:
|
else:
|
||||||
output_path = input_path.parent / (input_path.stem + "_av1.mp4")
|
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 ---
|
# --- Mode 2: Watch Directory ---
|
||||||
elif args.watch_dir:
|
if args.watch_dir:
|
||||||
if not args.output_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)
|
sys.exit(1)
|
||||||
|
|
||||||
watch_dir = Path(args.watch_dir)
|
watch_dir = Path(args.watch_dir)
|
||||||
@@ -253,13 +281,15 @@ def main():
|
|||||||
|
|
||||||
logging.info(f"Monitoring {watch_dir}...")
|
logging.info(f"Monitoring {watch_dir}...")
|
||||||
logging.info(f"Outputting to {output_dir}")
|
logging.info(f"Outputting to {output_dir}")
|
||||||
if skip_av1:
|
|
||||||
logging.info("Policy: Skipping files that are already AV1.")
|
if args.no_skip_av1:
|
||||||
else:
|
|
||||||
logging.info("Policy: Force transcoding all files (including 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.")
|
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 = Observer()
|
||||||
observer.schedule(event_handler, str(watch_dir), recursive=False)
|
observer.schedule(event_handler, str(watch_dir), recursive=False)
|
||||||
observer.start()
|
observer.start()
|
||||||
@@ -271,6 +301,10 @@ def main():
|
|||||||
logging.info("Stopping observer...")
|
logging.info("Stopping observer...")
|
||||||
observer.stop()
|
observer.stop()
|
||||||
observer.join()
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user