CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-bytedeco--ffmpeg

JavaCPP bindings for FFmpeg multimedia framework providing comprehensive Java access to audio/video encoding, decoding, filtering, and format conversion

Pending
Overview
Eval results
Files

format-handling.mddocs/

Format Handling (Muxing/Demuxing)

Container format operations for reading and writing media files with support for all major formats including MP4, AVI, MKV, MOV, WebM, and streaming protocols.

Capabilities

Input Format Operations

Opening Input Files

/**
 * Open an input stream and read the header
 * @param ps Pointer to user-allocated AVFormatContext
 * @param filename Name of stream/file to open, or null for default
 * @param fmt Input format to use, or null for auto-detection
 * @param options Dictionary of options
 * @return 0 on success, negative AVERROR on failure
 */
int avformat_open_input(@ByPtrPtr AVFormatContext ps, String filename, AVInputFormat fmt, @ByPtrPtr AVDictionary options);

/**
 * Read packets of a media file to get stream information
 * @param ic Media file handle
 * @param options Dictionary of options
 * @return >= 0 if OK, AVERROR_xxx on error
 */
int avformat_find_stream_info(AVFormatContext ic, PointerPointer options);

/**
 * Close an opened input AVFormatContext
 * @param s Pointer to AVFormatContext to close
 */
void avformat_close_input(@ByPtrPtr AVFormatContext s);

Usage Example:

import org.bytedeco.ffmpeg.avformat.*;
import static org.bytedeco.ffmpeg.global.avformat.*;

AVFormatContext formatContext = new AVFormatContext(null);

// Open input file
int result = avformat_open_input(formatContext, "input.mp4", null, null);
if (result < 0) {
    throw new RuntimeException("Cannot open input file");
}

// Analyze streams
result = avformat_find_stream_info(formatContext, (PointerPointer)null);
if (result < 0) {
    throw new RuntimeException("Cannot find stream information");
}

// Print format information
av_dump_format(formatContext, 0, "input.mp4", 0);

// Cleanup
avformat_close_input(formatContext);

Output Format Operations

Creating Output Files

/**
 * Allocate an AVFormatContext for an output format
 * @param ctx Pointer to receive the allocated context
 * @param oformat Format to use for allocating the context
 * @param format_name Format name if oformat is null
 * @param filename Filename to use for context if oformat is null
 * @return >= 0 in case of success, negative AVERROR code in case of failure
 */
int avformat_alloc_output_context2(@ByPtrPtr AVFormatContext ctx, AVOutputFormat oformat, 
    String format_name, String filename);

/**
 * Add a new stream to a media file
 * @param s Media file context
 * @param c Codec context (deprecated, pass null)
 * @return New stream, or null on error
 */
AVStream avformat_new_stream(AVFormatContext s, AVCodec c);

/**
 * Allocate the stream private data and write the stream header
 * @param s Media file context
 * @param options Dictionary of options
 * @return 0 on success, negative AVERROR on failure
 */
int avformat_write_header(AVFormatContext s, PointerPointer options);

/**
 * Write the stream trailer and free the file private data
 * @param s Media file context
 * @return 0 if OK, AVERROR_xxx on error
 */
int av_write_trailer(AVFormatContext s);

Usage Example:

AVFormatContext outputContext = new AVFormatContext(null);

// Allocate output context
int result = avformat_alloc_output_context2(outputContext, null, null, "output.mp4");
if (result < 0) {
    throw new RuntimeException("Cannot allocate output context");
}

// Add video stream
AVStream videoStream = avformat_new_stream(outputContext, null);
if (videoStream == null) {
    throw new RuntimeException("Cannot create video stream");
}

// Set up codec parameters
videoStream.codecpar().codec_type(AVMEDIA_TYPE_VIDEO);
videoStream.codecpar().codec_id(AV_CODEC_ID_H264);
videoStream.codecpar().width(1920);
videoStream.codecpar().height(1080);

