MediaMuxer

public final class MediaMuxer
extends Object

java.lang.Object
   ↳ android.media.MediaMuxer


MediaMuxer facilitates muxing elementary streams. Currently MediaMuxer supports MP4, Webm and 3GP file as the output. It also supports muxing B-frames in MP4 since Android Nougat.

It is generally used like this:

 MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
 // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
 // or MediaExtractor.getTrackFormat().
 MediaFormat audioFormat = new MediaFormat(...);
 MediaFormat videoFormat = new MediaFormat(...);
 int audioTrackIndex = muxer.addTrack(audioFormat);
 int videoTrackIndex = muxer.addTrack(videoFormat);
 ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
 boolean finished = false;
 BufferInfo bufferInfo = new BufferInfo();

 muxer.start();
 while(!finished) {
   // getInputBuffer() will fill the inputBuffer with one frame of encoded
   // sample from either MediaCodec or MediaExtractor, set isAudioSample to
   // true when the sample is audio data, set up all the fields of bufferInfo,
   // and return true if there are no more samples.
   finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
   if (!finished) {
     int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
     muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
   }
 };
 muxer.stop();
 muxer.release();
 

Metadata Track

Per-frame metadata carries information that correlates with video or audio to facilitate offline processing. For example, gyro signals from the sensor can help video stabilization when doing offline processing. Metadata tracks are only supported when multiplexing to the MP4 container format. When adding a new metadata track, the MIME type format must start with prefix "application/" (for example, "application/gyro"). The format of the metadata is application-defined. Metadata timestamps must be in the same time base as video and audio timestamps. The generated MP4 file uses TextMetaDataSampleEntry (defined in section 12.3.3.2 of the ISOBMFF specification) to signal the metadata's MIME type.

   MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
   // SetUp Video/Audio Tracks.
   MediaFormat audioFormat = new MediaFormat(...);
   MediaFormat videoFormat = new MediaFormat(...);
   int audioTrackIndex = muxer.addTrack(audioFormat);
   int videoTrackIndex = muxer.addTrack(videoFormat);
 
   // Setup Metadata Track
   MediaFormat metadataFormat = new MediaFormat(...);
   metadataFormat.setString(KEY_MIME, "application/gyro");
   int metadataTrackIndex = muxer.addTrack(metadataFormat);
 
   muxer.start();
   while(..) {
       // Allocate bytebuffer and write gyro data(x,y,z) into it.
       ByteBuffer metaData = ByteBuffer.allocate(bufferSize);
       metaData.putFloat(x);
       metaData.putFloat(y);
       metaData.putFloat(z);
       BufferInfo metaInfo = new BufferInfo();
       // Associate this metadata with the video frame by setting
       // the same timestamp as the video frame.
       metaInfo.presentationTimeUs = currentVideoTrackTimeUs;
       metaInfo.offset = 0;
       metaInfo.flags = 0;
       metaInfo.size = bufferSize;
       muxer.writeSampleData(metadataTrackIndex, metaData, metaInfo);
   };
   muxer.stop();
   muxer.release();
 }

Features and API History

The following table summarizes the feature support in different API version and containers. For API version numbers, see Build.VERSION_CODES.

Symbol Meaning
Supported
Not supported
Supported in MP4/WebM/3GP
Only Supported in MP4
Feature SDK Version
18 19 20 21 22 23 24 25 26+
MP4 container
WebM container
3GP container
Muxing B-Frames(bi-directional predicted frames)
Muxing Single Video/Audio Track
Muxing Multiple Video/Audio Tracks
Muxing Metadata Tracks

Summary

Nested classes

class MediaMuxer.OutputFormat

Defines the output format. 

Public constructors

MediaMuxer(String path, int format)

Creates a media muxer that writes to the specified path.

MediaMuxer(FileDescriptor fd, int format)

Creates a media muxer that writes to the specified FileDescriptor.

Public methods

int addTrack(MediaFormat format)

Adds a track with the specified format.

