Interface IMediaWriter
-
- All Superinterfaces:
IMediaCoder,IMediaGenerator,IMediaListener,IMediaTool
public interface IMediaWriter extends IMediaCoder, IMediaTool
AnIMediaCoderthat encodes and decodes media to anIContainer, and can optionally read data for encoding from otherIMediaGeneratorobjects.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
IMediaWriteris a simplified interface to the AVPKit library that opens up a media container, and allows media data to be written into it.The
IMediaWriterclass implementsIMediaListener, and so it can be attached to anyIMediaGeneratorthat generates raw media events (e.g.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
encodeAudio(int, IAudioSamples)andencodeVideo(int, IVideoPicture)encode media into packets and write those packets to the specified container.If you are generating video from Java
BufferedImagebut you don't have anIVideoPictureobject handy, don't sweat. You can useencodeVideo(int, BufferedImage, long, TimeUnit)for that.If you are generating audio from in Java short arrays (16-bit audio) but don't have an
IAudioSamplesobject handy, don't sweat. You can useencodeAudio(int, short[], long, TimeUnit)for that.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description intaddAudioStream(int inputIndex, int streamId, int channelCount, int sampleRate)Add an audio stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.intaddAudioStream(int inputIndex, int streamId, ICodec.ID codecId, int channelCount, int sampleRate)Add an audio stream that will later have data encoded withencodeAudio(int, IAudioSamples).intaddAudioStream(int inputIndex, int streamId, ICodec codec, int channelCount, int sampleRate)Add an audio stream that will later have data encoded withencodeAudio(int, IAudioSamples).intaddVideoStream(int inputIndex, int streamId, int width, int height)Add a video stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.intaddVideoStream(int inputIndex, int streamId, ICodec.ID codecId, int width, int height)Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).intaddVideoStream(int inputIndex, int streamId, ICodec.ID codecId, IRational frameRate, int width, int height)Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).intaddVideoStream(int inputIndex, int streamId, ICodec codec, int width, int height)Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).intaddVideoStream(int inputIndex, int streamId, ICodec codec, IRational frameRate, int width, int height)Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).intaddVideoStream(int inputIndex, int streamId, IRational frameRate, int width, int height)Add a video stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.voidencodeAudio(int streamIndex, short[] samples)Encoded audio from samples into the stream with the specified index.voidencodeAudio(int streamIndex, short[] samples, long timeStamp, java.util.concurrent.TimeUnit timeUnit)Encoded audio from samples into the stream with the specified index.voidencodeAudio(int streamIndex, IAudioSamples samples)Encodes audio from samples into the stream with the specified index.voidencodeVideo(int streamIndex, IVideoPicture picture)Encodes video from the given picture into the stream with the specified index.voidencodeVideo(int streamIndex, java.awt.image.BufferedImage image, long timeStamp, java.util.concurrent.TimeUnit timeUnit)Encodes video from the given picture into the stream with the specified index.voidflush()Flushes all encoders and writes their contents.IPixelFormat.TypegetDefaultPixelType()Get the default pixel typeIAudioSamples.FormatgetDefaultSampleFormat()Get the default audio sample formatIRationalgetDefaultTimebase()Get the default time base we'll use on our encoders if one is not specified by the codec.java.lang.IntegergetOutputStreamIndex(int inputStreamIndex)Map an input stream index to an output stream index.booleanisSupportedCodecType(ICodec.Type type)Test if thisIMediaWritercan write streams of this type.voidsetForceInterleave(boolean forceInterleave)Set the force interleave option.voidsetMaskLateStreamExceptions(boolean maskLateStreamExceptions)Set late stream exception policy.booleanwillForceInterleave()Test if theIMediaWriterwill forcibly interleave media data.booleanwillMaskLateStreamExceptions()Get the late stream exception policy.-
Methods inherited from interface com.avpkit.mediatool.IMediaCoder
close, getContainer, getUrl, isOpen, open
-
Methods inherited from interface com.avpkit.mediatool.IMediaGenerator
addListener, getListeners, removeListener
-
Methods inherited from interface com.avpkit.mediatool.IMediaListener
onAddStream, onAudioSamples, onClose, onCloseCoder, onFlush, onOpen, onOpenCoder, onReadPacket, onVideoPicture, onWriteHeader, onWritePacket, onWriteTrailer
-
-
-
-
Method Detail
-
setMaskLateStreamExceptions
void setMaskLateStreamExceptions(boolean maskLateStreamExceptions)
Set late stream exception policy. WhenencodeAudio(int, IAudioSamples)orencodeVideo(int, IVideoPicture)is passed an unrecognized stream index after the the header has been written, either an exception is raised, or the media data is silently ignored. By default exceptions are raised, not masked.- Parameters:
maskLateStreamExceptions- true if late med- See Also:
willMaskLateStreamExceptions()
-
willMaskLateStreamExceptions
boolean willMaskLateStreamExceptions()
Get the late stream exception policy. WhenencodeVideo(int, IVideoPicture)orencodeAudio(int, IAudioSamples)is passed an unrecognized stream index after the the header has been written, either an exception is raised, or the media data is silently ignored. By default exceptions are raised, not masked.- Returns:
- true if late stream data raises exceptions
- See Also:
setMaskLateStreamExceptions(boolean)
-
setForceInterleave
void setForceInterleave(boolean forceInterleave)
Set the force interleave option.If false the media data will be left in the order in which it is presented to the IMediaWriter.
If true
IMediaWriterwill buffer media data in time stamp order, and only write out data when it has at least one same time or later packet from all streams.- Parameters:
forceInterleave- true if the IMediaWriter should force interleaving of media data- See Also:
willForceInterleave()
-
willForceInterleave
boolean willForceInterleave()
Test if theIMediaWriterwill forcibly interleave media data. The default value for this value is true.- Returns:
- true if
IMediaWriterinterleaves media data. - See Also:
setForceInterleave(boolean)
-
isSupportedCodecType
boolean isSupportedCodecType(ICodec.Type type)
Test if thisIMediaWritercan write streams of this type.- Parameters:
type- the type of codec to be tested- Returns:
- true if codec type is supported type
-
getDefaultPixelType
IPixelFormat.Type getDefaultPixelType()
Get the default pixel type- Returns:
- the default pixel type
-
getDefaultSampleFormat
IAudioSamples.Format getDefaultSampleFormat()
Get the default audio sample format- Returns:
- the format
-
getDefaultTimebase
IRational getDefaultTimebase()
Get the default time base we'll use on our encoders if one is not specified by the codec.- Returns:
- the default time base
-
addAudioStream
int addAudioStream(int inputIndex, int streamId, int channelCount, int sampleRate)
Add an audio stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.The time base defaults to
getDefaultTimebase()and the audio format defaults togetDefaultSampleFormat().- Parameters:
inputIndex- the index that will be passed toencodeAudio(int, IAudioSamples)for this streamstreamId- a format-dependent id for this streamchannelCount- the number of audio channels for the streamsampleRate- sample rate in Hz (samples per seconds), common values are 44100, 22050, 11025, etc.- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
IContainer,IStream,IStreamCoder,ICodec,ICodec.guessEncodingCodec(com.avpkit.core.IContainerFormat, String, String, String, com.avpkit.core.ICodec.Type)
-
addAudioStream
int addAudioStream(int inputIndex, int streamId, ICodec.ID codecId, int channelCount, int sampleRate)
Add an audio stream that will later have data encoded withencodeAudio(int, IAudioSamples).The time base defaults to
getDefaultTimebase()and the audio format defaults togetDefaultSampleFormat().- Parameters:
inputIndex- the index that will be passed toencodeAudio(int, IAudioSamples)for this streamstreamId- a format-dependent id for this streamcodecId- the codec id to used to encode data, to establish the codec seeICodecchannelCount- the number of audio channels for the streamsampleRate- sample rate in Hz (samples per seconds), common values are 44100, 22050, 11025, etc.- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
IContainer,IStream,IStreamCoder,ICodec
-
addAudioStream
int addAudioStream(int inputIndex, int streamId, ICodec codec, int channelCount, int sampleRate)
Add an audio stream that will later have data encoded withencodeAudio(int, IAudioSamples).The time base defaults to
getDefaultTimebase()and the audio format defaults togetDefaultSampleFormat().- Parameters:
inputIndex- the index that will be passed toencodeAudio(int, IAudioSamples)for this streamstreamId- a format-dependent id for this streamcodec- the codec to used to encode data, to establish the codec seeICodecchannelCount- the number of audio channels for the streamsampleRate- sample rate in Hz (samples per seconds), common values are 44100, 22050, 11025, etc.- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0- See Also:
IContainer,IStream,IStreamCoder,ICodec
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, int width, int height)
Add a video stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamwidth- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
IContainer,IStream,IStreamCoder,ICodec,ICodec.guessEncodingCodec(com.avpkit.core.IContainerFormat, String, String, String, com.avpkit.core.ICodec.Type)
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, ICodec.ID codecId, int width, int height)
Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamcodecId- the codec to used to encode data, to establish the codec seeICodecwidth- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
IContainer,IStream,IStreamCoder,ICodec
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, ICodec codec, int width, int height)
Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamcodec- the codec to used to encode data, to establish the codec seeICodecwidth- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0- See Also:
IContainer,IStream,IStreamCoder,ICodec
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, IRational frameRate, int width, int height)
Add a video stream with a codec guessed based onIMediaCoder.getUrl()of thisIMediaWriter.The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamframeRate- The frame rate if known or required by the output codec. SeeaddVideoStream(int, int, com.avpkit.core.ICodec.ID, IRational, int, int).width- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
addVideoStream(int, int, com.avpkit.core.ICodec.ID, IRational, int, int),IContainer,IStream,IStreamCoder,ICodec,ICodec.guessEncodingCodec(com.avpkit.core.IContainerFormat, String, String, String, com.avpkit.core.ICodec.Type)
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, ICodec.ID codecId, IRational frameRate, int width, int height)
Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamcodecId- the codec to used to encode data, to establish the codec seeICodecframeRate- The frame rate if known or required by the output codec. SeeaddVideoStream(int, int, com.avpkit.core.ICodec.ID, IRational, int, int).width- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0java.lang.UnsupportedOperationException- if the given codec cannot be used for encoding.- See Also:
addVideoStream(int, int, com.avpkit.core.ICodec.ID, IRational, int, int),IContainer,IStream,IStreamCoder,ICodec
-
addVideoStream
int addVideoStream(int inputIndex, int streamId, ICodec codec, IRational frameRate, int width, int height)
Add a video stream that will later have data encoded withencodeVideo(int, IVideoPicture).The time base defaults to
getDefaultTimebase()and the pixel format defaults togetDefaultPixelType().- Parameters:
inputIndex- the index that will be passed toencodeVideo(int, IVideoPicture)for this streamstreamId- a format-dependent id for this streamcodec- the codec to used to encode data, to establish the codec seeICodecframeRate- if non null, then this is the frame-rate you will encode video at. Some codecs (e.g. MPEG2 video) require this to be fixed and will effectively ignore timestamps in favor of this. If you do not specify, we'll guess the highest frame-rate that can fit in the output container, and then try to honor the actual timestamps encoded.width- width of video framesheight- height of video frames- Returns:
- <0 on failure; otherwise returns the index of the new stream added by the writer.
- Throws:
java.lang.IllegalArgumentException- if inputIndex < 0, the stream id < 0, the codec is NULL or if the container is already open.java.lang.IllegalArgumentException- if width or height are <= 0- See Also:
IContainer,IStream,IStreamCoder,ICodec
-
encodeAudio
void encodeAudio(int streamIndex, IAudioSamples samples)
Encodes audio from samples into the stream with the specified index.Callers must ensure that
IMediaData.getTimeStamp(), if specified is always monotonically increasing or an error will be returned.- Parameters:
streamIndex- The stream index, as returned fromaddAudioStream(int, int, ICodec, int, int).samples- A set of samples to add.
-
encodeAudio
void encodeAudio(int streamIndex, short[] samples)
Encoded audio from samples into the stream with the specified index.IMediaWriterwill assume that these samples are to played immediately after the last set of samples, or with the earliest time stamp in the container if they are the first samples.- Parameters:
streamIndex- The stream index, as returned fromaddAudioStream(int, int, ICodec, int, int).samples- A set of samples to add.
-
encodeAudio
void encodeAudio(int streamIndex, short[] samples, long timeStamp, java.util.concurrent.TimeUnit timeUnit)
Encoded audio from samples into the stream with the specified index.If
timeUnitis null,IMediaWriterwill assume that these samples are to played immediately after the last set of samples, or with the earliest time stamp in the container if they are the first samples.Callers must ensure that
timeStamp, iftimeUnitis non-null, is always monotonically increasing or an runtime exception will be raised.- Parameters:
streamIndex- The stream index, as returned fromaddAudioStream(int, int, ICodec, int, int).samples- A set of samples to add.timeStamp- The time stamp for this media.timeUnit- The units of timeStamp, or null if you wantIMediaWriterto assume these samples immediately precede any prior samples.
-
encodeVideo
void encodeVideo(int streamIndex, IVideoPicture picture)
Encodes video from the given picture into the stream with the specified index.Callers must ensure that
IMediaData.getTimeStamp(), if specified is always monotonically increasing or anRuntimeExceptionwill be raised.- Parameters:
streamIndex- The stream index, as returned fromaddVideoStream(int, int, ICodec, int, int).picture- A picture to encode
-
encodeVideo
void encodeVideo(int streamIndex, java.awt.image.BufferedImage image, long timeStamp, java.util.concurrent.TimeUnit timeUnit)
Encodes video from the given picture into the stream with the specified index.Callers must ensure that
IMediaData.getTimeStamp(), if specified is always monotonically increasing or anRuntimeExceptionwill be raised.- Parameters:
streamIndex- The stream index, as returned fromaddVideoStream(int, int, ICodec, int, int).image- ABufferedImageto encodetimeStamp- The time stamp for this imagetimeUnit- The time unit of timeStamp. Cannot be null.
-
flush
void flush()
Flushes all encoders and writes their contents.Callers should call this when they have finished encoding all audio and video to ensure that any cached data necessary for encoding was written.
-
getOutputStreamIndex
java.lang.Integer getOutputStreamIndex(int inputStreamIndex)
Map an input stream index to an output stream index.- Parameters:
inputStreamIndex- the input stream index value- Returns:
- the associated output stream index or null, if the input stream index has not been mapped to an output index.
-
-