Skip to content

Software

ffmpeg_encoder_decoder

By ros-misc-utilities, from github.com/ros-misc-utilities/ffmpeg_encoder_decoder (opens in a new tab)

  • Perception
  • Library
  • Apache-2.0

A ROS 2 package that encodes and decodes ROS images with FFmpeg (libav), including hardware-accelerated codecs such as H.264 and HEVC, meant to be used by image transport plugins.

Facts

GitHub stars
12read on 28 Nov 2026
Last update
18 Nov 2026Newest commit on master
Maintenance
Activea commit in the last 90 days
Difficulty
Advanced

Checked on GitHub on 28 Nov 2026.

Licenses

Code
Apache-2.0 (opens in a new tab)

Read by Robotheca from LICENSE (opens in a new tab) (Apache-2.0) at commit 303ace0 (opens in a new tab), on 21 Nov 2026.

Commercial use
Yes

At a glance

What it is
  • Encodes ROS images to codecs like H.264 and H.265/HEVC using FFmpeg, using hardware acceleration when available.
  • Decodes the encoded packets back into ROS images, reproducing the original encoding where possible.
  • Provides Encoder and Decoder classes with configurable pixel formats, encoder choice and AV options.
  • Includes a hack for single-channel images such as mono8 and Bayer, adding and removing an empty colour channel.
  • Is intended to back the ffmpeg image transport and foxglove compressed video transport packages.
  • Can be built against a custom FFmpeg install, and documents enabling NVMPI-based encoding on NVIDIA Jetson.

Robotheca's summary of the project's documents, not the creator's words.

Robots on Robotheca
No robot on Robotheca uses it yet.
Licence
Apache-2.0. Commercial use: Yes. Where it was read
Install
In the creator's words: The project's README (opens in a new tab)
Maintenance
Active, last commit on 18 Nov 2026.

Use it

How to install it, in the creator's words: The project's README (opens in a new tab)

Robotheca doesn't review code. Read it before running it.

Files and versions

Facts read at commit 303ace0

Robotheca read the repository at two versions: the summary at da09f5d and the facts and the README at 303ace0 (read on 28 Nov 2026).

Robotheca never hosts the code: each link goes to the creator's own page.

The README

The creator's own README, copied as written: README.md by ros-misc-utilities, at commit 303ace0, under Apache-2.0 (opens in a new tab). The original on GitHub (opens in a new tab).

Read the README here

ROS2 FFMPeg encoder/decoder

This ROS2 package supports encoding/decoding with the FFMpeg library, for example encoding h264 and h265 or HEVC, using Nvidia or other hardware acceleration when available. This package is meant to be used by image transport plugins like the ffmpeg image transport (opens in a new tab) and the foxglove compressed video transport (opens in a new tab).

Supported systems

Continuous integration is tested under Ubuntu with the following ROS2 distros:

Image: Build Status (opens in a new tab) (opens in a new tab) Image: Build Status (opens in a new tab) (opens in a new tab) Image: Build Status (opens in a new tab) (opens in a new tab) Image: Build Status (opens in a new tab) (opens in a new tab) Image: Build Status (opens in a new tab) (opens in a new tab)

Installation
From packages
sudo apt-get install ros-${ROS_DISTRO}-ffmpeg-encoder-decoder
From source

Set the following shell variables:

repo=ffmpeg_encoder_decoder
url=https://github.com/ros-misc-utilities/${repo}.git

and follow the instructions here (opens in a new tab)

Make sure to source your workspace's install/setup.bash afterwards.

API overview
Preliminaries

