JavaCPP bindings for FFmpeg multimedia framework providing comprehensive Java access to audio/video encoding, decoding, filtering, and format conversion
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
JavaCPP FFmpeg bindings provide comprehensive Java access to FFmpeg 7.1.1, the complete multimedia framework for audio/video encoding, decoding, transcoding, filtering, and streaming. This library enables Java developers to harness FFmpeg's powerful C/C++ libraries through a type-safe Java API.
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>7.1.1-1.5.12</version>
</dependency>Core FFmpeg functionality access:
import org.bytedeco.ffmpeg.avcodec.*;
import org.bytedeco.ffmpeg.avformat.*;
import org.bytedeco.ffmpeg.avutil.*;
import org.bytedeco.ffmpeg.avfilter.*;
import org.bytedeco.ffmpeg.swscale.*;
import org.bytedeco.ffmpeg.swresample.*;
import static org.bytedeco.ffmpeg.global.avcodec.*;
import static org.bytedeco.ffmpeg.global.avformat.*;
import static org.bytedeco.ffmpeg.global.avutil.*;
import static org.bytedeco.ffmpeg.global.swscale.*;Command-line tool access:
import org.bytedeco.ffmpeg.ffmpeg;
import org.bytedeco.ffmpeg.ffprobe;
import org.bytedeco.javacpp.Loader;import org.bytedeco.ffmpeg.avformat.*;
import org.bytedeco.ffmpeg.avcodec.*;
import org.bytedeco.ffmpeg.avutil.*;
import static org.bytedeco.ffmpeg.global.avformat.*;
import static org.bytedeco.ffmpeg.global.avcodec.*;
import static org.bytedeco.ffmpeg.global.avutil.*;
// Open video file and read frames
AVFormatContext formatContext = new AVFormatContext(null);
AVPacket packet = new AVPacket();
// Open input file
int result = avformat_open_input(formatContext, "input.mp4", null, null);
if (result < 0) {
throw new RuntimeException("Cannot open file");
}
// Find stream information
avformat_find_stream_info(formatContext, (PointerPointer)null);
// Find video stream
int videoStreamIndex = -1;
for (int i = 0; i < formatContext.nb_streams(); i++) {
if (formatContext.streams(i).codecpar().codec_type() == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
// Set up decoder
AVCodecContext codecContext = avcodec_alloc_context3(null);
avcodec_parameters_to_context(codecContext, formatContext.streams(videoStreamIndex).codecpar());
AVCodec codec = avcodec_find_decoder(codecContext.codec_id());
avcodec_open2(codecContext, codec, (PointerPointer)null);
// Read frames
AVFrame frame = av_frame_alloc();
while (av_read_frame(formatContext, packet) >= 0) {
if (packet.stream_index() == videoStreamIndex) {
avcodec_send_packet(codecContext, packet);
while (avcodec_receive_frame(codecContext, frame) >= 0) {
// Process frame data
System.out.println("Frame: " + frame.width() + "x" + frame.height());
}
}
av_packet_unref(packet);
}
// Cleanup
av_frame_free(frame);
avcodec_free_context(codecContext);
avformat_close_input(formatContext);JavaCPP FFmpeg bindings are organized into several key modules:
org.bytedeco.ffmpeg.global.*) providing direct access to FFmpeg C functionsorg.bytedeco.ffmpeg.avcodec.*, etc.) with memory managementorg.bytedeco.ffmpeg.presets.*)ffmpeg, ffprobe) for external process executionContainer format operations for reading and writing media files. Supports all major formats including MP4, AVI, MKV, MOV, WebM, and streaming protocols.
int avformat_open_input(AVFormatContext ctx, String filename, AVInputFormat fmt, PointerPointer options);
int avformat_find_stream_info(AVFormatContext ctx, PointerPointer options);
int av_read_frame(AVFormatContext ctx, AVPacket pkt);
int av_write_frame(AVFormatContext ctx, AVPacket pkt);Audio and video encoding/decoding with support for all major codecs including H.264, H.265, VP9, AV1, AAC, MP3, and many others.
AVCodec avcodec_find_encoder(int id);
AVCodec avcodec_find_decoder(int id);
AVCodecContext avcodec_alloc_context3(AVCodec codec);
int avcodec_open2(AVCodecContext avctx, AVCodec codec, PointerPointer options);
int avcodec_send_packet(AVCodecContext avctx, AVPacket avpkt);
int avcodec_receive_frame(AVCodecContext avctx, AVFrame frame);Comprehensive audio and video filtering pipeline for effects, transformations, format conversions, and complex processing graphs.
AVFilterGraph avfilter_graph_alloc();
int avfilter_graph_create_filter(AVFilterContext filt_ctx, AVFilter filt,
String name, String args, Pointer opaque, AVFilterGraph graph_ctx);
int avfilter_graph_config(AVFilterGraph graphctx, Pointer log_ctx);Video scaling, pixel format conversion, and color space transformations with optimized implementations.
SwsContext sws_getContext(int srcW, int srcH, int srcFormat,
int dstW, int dstH, int dstFormat, int flags,
SwsFilter srcFilter, SwsFilter dstFilter, DoublePointer param);
int sws_scale(SwsContext c, PointerPointer srcSlice, IntPointer srcStride,
int srcSliceY, int srcSliceH, PointerPointer dst, IntPointer dstStride);Audio resampling, channel layout conversion, and sample format transformations for comprehensive audio processing.
SwrContext swr_alloc();
int swr_init(SwrContext s);
int swr_convert(SwrContext s, PointerPointer out, int out_count,
PointerPointer in, int in_count);Device enumeration and access for cameras, microphones, and other multimedia input/output devices across platforms.
int avdevice_list_devices(AVFormatContext s, AVDeviceInfoList device_list);
int avdevice_list_input_sources(AVInputFormat device, String device_name,
AVDictionary device_options, AVDeviceInfoList device_list);Direct access to ffmpeg and ffprobe executables for complex operations and external processing workflows.
// Access via Loader.load() for process execution
String ffmpegPath = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
String ffprobePath = Loader.load(org.bytedeco.ffmpeg.ffprobe.class);Video enhancement and quality improvement through deblocking, deringing, and color correction filters for decoded video content.
pp_context pp_get_context(int width, int height, int flags);
pp_mode pp_get_mode_by_name_and_quality(String name, int quality);
void pp_postprocess(PointerPointer src, IntPointer srcStride,
PointerPointer dst, IntPointer dstStride,
int horizontalSize, int verticalSize,
BytePointer QP_store, int QP_stride,
pp_mode mode, pp_context ppContext, int pict_type);GPU and specialized hardware acceleration support for high-performance encoding, decoding, and processing with CUDA, VAAPI, DXVA2, VideoToolbox, and other acceleration APIs.
int av_hwdevice_find_type_by_name(String name);
AVBufferRef av_hwdevice_ctx_alloc(int type);
int av_hwdevice_ctx_create(@ByPtrPtr AVBufferRef device_ctx, int type,
String device, AVDictionary opts, int flags);
int av_hwframe_transfer_data(AVFrame dst, AVFrame src, int flags);
AVCodecHWConfig avcodec_get_hw_config(AVCodec codec, int index);Cryptographic functions and security features for multimedia data protection, including AES encryption, hash functions, and media content encryption support.
AVAES av_aes_alloc();
int av_aes_init(AVAES a, byte[] key, int key_bits, int decrypt);
void av_aes_crypt(AVAES a, byte[] dst, byte[] src, int count, byte[] iv, int decrypt);
int av_hash_alloc(@ByPtrPtr AVHashContext ctx, String name);
void av_hash_update(AVHashContext ctx, BytePointer src, int len);
void av_hash_final(AVHashContext ctx, BytePointer dst);Cryptographic and Security Features
Comprehensive reference of constants, enumerations, and compile-time values for configuration, error handling, and API parameters.
// Media types, pixel formats, sample formats
int AVMEDIA_TYPE_VIDEO = 0;
int AV_PIX_FMT_YUV420P = 0;
int AV_SAMPLE_FMT_FLTP = 8;
// Codec IDs
int AV_CODEC_ID_H264 = 27;
int AV_CODEC_ID_AAC = 86018;
// Error codes
int AVERROR_EOF();
int AVERROR_EAGAIN();The fundamental structure for raw audio/video data.
class AVFrame extends Pointer {
AVFrame av_frame_alloc();
void av_frame_free(AVFrame frame);
int av_frame_ref(AVFrame dst, AVFrame src);
void av_frame_unref(AVFrame frame);
// Data access
PointerPointer data();
IntPointer linesize();
int width();
int height();
int format();
long pts();
}Container for compressed/encoded media data.
class AVPacket extends Pointer {
AVPacket av_packet_alloc();
void av_packet_free(AVPacket pkt);
int av_packet_ref(AVPacket dst, AVPacket src);
void av_packet_unref(AVPacket pkt);
// Packet properties
BytePointer data();
int size();
long pts();
long dts();
int stream_index();
int flags();
}Container format context for input/output operations.
class AVFormatContext extends Pointer {
AVFormatContext avformat_alloc_context();
void avformat_free_context(AVFormatContext s);
// Stream access
int nb_streams();
PointerPointer streams();
AVInputFormat iformat();
AVOutputFormat oformat();
// Metadata
AVDictionary metadata();
long duration();
long bit_rate();
}FFmpeg functions typically return negative values for errors. Use AVERROR macros for error checking:
int result = avformat_open_input(formatContext, filename, null, null);
if (result < 0) {
if (result == AVERROR_ENOENT()) {
throw new FileNotFoundException("File not found: " + filename);
} else {
throw new RuntimeException("Failed to open file, error code: " + result);
}
}JavaCPP handles Java-to-native memory mapping automatically, but explicit cleanup is required for native contexts:
// Always pair allocation with deallocation
AVFrame frame = av_frame_alloc();
try {
// Use frame
} finally {
av_frame_free(frame);
}
// Use reference counting for shared resources
AVPacket packet = av_packet_alloc();
AVPacket copy = av_packet_alloc();
av_packet_ref(copy, packet); // Increment reference count
av_packet_unref(copy); // Decrement reference count
av_packet_free(packet); // Final cleanup