Enable libopenjpeg in FFmpeg for JPEG 2000

This guide explains how to enable and use the libopenjpeg library within FFmpeg to achieve high-quality JPEG 2000 (JP2) encoding and decoding. While FFmpeg includes a native JPEG 2000 encoder, compiling FFmpeg with OpenJPEG support provides a much more robust, standard-compliant, and higher-quality compression workflow suitable for digital cinema (DCI) and professional archiving.

Step 1: Install the OpenJPEG Development Libraries

Before compiling FFmpeg, you must install the OpenJPEG development headers on your system.

For Ubuntu/Debian systems:

sudo apt update
sudo apt install libopenjp2-7-dev

For macOS (using Homebrew):

brew install openjpeg

For CentOS/RHEL/Fedora:

sudo dnf install openjpeg2-devel

Step 2: Configure and Compile FFmpeg

To enable the encoder, you must compile FFmpeg from source and explicitly include the --enable-libopenjpeg flag during the configuration step.

  1. Clone the FFmpeg git repository:

    git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
    cd ffmpeg
  2. Run the configure script with the required flags. Ensure you also include --enable-gpl and any other libraries you require:

    ./configure --enable-gpl --enable-libopenjpeg
  3. Compile and install:

    make -j$(nproc)
    sudo make install

Step 3: Verify the Installation

To confirm that FFmpeg was built successfully with OpenJPEG support, query the available encoders:

ffmpeg -encoders | grep openjpeg

If successful, the output will display the libopenjpeg encoder: V..... libopenjpeg OpenJPEG JPEG 2000 (codec jpeg2000)

Step 4: Encode Video with libopenjpeg

Once enabled, you can use the library to encode videos or image sequences.

Basic Encoding Command

To convert a video file to JPEG 2000 using libopenjpeg:

ffmpeg -i input.mp4 -c:v libopenjpeg -b:v 50M output.mkv

High-Quality and Lossless Encoding

To achieve lossless JPEG 2000 compression, set the compression level to lossless using the -cinema_mode or -lossless private options:

ffmpeg -i input.mp4 -c:v libopenjpeg -lossless 1 output.mkv

For mathematically lossy but high-quality encoding, you can specify the target rate or image quality using the -q:v option:

ffmpeg -i input.mp4 -c:v libopenjpeg -q:v 30 output.mkv