public abstract class VideoWriter
- Object
- 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
Inherited methods
Constructor details
VideoWriter
public VideoWriter()Method details
writeFrame
public void writeFrame(Image frame, long presentationTimeMillis)
throws IOExceptionWrites 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
frameImage- the frame to encode, expected to match the configured width and height
presentationTimeMillislong- 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 IOExceptionWrites 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
argbint[]- the frame pixels in ARGB order, length
width * height widthint- the frame width in pixels
heightint- the frame height in pixels
presentationTimeMillislong- 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 IOExceptionWrites 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
interleavedPcmshort[]- signed 16 bit interleaved samples
sampleRateint- the sample rate of the supplied data in Hz
channelsint- the number of interleaved channels
presentationTimeMillislong- the timestamp of the first sample in milliseconds
Throws
IOException- if encoding fails
writeAudio
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. The buffer’s own sample rate and
channel count are used.Parameters
bufferAudioBuffer- the audio buffer to write
presentationTimeMillislong- the timestamp of the first sample in milliseconds
Throws
IOException- if encoding fails
close
public abstract void close()
throws IOExceptionFinalizes 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.