Class 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();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()Finalizes the file: flushes any pending frames, writes the container index and releases native resources.abstract floatThe configured nominal frame rate.abstract intThe configured frame height in pixels.abstract intgetWidth()The configured frame width in pixels.abstract voidwriteAudio(short[] interleavedPcm, int sampleRate, int channels, long presentationTimeMillis) Writes a block of interleaved PCM audio samples at the given timestamp.voidwriteAudio(AudioBuffer buffer, long presentationTimeMillis) Convenience method that writes the current contents of anAudioBuffer(which stores floating point samples) as 16 bit PCM.abstract voidwriteFrame(int[] argb, int width, int height, long presentationTimeMillis) Writes a single video frame supplied as a raw ARGB pixel array.voidwriteFrame(Image frame, long presentationTimeMillis) Writes a single video frame at the given presentation timestamp.
-
Constructor Details
-
VideoWriter
public VideoWriter()
-
-
Method Details
-
writeFrame
Writes a single video frame at the given presentation timestamp. The frame may be any
Image; its pixels are read viaImage#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, lengthwidth * 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
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
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.
-