{"record":{"id":"341603d6d1cbd3e7","repo":"ATH-MaaS/Pixelle-Video","slug":"failed-to-trim-video-error-msg","errorCode":null,"errorMessage":"Failed to trim video: {error_msg}","messagePattern":"Failed to trim video: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"pixelle_video/services/video.py","lineNumber":918,"sourceCode":"        output = self._get_unique_temp_path(\"trimmed\", os.path.basename(video))\n        \n        try:\n            # Use stream copy when possible for fast trimming\n            input_stream = ffmpeg.input(video, t=target_duration)\n            output_kwargs = {\"vcodec\": \"copy\"}\n            if self.has_audio_stream(video):\n                output_kwargs[\"acodec\"] = \"copy\"\n            (\n                input_stream\n                .output(output, **output_kwargs)\n                .overwrite_output()\n                .run(capture_stdout=True, capture_stderr=True, quiet=True)\n            )\n            return output\n        except ffmpeg.Error as e:\n            error_msg = e.stderr.decode() if e.stderr else str(e)\n            logger.error(f\"FFmpeg error trimming video: {error_msg}\")\n            raise RuntimeError(f\"Failed to trim video: {error_msg}\")\n    \n    def _pad_video_to_duration(self, video: str, target_duration: float, pad_strategy: str = \"freeze\") -> str:\n        \"\"\"\n        Pad video to specified duration by extending the last frame or adding black frames\n        \n        Args:\n            video: Input video file path\n            target_duration: Target duration in seconds\n            pad_strategy: Padding strategy - \"freeze\" (freeze last frame) or \"black\" (black screen)\n        \n        Returns:\n            Path to padded video (temp file)\n        \n        Raises:\n            RuntimeError: If FFmpeg execution fails\n        \"\"\"\n        output = self._get_unique_temp_path(\"padded\", os.path.basename(video))\n        ","sourceCodeStart":900,"sourceCodeEnd":936,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/services/video.py#L900-L936","documentation":"_trim_video_to_duration wraps any ffmpeg-python error raised while trimming the input video to the target duration and re-raises it as RuntimeError with ffmpeg's stderr appended. This means the ffmpeg trim command failed (bad input, unsupported codec, invalid seek times), not a Python logic error.","triggerScenarios":"merge_audio_video calls _trim_video_to_duration and ffmpeg exits non-zero: corrupt/unreadable input video, an output path that is not writable, '-ss'/'-t' values that are invalid (negative, or beyond stream bounds in some formats), or a missing system ffmpeg binary.","commonSituations":"ffmpeg not installed or not on PATH; input file truncated or in a container ffmpeg cannot demux; disk full so the output can't be written; passing a duration of 0 or negative.","solutions":["Read the stderr text embedded in the RuntimeError message — it names the exact ffmpeg failure","Confirm ffmpeg is installed and on PATH (`ffmpeg -version`)","Verify the input video plays and the trim start/duration values are positive and within the video length","Check output directory permissions and free disk space"],"exampleFix":"// before\noutput = service._trim_video_to_duration('clip.mp4', 0)\n// after\nduration = max(0.1, min(requested, probe_duration('clip.mp4')))\noutput = service._trim_video_to_duration('clip.mp4', duration)","handlingStrategy":"try-catch","validationCode":"import shutil\nfrom pathlib import Path\n\ndef validate_trim_inputs(video: str, duration: float) -> None:\n    if not Path(video).exists():\n        raise FileNotFoundError(video)\n    if duration <= 0:\n        raise ValueError(f'duration must be > 0, got {duration}')\n    if shutil.which('ffmpeg') is None:\n        raise RuntimeError('ffmpeg not on PATH')","typeGuard":null,"tryCatchPattern":"try:\n    trimmed = service._trim_video_to_duration(video, target)\nexcept RuntimeError as e:\n    logger.error(f'trim ffmpeg stderr: {e}')\n    raise VideoProcessingError(video, target) from e","preventionTips":["Check `shutil.which('ffmpeg')` at startup and fail fast with a clear message","Sanitize/validate durations (positive, finite, within probed video length)","Keep enough free disk space and writable temp dirs for ffmpeg outputs"],"tags":["ffmpeg","runtime-error","video-trim","subprocess"],"backgroundTag":"ffmpeg-command-failed","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}