Skip to content

Error Handling

FFmpegError

All runtime failures produce an FFmpegError. It always includes:

typescript
class FFmpegError extends Error {
  code: FFmpegErrorCode;    // classified error type
  message: string;          // human-readable description
  command: string[];        // the FFmpeg args that were passed
  stderr: string;           // full FFmpeg stderr output
  exitCode: number | null;  // process exit code (null if killed/timed out)
}

FFmpegErrorCode enum

CodeWhen it occurs
INPUT_NOT_FOUNDInput file does not exist or is not readable
OUTPUT_EXISTSOutput file already exists and overwrite is false
CODEC_NOT_SUPPORTEDRequested codec is not available in this FFmpeg build
INVALID_ARGUMENTMalformed FFmpeg argument rejected at startup
ENCODING_FAILEDEncoding process failed mid-stream
DECODING_FAILEDInput could not be decoded
HARDWARE_INIT_FAILEDGPU hardware encoder failed to initialize
TIMEOUTFFmpeg exceeded the configured timeout
CANCELLEDOperation was cancelled via AbortSignal
PERMISSION_DENIEDOutput directory not writable
DISK_FULLDisk ran out of space during encoding
UNKNOWNUnclassified failure — check stderr for details

Catching specific errors

typescript
import { ffmpeg, FFmpegError, FFmpegErrorCode } from "ffmpeg-kit";

try {
  await ffmpeg.extract()
    .input("missing.mp4")
    .timestamp(5)
    .output("frame.png")
    .execute();
} catch (e) {
  if (e instanceof FFmpegError) {
    switch (e.code) {
      case FFmpegErrorCode.INPUT_NOT_FOUND:
        console.error("Input file not found:", e.command);
        break;
      case FFmpegErrorCode.TIMEOUT:
        console.error("FFmpeg timed out after", e.exitCode, "ms");
        break;
      case FFmpegErrorCode.HARDWARE_INIT_FAILED:
        console.error("GPU encoder failed, retry with CPU");
        break;
      default:
        console.error("Unexpected error:", e.stderr);
    }
  }
}

Using Result types

Prefer .tryExecute() when error handling is part of normal control flow:

typescript
const result = await ffmpeg.extract()
  .input("video.mp4")
  .timestamp(5)
  .output("frame.png")
  .tryExecute();

if (!result.success) {
  if (result.error.code === FFmpegErrorCode.INPUT_NOT_FOUND) {
    return { error: "Video file not found" };
  }
  throw result.error; // re-throw unexpected errors
}

return result.data.outputPath;

Builder errors vs runtime errors

Builder errors (thrown synchronously, before execution):

These occur when required fields are missing — e.g., calling .execute() without setting .input() first. They throw a regular Error with a descriptive message, not an FFmpegError. Fix them at development time.

typescript
// Throws Error: "input is required" — caught in development
ffmpeg.extract().output("out.png").execute();

Runtime errors (thrown by .execute(), always FFmpegError):

These occur during FFmpeg execution — input missing on disk, codec unavailable, etc.

Accessing raw stderr

When code is UNKNOWN, inspect e.stderr directly:

typescript
} catch (e) {
  if (e instanceof FFmpegError && e.code === FFmpegErrorCode.UNKNOWN) {
    // Search stderr for clues
    if (e.stderr.includes("No space left on device")) {
      console.error("Disk full");
    }
  }
}

Released under the MIT License.