transcode.py: log ffmpeg separately
This commit is contained in:
+22
-9
@@ -18,14 +18,17 @@ from watchdog.events import FileSystemEventHandler
|
|||||||
# Supported video extensions to monitor
|
# Supported video extensions to monitor
|
||||||
VIDEO_EXTENSIONS = {'.mkv', '.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.m4v', '.ts'}
|
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():
|
def setup_logging():
|
||||||
"""
|
"""
|
||||||
Sets up logging to both console (INFO) and file (DEBUG/Verbose).
|
Sets up logging to both console (INFO) and file (DEBUG/Verbose).
|
||||||
Log file is stored in $XDG_CONFIG_HOME/transcoder/transcoder.log
|
Log file is stored in $XDG_CONFIG_HOME/transcoder/transcoder.log
|
||||||
"""
|
"""
|
||||||
# Determine config directory
|
log_dir = get_config_dir()
|
||||||
xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
|
|
||||||
log_dir = Path(xdg_config) / "transcoder"
|
|
||||||
log_dir.mkdir(parents=True, exist_ok=True)
|
log_dir.mkdir(parents=True, exist_ok=True)
|
||||||
log_file = log_dir / "transcoder.log"
|
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}")
|
logging.warning(f"File vanished during checks: {input_path.name}")
|
||||||
return
|
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"START: Transcoding {input_path.name}")
|
||||||
|
logging.info(f" FFmpeg details logging to: {ffmpeg_log_file}")
|
||||||
|
|
||||||
cmd = get_ffmpeg_command(input_path, output_path)
|
cmd = get_ffmpeg_command(input_path, output_path)
|
||||||
logging.debug(f"Executing FFmpeg command: {' '.join(cmd)}")
|
logging.debug(f"Executing FFmpeg command: {' '.join(cmd)}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Run FFmpeg
|
with open(ffmpeg_log_file, "w", encoding="utf-8", errors="replace") as f_log:
|
||||||
# We capture output to prevent FFmpeg spamming the console, but log it on error
|
# Write the command itself to the top of the log
|
||||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
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:
|
if result.returncode == 0:
|
||||||
logging.info(f"DONE: Successfully created {output_path.name}")
|
logging.info(f"DONE: Successfully created {output_path.name}")
|
||||||
logging.debug(f"FFmpeg stdout: {result.stdout}")
|
|
||||||
else:
|
else:
|
||||||
logging.error(f"FFmpeg failed for {input_path.name}")
|
logging.error(f"FFmpeg failed for {input_path.name}. See log at {ffmpeg_log_file}")
|
||||||
logging.error(f"FFmpeg stderr: {result.stderr}")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(f"Unexpected error during transcoding of {input_path.name}")
|
logging.exception(f"Unexpected error during transcoding of {input_path.name}")
|
||||||
|
|||||||
Reference in New Issue
Block a user