MediaMuxerCompat
public final class MediaMuxerCompat
A drop-in replacement for MediaMuxer that provides similar functionality, based on the media3.muxer logic.
Currently only MP4 file format is supported.
Supported codecs are:
- Video Codecs:
- AV1
- MPEG-4
- H.263
- H.264 (AVC)
- H.265 (HEVC)
- VP9
- APV
- Dolby Vision
- Audio Codecs:
- AAC
- AMR-NB (Narrowband AMR)
- AMR-WB (Wideband AMR)
- Opus
- Vorbis
- Raw Audio
All the methods should be called from the same thread.
All the operations are performed on the caller thread.
Summary
Nested types |
|---|
@DocumentedThe output file format. |
Constants |
|
|---|---|
static final int |
The MP4 file format. |
Public constructors |
|---|
MediaMuxerCompat(Creates an instance. |
MediaMuxerCompat(Creates an instance. |
Public methods |
|
|---|---|
int |
addTrack(MediaFormat format)Adds a track of the given media format. |
void |
release()Releases the underlying resources. |
void |
setLocation(Sets the location of the media file. |
void |
setOrientationHint(int degrees)Sets the orientation hint for the media file. |
void |
start()Starts the muxer. |
void |
stop()Stops the muxer. |
void |
writeSampleData(Writes encoded sample data. |
Constants
Public constructors
MediaMuxerCompat
public MediaMuxerCompat(
FileDescriptor fileDescriptor,
@MediaMuxerCompat.OutputFormat int outputFormat
)
Creates an instance.
It is the caller's responsibility to close the fileDescriptor, which is safe to do so as soon as this call returns.
| Parameters | |
|---|---|
FileDescriptor fileDescriptor |
A |
@MediaMuxerCompat.OutputFormat int outputFormat |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs while performing an I/O operation. |
MediaMuxerCompat
public MediaMuxerCompat(
String filePath,
@MediaMuxerCompat.OutputFormat int outputFormat
)
Creates an instance.
| Parameters | |
|---|---|
String filePath |
The path of the output media file. |
@MediaMuxerCompat.OutputFormat int outputFormat |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs while performing an I/O operation. |
Public methods
addTrack
public int addTrack(MediaFormat format)
Adds a track of the given media format.
All tracks must be added before any samples are written to any track.
KEY_CAPTURE_RATE is used to write KEY_ANDROID_CAPTURE_FPS metadata in the MP4 file.
| Parameters | |
|---|---|
MediaFormat format |
The |
| Returns | |
|---|---|
int |
A track index for this track, which should be passed to |
| See also | |
|---|---|
addTrack |
release
public void release()
Releases the underlying resources.
It should be called after stop.
| See also | |
|---|---|
release |
setLocation
public void setLocation(
@FloatRange(from = "-90.0", to = 90.0) float latitude,
@FloatRange(from = "-180.0", to = 180.0) float longitude
)
Sets the location of the media file.
It must be called before start.
| Parameters | |
|---|---|
@FloatRange(from = "-90.0", to = 90.0) float latitude |
The latitude, in degrees. Its value must be in the range [-90, 90]. |
@FloatRange(from = "-180.0", to = 180.0) float longitude |
The longitude, in degrees. Its value must be in the range [-180, 180]. |
| See also | |
|---|---|
setLocation |
setOrientationHint
public void setOrientationHint(int degrees)
Sets the orientation hint for the media file.
It must be called before start.
| Parameters | |
|---|---|
int degrees |
The orientation, in degrees. The supported values are 0, 90, 180 and 270 (degrees). |
| See also | |
|---|---|
setOrientationHint |
start
public void start()
Starts the muxer.
This must be called after addTrack and before writeSampleData.
| See also | |
|---|---|
start |
stop
public void stop()
Stops the muxer.
Once the muxer is stopped, it can not be restarted.
| See also | |
|---|---|
stop |
writeSampleData
public void writeSampleData(
int trackIndex,
ByteBuffer byteBuffer,
MediaCodec.BufferInfo bufferInfo
)
Writes encoded sample data.
To set the duration of the last sample in a track, an additional empty buffer (
bufferInfo.size = 0) with BUFFER_FLAG_END_OF_STREAM flag and a suitable presentation timestamp must be passed as the last sample of that track. This timestamp should be the sum of the desired duration and the presentation timestamp of the original last sample. If no explicit END_OF_STREAM sample is provided, the last sample's duration will be equal to the previous sample's duration.
| Parameters | |
|---|---|
int trackIndex |
The track index, previously returned by |
ByteBuffer byteBuffer |
A buffer containing the sample data to write. |
MediaCodec.BufferInfo bufferInfo |
The |
| See also | |
|---|---|
writeSampleData |