void release()

Make sure you call this when you're done to free up any resources instead of relying on the garbage collector to do this for you at some point in the future.

void setLocation(float latitude, float longitude)

Set and store the geodata (latitude and longitude) in the output file.

void setOrientationHint(int degrees)

Sets the orientation hint for output video playback.

void start()

Starts the muxer.

void stop()

Stops the muxer.

void writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo)

Writes an encoded sample into the muxer.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

Public constructors

MediaMuxer

Added in API level 18
public MediaMuxer (String path, 
                int format)

Creates a media muxer that writes to the specified path.

The caller must not use the file path before calling stop().

Parameters
path String: The path of the output media file. This value cannot be null.

format int: The format of the output media file. Value is MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4, MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP, MediaMuxer.OutputFormat.MUXER_OUTPUT_HEIF, or MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG

Throws
IllegalArgumentException if path is invalid or format is not supported.
IOException if an error occurs while opening or creating the output file.

MediaMuxer

Added in API level 18
public MediaMuxer (FileDescriptor fd, 
                int format)

Creates a media muxer that writes to the specified FileDescriptor.

The caller must not use the file referenced by the specified fd before calling stop().

It is the caller's responsibility to close the file descriptor, which is safe to do so as soon as this call returns.

Parameters
fd FileDescriptor: The FileDescriptor of the output media file. If format is OutputFormat#MUXER_OUTPUT_WEBM, fd must be open in read-write mode. Otherwise, write mode is sufficient, but read-write is also accepted. This value cannot be null.

format int: The format of the output media file. Value is MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4, MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP, MediaMuxer.OutputFormat.MUXER_OUTPUT_HEIF, or MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG

Throws
IllegalArgumentException if format is not supported, or if fd is not open in the expected mode.
IOException if an error occurs while performing an IO operation.

Public methods

addTrack

Added in API level 18
public int addTrack (MediaFormat format)

Adds a track with the specified format.

The following table summarizes support for specific format keys across android releases. Keys marked with '+:' are required.

OS Version(s) MediaFormat keys used for
All Tracks Audio Tracks Video Tracks
Build.VERSION_CODES.JELLY_BEAN_MR2 +: MediaFormat#KEY_MIME +: MediaFormat#KEY_SAMPLE_RATE,
+: MediaFormat#KEY_CHANNEL_COUNT,
+: codec-specific dataAAC
+: MediaFormat#KEY_WIDTH,
+: MediaFormat#KEY_HEIGHT,
no KEY_ROTATION, use setOrientationHint().mp4,
+: codec-specific dataAVC, MPEG4
Build.VERSION_CODES.KITKAT
Build.VERSION_CODES.KITKAT_WATCH
Build.VERSION_CODES.LOLLIPOP as above, plus
+: codec-specific dataVorbis & .webm
Build.VERSION_CODES.LOLLIPOP_MR1
Build.VERSION_CODES.M as above, plus
MediaFormat#KEY_BIT_RATEAAC
Build.VERSION_CODES.N as above, plus
MediaFormat#KEY_BIT_RATEMPEG4,
MediaFormat#KEY_HDR_STATIC_INFO#, .webm,
MediaFormat#KEY_COLOR_STANDARD#,
MediaFormat#KEY_COLOR_TRANSFER#,
MediaFormat#KEY_COLOR_RANGE#,
+: codec-specific dataHEVC,
codec-specific dataVP9

Notes:
#: storing into container metadata.
.mp4, .webm…: for listed containers
MPEG4, AAC…: for listed codecs

Note that the codec-specific data for the track must be specified using this method. Furthermore, codec-specific data must not be passed/specified via the writeSampleData() call.

The following table summarizes codec support for containers across android releases:

