public abstract class VideoWriter

  1. Object
  2. VideoWriter

Encodes application supplied frames and audio into a video file using the platform’s native codecs. Obtain a VideoWriter from a VideoWriterBuilder, then push video frames (each as an Image you fully control, with an explicit presentation timestamp) and, optionally, interleaved PCM audio. Call #close() to finalize and mux the file.

This is the encode counterpart of VideoReader. Because every frame is just an Image, you have complete control over the pixels: render charts, overlays, transformed camera frames, generated animation, etc. into the image’s Graphics before handing it to the writer.

Example:

VideoWriter w = new VideoWriterBuilder().path(out).width(640).height(480).frameRate(30).build();
for (int i = 0; i < 90; i++) {
    Image frame = Image.createImage(640, 480, 0xff000000);
    Graphics g = frame.getGraphics();
    g.setColor(0xffffff);
    g.fillRect(i * 6 % 640, 0, 40, 480);
    w.writeFrame(frame, Math.round(i * 1000f / 30f));
}
w.close();

Constructors

public VideoWriter()

Methods

public void writeFrame(Image frame, long presentationTimeMillis) throws IOExceptionWrites a single video frame at the given presentation timestamp.
public abstract void writeFrame(int[] argb, int width, int height, long presentationTimeMillis) throws IOExceptionWrites a single video frame supplied as a raw ARGB pixel array.
public abstract void writeAudio(short[] interleavedPcm, int sampleRate, int channels, long presentationTimeMillis) throws IOExceptionWrites a block of interleaved PCM audio samples at the given timestamp.
public void writeAudio(AudioBuffer buffer, long presentationTimeMillis) throws IOExceptionConvenience method that writes the current contents of an AudioBuffer (which stores floating point samples) as 16 bit PCM.
public abstract void close() throws IOExceptionFinalizes the file: flushes any pending frames, writes the container index and releases native resources.
public abstract int getWidth()The configured frame width in pixels.
public abstract int getHeight()The configured frame height in pixels.
public abstract float getFrameRate()The configured nominal frame rate.

Inherited methods

Constructor details

VideoWriter

public VideoWriter()

Method details

writeFrame

public void writeFrame(Image frame, long presentationTimeMillis) throws IOException
Writes a single video frame at the given presentation timestamp. The frame may be any Image; its pixels are read via Image#getRGB(). Frames should be supplied in non decreasing timestamp order.

Parameters

frame Image
the frame to encode, expected to match the configured width and height
presentationTimeMillis long
the timestamp of this frame in milliseconds

Throws

IOException
if encoding fails

writeFrame

public abstract void writeFrame(int[] argb, int width, int height, long presentationTimeMillis) throws IOException
Writes a single video frame supplied as a raw ARGB pixel array. This is the method platform implementations provide; #writeFrame(Image, long) funnels into it.

Parameters

argb int[]
the frame pixels in ARGB order, length width * height
width int
the frame width in pixels
height int
the frame height in pixels
presentationTimeMillis long
the timestamp of this frame in milliseconds

Throws

IOException
if encoding fails

writeAudio

public abstract void writeAudio(short[] interleavedPcm, int sampleRate, int channels, long presentationTimeMillis) throws IOException
Writes a block of interleaved PCM audio samples at the given timestamp. Samples are signed 16 bit, interleaved by channel. The audio track must have been enabled with VideoWriterBuilder#hasAudio(boolean).

Parameters

interleavedPcm short[]
signed 16 bit interleaved samples
sampleRate int
the sample rate of the supplied data in Hz
channels int
the number of interleaved channels
presentationTimeMillis long
the timestamp of the first sample in milliseconds

Throws

IOException
if encoding fails

writeAudio

public void writeAudio(AudioBuffer buffer, long presentationTimeMillis) throws IOException
Convenience method that writes the current contents of an AudioBuffer (which stores floating point samples) as 16 bit PCM. The buffer’s own sample rate and channel count are used.

Parameters

buffer AudioBuffer
the audio buffer to write
presentationTimeMillis long
the timestamp of the first sample in milliseconds

Throws

IOException
if encoding fails

close

public abstract void close() throws IOException
Finalizes the file: flushes any pending frames, writes the container index and releases native resources. The writer cannot be used after this call.

Throws

IOException
if finalization fails

getWidth

public abstract int getWidth()
The configured frame width in pixels.

getHeight

public abstract int getHeight()
The configured frame height in pixels.

getFrameRate

public abstract float getFrameRate()
The configured nominal frame rate.