// Open output file
if ((outputContext.oformat().flags() & AVFMT_NOFILE) == 0) {
    AVIOContext ioContext = new AVIOContext(null);
    result = avio_open(ioContext, "output.mp4", AVIO_FLAG_WRITE);
    if (result < 0) {
        throw new RuntimeException("Cannot open output file");
    }
    outputContext.pb(ioContext);
}

// Write header
result = avformat_write_header(outputContext, (PointerPointer)null);
if (result < 0) {
    throw new RuntimeException("Cannot write header");
}

// Write frames (not shown)

// Finalize
av_write_trailer(outputContext);
avformat_free_context(outputContext);

Packet Operations

Reading and Writing Packets

/**
 * Return the next frame of a stream
 * @param s Media file context
 * @param pkt Packet to fill with data
 * @return 0 if OK, < 0 on error or end of file
 */
int av_read_frame(AVFormatContext s, AVPacket pkt);

/**
 * Write a packet to output media file
 * @param s Media file context
 * @param pkt Packet to write
 * @return < 0 on error, 0 on success
 */
int av_write_frame(AVFormatContext s, AVPacket pkt);

/**
 * Write a packet to output media file ensuring correct interleaving
 * @param s Media file context  
 * @param pkt Packet to write
 * @return < 0 on error, 0 on success
 */
int av_interleaved_write_frame(AVFormatContext s, AVPacket pkt);

/**
 * Seek to keyframe at timestamp
 * @param s Media file context
 * @param stream_index Stream index (-1 for default)
 * @param timestamp Target timestamp
 * @param flags Seek flags
 * @return >= 0 on success
 */
int av_seek_frame(AVFormatContext s, int stream_index, long timestamp, int flags);

Stream Information

Accessing Stream Data

/**
 * AVFormatContext stream access
 */
class AVFormatContext extends Pointer {
    /** Number of streams in the file */
    int nb_streams();
    
    /** Array of streams */
    PointerPointer streams();
    
    /** Input format information */
    AVInputFormat iformat();
    
    /** Output format information */  
    AVOutputFormat oformat();
    
    /** File duration in AV_TIME_BASE units */
    long duration();
    
    /** Total stream bitrate */
    long bit_rate();
    
    /** Metadata dictionary */
    AVDictionary metadata();
}

/**
 * Individual stream information
 */
class AVStream extends Pointer {
    /** Stream index in AVFormatContext.streams[] */
    int index();
    
    /** Stream ID */
    int id();
    
    /** Codec parameters */
    AVCodecParameters codecpar();
    
    /** Time base for stream timestamps */
    AVRational time_base();
    
    /** Stream duration in time_base units */
    long duration();
    
    /** Number of frames if known */
    long nb_frames();
    
    /** Stream metadata */
    AVDictionary metadata();
}

Format Detection

Input Format Discovery

/**
 * Guess file format based on filename
 * @param filename Filename to analyze
 * @param mime_type MIME type (optional)
 * @return Detected input format or null
 */
AVInputFormat av_find_input_format(String short_name);

/**
 * Probe input format from data
 * @param pd Probe data containing buffer and filename
 * @param is_opened Whether file is already opened
 * @return Probe score (higher = more likely)
 */
int av_probe_input_format(AVProbeData pd, int is_opened);

/**
 * Get list of all supported input formats
 * @return Iterator for input formats
 */
AVInputFormat av_demuxer_iterate(Pointer opaque);

/**
 * Get list of all supported output formats  
 * @return Iterator for output formats
 */
AVOutputFormat av_muxer_iterate(Pointer opaque);

I/O Context Operations

Custom I/O Handling

/**
 * Allocate and initialize an AVIOContext for buffered I/O
 * @param s Pointer to receive allocated context
 * @param url URL to open
 * @param flags Access flags (AVIO_FLAG_READ, AVIO_FLAG_WRITE)
 * @return >= 0 on success
 */
int avio_open(AVIOContext s, String url, int flags);