OS Version(s) Codec support
MP4 WEBM
Build.VERSION_CODES.JELLY_BEAN_MR2 AAC,
NB-AMR,
WB-AMR,
H.263,
MPEG-4,
AVC (H.264)
Not supported
Build.VERSION_CODES.KITKAT
Build.VERSION_CODES.KITKAT_WATCH
Build.VERSION_CODES.LOLLIPOP Vorbis,
VP8
Build.VERSION_CODES.LOLLIPOP_MR1
Build.VERSION_CODES.M
Build.VERSION_CODES.N as above, plus
HEVC (H.265)
as above, plus
VP9

Parameters
format MediaFormat: The media format for the track. This must not be an empty MediaFormat. This value cannot be null.

Returns
int The track index for this newly added track, and it should be used in the writeSampleData(int, ByteBuffer, BufferInfo).

Throws
IllegalArgumentException if format is invalid.
IllegalStateException if muxer is in the wrong state.

release

Added in API level 18
public void release ()

Make sure you call this when you're done to free up any resources instead of relying on the garbage collector to do this for you at some point in the future.

setLocation

Added in API level 19
public void setLocation (float latitude, 
                float longitude)

Set and store the geodata (latitude and longitude) in the output file. This method should be called before start(). The geodata is stored in udta box if the output format is OutputFormat#MUXER_OUTPUT_MPEG_4, and is ignored for other output formats. The geodata is stored according to ISO-6709 standard.

Parameters
latitude float: Latitude in degrees. Its value must be in the range [-90, 90].

longitude float: Longitude in degrees. Its value must be in the range [-180, 180].

Throws
IllegalArgumentException If the given latitude or longitude is out of range.
IllegalStateException If this method is called after start().

setOrientationHint

Added in API level 18
public void setOrientationHint (int degrees)

Sets the orientation hint for output video playback.

This method should be called before start(). Calling this method will not rotate the video frame when muxer is generating the file, but add a composition matrix containing the rotation angle in the output video if the output format is OutputFormat#MUXER_OUTPUT_MPEG_4 so that a video player can choose the proper orientation for playback. Note that some video players may choose to ignore the composition matrix in a video during playback. By default, the rotation degree is 0.

Parameters
degrees int: the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees.

Throws
IllegalArgumentException if degree is not supported.
IllegalStateException If this method is called after start().

start

Added in API level 18
public void start ()

Starts the muxer.

Make sure this is called after addTrack(MediaFormat) and before writeSampleData(int, ByteBuffer, BufferInfo).

Throws
IllegalStateException If this method is called after start() or Muxer is released

stop

Added in API level 18
public void stop ()

Stops the muxer.

Once the muxer stops, it can not be restarted.

Throws
IllegalStateException if muxer is in the wrong state.

writeSampleData

Added in API level 18
public void writeSampleData (int trackIndex, 
                ByteBuffer byteBuf, 
                MediaCodec.BufferInfo bufferInfo)

Writes an encoded sample into the muxer.

The application needs to make sure that the samples are written into the right tracks. Also, it needs to make sure the samples for each track are written in chronological order (e.g. in the order they are provided by the encoder.)

For MPEG4 media format, the duration of the last sample in a track can be set by passing an additional empty buffer(bufferInfo.size = 0) with MediaCodec.BUFFER_FLAG_END_OF_STREAM flag and a suitable presentation timestamp set in bufferInfo parameter as the last sample of that track. This last sample's presentation timestamp shall be a sum of the presentation timestamp and the duration preferred for the original last sample. If no explicit END_OF_STREAM sample was passed, then the duration of the last sample would be the same as that of the sample before that.

Parameters
trackIndex int: The track index for this sample.

byteBuf ByteBuffer: The encoded sample. This value cannot be null.

bufferInfo MediaCodec.BufferInfo: The buffer information related to this sample. This value cannot be null.

Throws
IllegalArgumentException if trackIndex, byteBuf or bufferInfo is invalid.
IllegalStateException if muxer is in wrong state. MediaMuxer uses the flags provided in MediaCodec.BufferInfo, to signal sync frames.

Protected methods

finalize

Added in API level 18
protected void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize 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.

Throws
Throwable