public class Muxer extends Object
Example code:
String uri = "/sdcard/multimedia/media/short_video.mp4";
Extractor extractor = new Extractor();
Source mSource = new Source(uri);
boolean ret = extractor.setSource(mSource);
Muxer muxer = new Muxer("/sdcard/multimedia/media/extractor_test.mp4", Muxer.MediaFileFormat.FORMAT_MPEG4);
int trackCount = extractor.getTotalStreams();
HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
for (int i = 0; i < trackCount; i++) {
extractor.specifyStream(i);
Format format = extractor.getStreamFormat(i);
int dstIndex = muxer.appendTrack(format);
indexMap.put(i, dstIndex);
}
ret = muxer.setMediaLocation(BAD_LATITUDE, LONGITUDE);
ret = muxer.setMediaOrientation(90);
ret = muxer.start();
boolean sawEOS = false;
int bufferSize = 256 * 1024;
int offset = 100;
int totalSize = 0;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
BufferInfo bufferInfo = new BufferInfo();
while (!sawEOS) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readBuffer(dstBuf, offset);
totalSize = totalSize + bufferInfo.size;
if (bufferInfo.size < 0) {
sawEOS = true;
bufferInfo.size = 0;
} else {
bufferInfo.timeStamp = extractor.getCurrentTime();
bufferInfo.bufferType = extractor.getCurrentFlags();
int trackIndex = extractor.getCurrentTrackId();
ret = muxer.writeBuffer(indexMap.get(trackIndex), dstBuf, bufferInfo);
extractor.next();
}
}
muxer.stop();
muxer.release();
SystemCapability.Multimedia#MEDIA| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
Muxer.MediaFileFormat |
Provides media file formats, including the MPEG-4, WebM, 3GPP, and HEIF.
|
| 构造器 | 说明 |
|---|---|
Muxer(FileDescriptor fd,
int outputFormat) |
A constructor used to create a
Muxer instance based on a file descriptor and output file format. |
Muxer(String outputUri,
int outputFormat) |
A constructor used to create a
Muxer instance based on a specified URI and output file format. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
int |
appendTrack(Format format) |
Adds a media track.
|
protected void |
finalize() |
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
boolean |
release() |
Releases resources.
|
boolean |
setMediaLocation(float latitude,
float longitude) |
Sets the longitude and latitude of a media file.
|
boolean |
setMediaOrientation(int degrees) |
Sets the rotation angle of an output video.
|
boolean |
start() |
Starts media multiplexing.
|
boolean |
stop() |
Stops media multiplexing.
|
boolean |
writeBuffer(int trackIndex,
ByteBuffer byteBuf,
BufferInfo bufferInfo) |
Writes encoded data into a specified track.
|
public Muxer(String outputUri, int outputFormat)
Muxer instance based on a specified URI and output file format.outputUri - Indicates the URI of an output file.outputFormat - Indicates the output file format.IllegalArgumentException - Throws this exception if a parameter value is invalid.Muxer.MediaFileFormatpublic Muxer(FileDescriptor fd, int outputFormat)
Muxer instance based on a file descriptor and output file format.fd - Indicates the output file descriptor.outputFormat - Indicates the output file format.IllegalArgumentException - Throws this exception if a parameter value is invalid.Muxer.MediaFileFormatpublic int appendTrack(Format format)
This method must be called prior to writeBuffer(int, java.nio.ByteBuffer, ohos.media.common.BufferInfo).
format - Indicates a media track.IllegalArgumentException - Throws this exception if a parameter is invalid.public boolean start()
This method must be called after appendTrack(ohos.media.common.Format) and before writeBuffer(int, java.nio.ByteBuffer, ohos.media.common.BufferInfo).
true if multiplexing is started; returns false otherwise.public boolean stop()
true if multiplexing is stopped; returns false otherwise.public boolean release()
true if resources are released; returns false otherwise.public boolean writeBuffer(int trackIndex,
ByteBuffer byteBuf,
BufferInfo bufferInfo)
trackIndex - Indicates a track number.byteBuf - Indicates encoded data.bufferInfo - Indicates media metadata.true if the data is written into the track; returns false otherwise.public boolean setMediaLocation(float latitude,
float longitude)
latitude - Indicates the latitude ranging from -90 to 90.longitude - Indicates the longitude ranging from -180 to 180.true if the latitude and longitude are set; returns false otherwise.public boolean setMediaOrientation(int degrees)
This method must be called prior to start().
degrees - Indicates the rotation angle, which can be 0, 90, 180,
or 270 degrees.true if the rotation angle is set; returns false otherwise.protected void finalize()
throws Throwable
Objectfinalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java™ virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
finalize 在类中 ObjectThrowable - the Exception raised by this methodWeakReference,
PhantomReference