When using libav it is important to understand the difference between the encoder and the codec. The codec is the standardized format in which images are encoded, for instance h264 or hevc. The encoder is a libav software module that can encode images for a given codec. For instance libx264, libx264rgb, `h264_nvenc, and h264_vaapi`` are all encoders that encode for the codec h264. Some of the encoders are hardware accelerated, some can handle more image formats than others, but in the end they all encode video for a specific codec.

For the many AV options available for various libav encoders, and for qmax, bit_rate and similar settings please refer to the ffmpeg documentation.

The inner workings

Image: overview_diagram (opens in a new tab)

The diagram above shows the stations that a ROS Image message passes as it traverses the encoder and decoder.

  1. The ROS image (with ROS sensor_msgs/Image encoding (opens in a new tab)) is first converted with the ROS cv_bridge (opens in a new tab) into the cv_bridge_target_format. This conversion is necessary because some ROS encodings (like bayer images) are not supported by libswscale. The cv_bridge_target_format can be set via setCVBridgeTargetFormat(const std::string & fmt). If it is not set, the encoder uses the encoding of the ROS image when libswscale can read it directly (e.g. rgb8, bgr8, yuv422_yuy2), so that no cv_bridge conversion takes place, and bgr8 otherwise. If this format is not set explicitly the image will be converted to the default format of bgr8. This may not be what you want for e.g. mono8 (gray) or Bayer images. Ideally thecv_bridge_target_format can be directly used by the libav decoder so the next step becomes a no-op. But at the very least cv_bridge_target_format must be an acceptable libswscale input format (with the exception of special hacks for encoding single-channel images, see below).

  2. The image is then converted to av_source_pixel_format using libswscale. The av_source_pixel_format can be set with setAVSourcePixelFormat(), defaulting to something that is acceptable to the libav encoder. You can use ffmpeg (ffmpeg -h encoder=libx264 | grep formats) to list all formats that an encoder supports. Note that the ffmpeg/libav format string notation is different from the ROS encoding strings, and the av_source_pixel_format is specified using the libav convention, whereas the cv_bridge_target_format uses ROS convention! (If you choose to bypass the cv_bridge conversion from step 1 by feeding the images to the encoder directly via the encodeImage(const cv::Mat & img ...) method, you must still set the cv_bridge_target_format such that the encoder knows what format the img argument has.) When aiming for lossless compression, beware of any av_source_pixel_format that reduces the color resolution, such as yuv420p, nv12 etc. For Bayer images, use the special hack for single-channel images.

  3. The libav encoder encodes the packet with its supported codec, e.g. the libx264 will produce h264 packets. The encoding field of the FFMPEGPacket message will document all image format conversions and the codec, in reverse order, separated by semicolon. This way the decoder can attempt to reproduce the original ros_encoding.

  4. The libav decoder decodes the packet into the original av_source_pixel_format.

  5. Finally the image is converted to output_message_format using libswscale. This format can be set (in ROS encoding syntax!) with setOutputMessageEncoding(). The format must be supported by both ROS and libswscale (except when using the special hack for single-channel images).

Note that only very few combinations of libav encoders, cv_bridge_target_format and av_source_pixel_format have been tested. Please provide feedback if you observe crashes or find obvious bugs. PRs are always appreciated!

The special single-channel hack

Many libav encoders do not support single-channel formats (like mono8 or bayer). For this reason a special hack is implemented in the encoder that adds an empty (zero-value) color channel to the single-channel image. Later, the decoder removes it again. To utilitze this hack, specify a cv_bridge_target_format of e.g. bayer_rggb8. Without the special hack, this would trigger an error because Bayer formats are not acceptable to libswscale. Instead, the image is converted to yuv420p or nv12 by adding an empty color channel. These formats are acceptable to most encoders. The decoder in turn recognizes that the cv_bridge_target_format is a single-channel format, but yuv420p/nv12 are not, and therefore drops the color channel. This hack greatly improves the efficiency for lossless encoding of Bayer images because it avoids conversion to full RGB and back.

API usage
Encoder

Using the encoder involves the following steps:

  • instantiating the Encoder object.
  • setting properties like the libav encoder to use, the encoding formats, and AV options.
  • initializing the encoder object. This requires knowledge of the image size and therefore can only be done when the first image is available. Note that many properties (encoding formats etc) must have been set before initializing the encoder.
  • feeding images to the encoder (and handling the callbacks when encoded packets become available)
  • flushing the encoder (may result in additional callbacks with encoded packets)
  • destroying the Encoder object.

The Encoder class API description has a short example code snippet.

Decoder

Using the decoder involves the following steps:

  • instantiating the Decoder object.
  • if so desired, setting the ROS output (image encoding) format.
  • initializing the decoder object. For this you need to know the encoding (codec, e.g. "h264"), and you have to specify the libav decoder name (e.g. "h264_cuvid").
  • feeding encoded packets to the decoder (and handling the callbacks when decoded images become available)
  • flushing the decoder (may result in additional callbacks with decoded images)
  • destroying the Decoder object. The Decoder class description has a short example code snippet.
How to use a custom version of libav (aka ffmpeg)

Compile and install ffmpeg. Let's say the install directory is /home/foo/ffmpeg/build, then for it to be found while building, run colcon like this:

colcon build --symlink-install --cmake-args --no-warn-unused-cli -DFFMPEG_PKGCONFIG=/home/foo/ffmpeg/build/lib/pkgconfig -DCMAKE_BUILD_TYPE=RelWithDebInfo 

This will compile against the right headers, but at runtime it may still load the system ffmpeg libraries. To avoid that, set LD_LIBRARY_PATH at runtime:

export LD_LIBRARY_PATH=/home/foo/ffmpeg/build/lib:${LD_LIBRARY_PATH}
How to use ffmpeg hardware accelerated encoding on the NVidia Jetson

Follow the instructions here (opens in a new tab) to build a version of ffmpeg that supports NVMPI. This long magic line should build a nvmpi enabled version of ffmpeg and install it under /usr/local/:

git clone https://github.com/berndpfrommer/jetson-ffmpeg.git && cd jetson-ffmpeg && mkdir build && cd build && cmake -DCMAKE_INSTALL
_PREFIX:PATH=/usr/local .. && make install && ldconfig && cd ../.. && git clone git://source.ffmpeg.org/ffmpeg.git -b release/7.1 --
depth=1 &&cd jetson-ffmpeg && ./ffpatch.sh ../ffmpeg && cd ../ffmpeg && ./configure --enable-nvmpi --enable-shared --disable-static 
--prefix=/usr/local && make install

Then follow the section above on how to actually use that custom ffmpeg library. As always first test on the CLI that the newly compiled ffmpeg command now supports h264_nvmpi.

License

This software is issued under the Apache License Version 2.0.

Robotheca lists software as a reference: nothing is hosted or run here, and every fact links to where it was read.