public abstract class VideoIO

  1. Object
  2. VideoIO

Low level video encoding and decoding using the platform’s native codecs. VideoIO is to video what com.codename1.ui.util.ImageIO is to still images, but goes deeper: it can enumerate the codecs the device actually supports, encode application rendered frames and audio into a standard container (VideoWriter), and decode an existing clip into frame accurate RGBA frames and PCM audio (VideoReader).

It is supported on iOS, macOS, Android, Windows, Linux, JavaScript and the desktop simulator. It is not available on the TV, Watch or Car targets. As with other optional platform features, always gate usage with #isSupported():

if (VideoIO.isSupported()) {
    VideoIO io = VideoIO.getVideoIO();
    // ... encode / decode
}

Encoding

Configure a VideoWriterBuilder, build a VideoWriter, push frames and audio, then close it. Every frame is an ordinary com.codename1.ui.Image, so the application has full control over the pixels.

Decoding

#openReader(String) returns a VideoReader exposing duration, dimensions and frame rate, frame accurate VideoReader#frameAt(long), a variable to constant frame rate resampler VideoReader#readFrames(float, com.codename1.media.VideoReader.FrameCallback) and the audio track as PCM via VideoReader#readAudio().

Fields

public static final String CODEC_H264 = "h264"Codec id for H.264 / AVC video, the most broadly compatible video codec.
public static final String CODEC_HEVC = "hevc"Codec id for H.265 / HEVC video.
public static final String CODEC_VP8 = "vp8"Codec id for VP8 video.
public static final String CODEC_VP9 = "vp9"Codec id for VP9 video.
public static final String CODEC_AV1 = "av1"Codec id for AV1 video.
public static final String CODEC_AAC = "aac"Codec id for AAC audio, the most broadly compatible audio codec.
public static final String CODEC_OPUS = "opus"Codec id for Opus audio.
public static final String CODEC_PCM = "pcm"Codec id for uncompressed 16 bit PCM audio.
public static final String CONTAINER_MP4 = "mp4"Container id for the MPEG 4 (.mp4) container.
public static final String CONTAINER_WEBM = "webm"Container id for the WebM (.webm) container.
public static final String CONTAINER_MOV = "mov"Container id for the QuickTime (.mov) container.
public static final String CONTAINER_MKV = "mkv"Container id for the Matroska (.mkv) container.

Constructors

public VideoIO()

Methods

public static VideoIO getVideoIO()Returns the VideoIO instance for the current platform, or null when video encoding/decoding is not supported.
public static boolean isSupported()Convenience test for whether this platform supports the VideoIO API.
public abstract VideoCodec[] getAvailableEncoders()Lists the codecs that can be used for encoding on this device.
public abstract VideoCodec[] getAvailableDecoders()Lists the codecs that can be used for decoding on this device.
public boolean isEncoderSupported(String codecId)Tests whether a given codec id can be used for encoding on this device.
public boolean isDecoderSupported(String codecId)Tests whether a given codec id can be used for decoding on this device.
public abstract VideoWriter createWriter(VideoWriterBuilder cfg) throws IOExceptionCreates a VideoWriter for the supplied configuration.
public abstract VideoReader openReader(String filePath) throws IOExceptionOpens a VideoReader for the clip at the supplied com.codename1.io.FileSystemStorage path.
public VideoReader openReader(InputStream stream, String mimeType) throws IOExceptionOpens a VideoReader for a clip supplied as a stream.

Inherited methods

Field details

CODEC_H264

public static final String CODEC_H264 = "h264"
Codec id for H.264 / AVC video, the most broadly compatible video codec.

CODEC_HEVC

public static final String CODEC_HEVC = "hevc"
Codec id for H.265 / HEVC video.

CODEC_VP8

public static final String CODEC_VP8 = "vp8"
Codec id for VP8 video.

CODEC_VP9

public static final String CODEC_VP9 = "vp9"
Codec id for VP9 video.

CODEC_AV1

public static final String CODEC_AV1 = "av1"
Codec id for AV1 video.

CODEC_AAC

public static final String CODEC_AAC = "aac"
Codec id for AAC audio, the most broadly compatible audio codec.

CODEC_OPUS

public static final String CODEC_OPUS = "opus"
Codec id for Opus audio.

CODEC_PCM

public static final String CODEC_PCM = "pcm"
Codec id for uncompressed 16 bit PCM audio.

CONTAINER_MP4

public static final String CONTAINER_MP4 = "mp4"
Container id for the MPEG 4 (.mp4) container.

CONTAINER_WEBM

public static final String CONTAINER_WEBM = "webm"
Container id for the WebM (.webm) container.

CONTAINER_MOV

public static final String CONTAINER_MOV = "mov"
Container id for the QuickTime (.mov) container.

CONTAINER_MKV

public static final String CONTAINER_MKV = "mkv"
Container id for the Matroska (.mkv) container.

Constructor details

VideoIO

public VideoIO()

Method details

getVideoIO

public static VideoIO getVideoIO()
Returns the VideoIO instance for the current platform, or null when video encoding/decoding is not supported. Prefer #isSupported() for a simple boolean check.

Returns

the platform VideoIO, or null if unsupported

isSupported

public static boolean isSupported()
Convenience test for whether this platform supports the VideoIO API.

Returns

true if video encoding/decoding is available

getAvailableEncoders

public abstract VideoCodec[] getAvailableEncoders()
Lists the codecs that can be used for encoding on this device. The set is device dependent and may include hardware accelerated entries (see VideoCodec#isHardwareAccelerated()).

Returns

the available encoder codecs (never null, possibly empty)

getAvailableDecoders

public abstract VideoCodec[] getAvailableDecoders()
Lists the codecs that can be used for decoding on this device.

Returns

the available decoder codecs (never null, possibly empty)

isEncoderSupported

public boolean isEncoderSupported(String codecId)
Tests whether a given codec id can be used for encoding on this device.

Parameters

codecId String
one of the #CODEC_H264 style constants

Returns

true if an encoder for the codec exists

isDecoderSupported

public boolean isDecoderSupported(String codecId)
Tests whether a given codec id can be used for decoding on this device.

Parameters

codecId String
one of the #CODEC_H264 style constants

Returns

true if a decoder for the codec exists

createWriter

public abstract VideoWriter createWriter(VideoWriterBuilder cfg) throws IOException
Creates a VideoWriter for the supplied configuration. Application code normally uses VideoWriterBuilder#build() rather than calling this directly.

Parameters

cfg VideoWriterBuilder
the encoder configuration

Returns

a ready to use writer

Throws

IOException
if the writer cannot be created

openReader

public abstract VideoReader openReader(String filePath) throws IOException
Opens a VideoReader for the clip at the supplied com.codename1.io.FileSystemStorage path.

Parameters

filePath String
the FileSystemStorage path of the clip to decode

Returns

a reader positioned at the start of the clip

Throws

IOException
if the clip cannot be opened

openReader

public VideoReader openReader(InputStream stream, String mimeType) throws IOException
Opens a VideoReader for a clip supplied as a stream. The default implementation spools the stream to a temporary file and opens that; platforms that can decode directly from a stream may override it. The stream is fully consumed and closed.

Parameters

stream InputStream
the clip data
mimeType String
the clip mime type (e.g. “video/mp4”), used to pick a file suffix; may be null

Returns

a reader for the clip

Throws

IOException
if the clip cannot be opened