/**
 * Close AVIOContext and free resources
 * @param s Pointer to AVIOContext to close
 * @return 0 on success
 */
int avio_closep(AVIOContext s);

/**
 * Create custom I/O context
 * @param buffer User-allocated buffer
 * @param buffer_size Buffer size
 * @param write_flag 1 for write, 0 for read
 * @param opaque User data pointer
 * @param read_packet Custom read function
 * @param write_packet Custom write function
 * @param seek Custom seek function
 * @return Allocated AVIOContext
 */
AVIOContext avio_alloc_context(BytePointer buffer, int buffer_size, int write_flag,
    Pointer opaque, Read_packet_Pointer_BytePointer_int read_packet,
    Write_packet_Pointer_BytePointer_int write_packet, 
    Seek_Pointer_long_int seek);

Metadata Operations

Reading and Writing Metadata

/**
 * Set metadata entry in dictionary
 * @param pm Pointer to dictionary
 * @param key Metadata key
 * @param value Metadata value
 * @param flags Control flags
 * @return >= 0 on success
 */
int av_dict_set(AVDictionary pm, String key, String value, int flags);

/**
 * Get metadata entry from dictionary
 * @param m Dictionary to search
 * @param key Key to find
 * @param prev Previous entry for iteration
 * @param flags Search flags
 * @return Dictionary entry or null if not found
 */
AVDictionaryEntry av_dict_get(AVDictionary m, String key, AVDictionaryEntry prev, int flags);

Usage Example:

// Read metadata from input file
AVFormatContext formatContext = /* ... opened context ... */;
AVDictionaryEntry entry = null;

// Iterate through all metadata
while ((entry = av_dict_get(formatContext.metadata(), "", entry, AV_DICT_IGNORE_SUFFIX)) != null) {
    System.out.println(entry.key().getString() + ": " + entry.value().getString());
}

// Set metadata for output
AVDictionary metadata = new AVDictionary(null);
av_dict_set(metadata, "title", "My Video", 0);
av_dict_set(metadata, "artist", "My Name", 0);
outputContext.metadata(metadata);

Constants

Format Flags

// AVFormatContext flags
int AVFMT_NOFILE = 0x0001;      // Demuxer will use a custom I/O
int AVFMT_NEEDNUMBER = 0x0002;  // Needs number in filename
int AVFMT_SHOW_IDS = 0x0008;    // Show format stream IDs
int AVFMT_GLOBALHEADER = 0x0040; // Format wants global header
int AVFMT_NOTIMESTAMPS = 0x0080; // Format does not need timestamps
int AVFMT_VARIABLE_FPS = 0x0400; // Format allows variable fps

// I/O flags
int AVIO_FLAG_READ = 1;         // Read-only access
int AVIO_FLAG_WRITE = 2;        // Write-only access
int AVIO_FLAG_READ_WRITE = 3;   // Read-write access

// Seek flags  
int AVSEEK_FLAG_BACKWARD = 1;   // Seek backwards
int AVSEEK_FLAG_BYTE = 2;       // Seek by bytes
int AVSEEK_FLAG_ANY = 4;        // Seek to any frame
int AVSEEK_FLAG_FRAME = 8;      // Seek by frame number

Common Media Types

// Media types
int AVMEDIA_TYPE_UNKNOWN = -1;
int AVMEDIA_TYPE_VIDEO = 0;
int AVMEDIA_TYPE_AUDIO = 1; 
int AVMEDIA_TYPE_DATA = 2;
int AVMEDIA_TYPE_SUBTITLE = 3;
int AVMEDIA_TYPE_ATTACHMENT = 4;

Install with Tessl CLI

npx tessl i tessl/maven-org-bytedeco--ffmpeg

docs

audio-processing.md

codec-operations.md

command-line-tools.md

constants-enums.md

cryptographic-security.md

device-io.md

format-handling.md

hardware-acceleration.md

index.md

media-filtering.md

postproc.md

scaling-conversion.md

tile.json