Class VideoWriter

java.lang.Object
com.codename1.media.VideoWriter

public abstract class VideoWriter extends Object

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();
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

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

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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: the frame to encode, expected to match the configured width and height

      • presentationTimeMillis: the timestamp of this frame in milliseconds

      Throws
      • IOException: if encoding fails
      Throws:
      IOException
    • 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: the frame pixels in ARGB order, length width * height

      • width: the frame width in pixels

      • height: the frame height in pixels

      • presentationTimeMillis: the timestamp of this frame in milliseconds

      Throws
      • IOException: if encoding fails
      Throws:
      IOException
    • 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: signed 16 bit interleaved samples

      • sampleRate: the sample rate of the supplied data in Hz

      • channels: the number of interleaved channels

      • presentationTimeMillis: the timestamp of the first sample in milliseconds

      Throws
      • IOException: if encoding fails
      Throws:
      IOException
    • 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: the audio buffer to write

      • presentationTimeMillis: the timestamp of the first sample in milliseconds

      Throws
      • IOException: if encoding fails
      Throws:
      IOException
    • 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
      Throws:
      IOException
    • 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.