diff --git a/transcode.py b/transcode.py index 4e00e8b..da33dc3 100644 --- a/transcode.py +++ b/transcode.py @@ -18,14 +18,17 @@ from watchdog.events import FileSystemEventHandler # Supported video extensions to monitor VIDEO_EXTENSIONS = {'.mkv', '.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.m4v', '.ts'} +def get_config_dir(): + """Returns the main configuration directory path.""" + xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) + return Path(xdg_config) / "transcoder" + def setup_logging(): """ Sets up logging to both console (INFO) and file (DEBUG/Verbose). Log file is stored in $XDG_CONFIG_HOME/transcoder/transcoder.log """ - # Determine config directory - xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) - log_dir = Path(xdg_config) / "transcoder" + log_dir = get_config_dir() log_dir.mkdir(parents=True, exist_ok=True) log_file = log_dir / "transcoder.log" @@ -103,22 +106,32 @@ def transcode_file(input_file, output_file): logging.warning(f"File vanished during checks: {input_path.name}") return + # Prepare individual FFmpeg log file + ffmpeg_logs_dir = get_config_dir() / "ffmpeg_logs" + ffmpeg_logs_dir.mkdir(parents=True, exist_ok=True) + + # We append .log to the full filename to avoid collisions (e.g. video.mp4.log) + ffmpeg_log_file = ffmpeg_logs_dir / f"{input_path.name}.log" + logging.info(f"START: Transcoding {input_path.name}") + logging.info(f" FFmpeg details logging to: {ffmpeg_log_file}") cmd = get_ffmpeg_command(input_path, output_path) logging.debug(f"Executing FFmpeg command: {' '.join(cmd)}") try: - # Run FFmpeg - # We capture output to prevent FFmpeg spamming the console, but log it on error - result = subprocess.run(cmd, capture_output=True, text=True) + with open(ffmpeg_log_file, "w", encoding="utf-8", errors="replace") as f_log: + # Write the command itself to the top of the log + f_log.write(f"COMMAND: {' '.join(cmd)}\n\n") + f_log.flush() + + # Run FFmpeg, redirecting both stdout and stderr to the individual log file + result = subprocess.run(cmd, stdout=f_log, stderr=subprocess.STDOUT, text=True) if result.returncode == 0: logging.info(f"DONE: Successfully created {output_path.name}") - logging.debug(f"FFmpeg stdout: {result.stdout}") else: - logging.error(f"FFmpeg failed for {input_path.name}") - logging.error(f"FFmpeg stderr: {result.stderr}") + logging.error(f"FFmpeg failed for {input_path.name}. See log at {ffmpeg_log_file}") except Exception as e: logging.exception(f"Unexpected error during transcoding of {input_path.name}")