Here's some pseudo code that shows how to encode an audio file:
IMediaWriter writer = ToolFactory.makeWriter("output.mp3"); int sampleRate = 22050; int channels = 1; writer.addAudioStream(0, 0, sampleRate, channels); while(haveMoreAudio()) { short[] samples = getSourceSamples(); writer.encodeAudio(0, samples); } writer.close();
And here's some pseudo code that shows how to encode a video image every 1 second:
IMediaWriter writer = ToolFactory.makeWriter("output.mp4"); writer.addVideoStream(0, 0, sampleRate, channels); long startTime = System.nanoTime(); while(haveMoreVideo()) { BufferedImage image = getImage(); writer.encodeAudio(0, image, System.nanoTime()-startTime, TimeUnit.NANOSECONDS); Thread.sleep(1000); } writer.close();
An {@link IMediaWriter} is a simplified interface to the Xuggler library thatopens up a media container, and allows media data to be written into it.
The {@link IMediaWriter} class implements {@link IMediaListener}, and so it can be attached to any {@link IMediaGenerator} that generates raw mediaevents (e.g. {@link IMediaReader}). If will query the input pipe for all of it's streams, and create the right output streams (and mappings) in the new file.
Calls to {@link #encodeAudio(int,IAudioSamples)} and{@link #encodeVideo(int,IVideoPicture)} encode media into packets and writethose packets to the specified container.
If you are generating video from Java {@link BufferedImage} but you don'thave an {@link IVideoPicture} object handy, don't sweat. You can use{@link #encodeVideo(int,BufferedImage,long,TimeUnit)} for that.
If you are generating audio from in Java short arrays (16-bit audio) but don't have an {@link IAudioSamples} object handy, don't sweat. You can use{@link #encodeAudio(int,short[],long,TimeUnit)} for that.
|
|
|
|
|
|
|
|
|
|
|
|