@UnstableApi
public final class Mp4Extractor implements Extractor, SeekMap


Extracts data from the MP4 container format.

Summary

Nested types

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(flag = true, value = )
public annotation Mp4Extractor.Flags

Flags controlling the behavior of the extractor.

Constants

static final ExtractorsFactory

This field is deprecated.

Use newFactory instead.

static final int
static final int

Flag to mark the first video track encountered as ROLE_FLAG_MAIN and all subsequent video tracks as ROLE_FLAG_ALTERNATE.

static final int

Flag to extract the editable video tracks.

static final int

Flag to extract MotionPhotoMetadata from HEIC motion photos following the Google Photos Motion Photo File Format V1.1.

static final int

Flag to extract SlowMotionData metadata from Samsung Extension Format (SEF) slow motion videos.

static final int

Flag to extract additional sample dependency information, and mark output buffers with BUFFER_FLAG_NOT_DEPENDED_ON.

static final int

Flag to ignore any edit lists in the stream.

Public fields

long

Public constructors

This method is deprecated.

Use Mp4Extractor instead

This method is deprecated.

Use Mp4Extractor instead

Mp4Extractor(SubtitleParser.Factory subtitleParserFactory)

Creates a new extractor for unfragmented MP4 streams.

Mp4Extractor(
    SubtitleParser.Factory subtitleParserFactory,
    @Mp4Extractor.Flags int flags
)

Creates a new extractor for unfragmented MP4 streams, using the specified flags to control the extractor's behavior.

Public methods

long

Returns the duration of the stream in microseconds.

long[]
getSampleTimestampsUs(int trackId)

Returns the list of sample timestamps of a trackId, in microseconds.

SeekMap.SeekPoints
getSeekPoints(long timeUs)

Obtains seek points for the specified seek time in microseconds.

SeekMap.SeekPoints
getSeekPoints(long timeUs, int trackId)

Equivalent to getSeekPoints, except it adds the trackId parameter.

ImmutableList<SniffFailure>

Returns additional details about the last call to sniff.

void

Initializes the extractor with an ExtractorOutput.

boolean

Returns whether seeking is supported.

static ExtractorsFactory
newFactory(SubtitleParser.Factory subtitleParserFactory)

Creates a factory for Mp4Extractor instances with the provided .

int
read(ExtractorInput input, PositionHolder seekPosition)

Extracts data read from a provided ExtractorInput.

void

Releases all kept resources.

void
seek(long position, long timeUs)

Notifies the extractor that a seek has occurred.

boolean

Returns whether this extractor can extract samples from the ExtractorInput, which must provide data from the start of the stream.

Inherited Constants

From androidx.media3.extractor.Extractor
static final int

Returned by read if the ExtractorInput passed to the next read is required to provide data continuing from the position in the stream reached by the returning call.

static final int

Returned by read if the end of the was reached.

static final int

Returned by read if the ExtractorInput passed to the next read is required to provide data starting from a specified position in the stream.

Inherited methods

From androidx.media3.extractor.Extractor
Extractor

Returns the 'real' Extractor implementation if this is a delegating instance, or this if this instance does the extraction directly without delegating (the default behaviour).

Constants

FACTORY

public static final ExtractorsFactory FACTORY

FLAG_EMIT_RAW_SUBTITLE_DATA

public static final int FLAG_EMIT_RAW_SUBTITLE_DATA = 16

FLAG_MARK_FIRST_VIDEO_TRACK_WITH_MAIN_ROLE

public static final int FLAG_MARK_FIRST_VIDEO_TRACK_WITH_MAIN_ROLE = 8

Flag to mark the first video track encountered as ROLE_FLAG_MAIN and all subsequent video tracks as ROLE_FLAG_ALTERNATE.

FLAG_READ_EDITABLE_VIDEO_TRACKS

public static final int FLAG_READ_EDITABLE_VIDEO_TRACKS = 64

Flag to extract the editable video tracks.

Either primary video tracks or editable video tracks (but not both) will be extracted based on the flag.

If the flag is set but the editable video tracks are not present, then it fallbacks to extract primary tracks instead.

FLAG_READ_MOTION_PHOTO_METADATA

public static final int FLAG_READ_MOTION_PHOTO_METADATA = 2

Flag to extract MotionPhotoMetadata from HEIC motion photos following the Google Photos Motion Photo File Format V1.1.

As playback is not supported for motion photos, this flag should only be used for metadata retrieval use cases.

FLAG_READ_SEF_DATA

public static final int FLAG_READ_SEF_DATA = 4

Flag to extract SlowMotionData metadata from Samsung Extension Format (SEF) slow motion videos.

FLAG_READ_WITHIN_GOP_SAMPLE_DEPENDENCIES

public static final int FLAG_READ_WITHIN_GOP_SAMPLE_DEPENDENCIES = 32

Flag to extract additional sample dependency information, and mark output buffers with BUFFER_FLAG_NOT_DEPENDED_ON.

This class always marks the samples at the start of each group of picture (GOP) with BUFFER_FLAG_KEY_FRAME. Usually, key frames can be decoded independently, without depending on other samples.

Setting this flag enables elementary stream parsing to identify disposable samples that are not depended on by other samples. Any disposable sample can be safely omitted, and the rest of the track will remain valid.

Supported formats are:

FLAG_WORKAROUND_IGNORE_EDIT_LISTS

public static final int FLAG_WORKAROUND_IGNORE_EDIT_LISTS = 1

Flag to ignore any edit lists in the stream.

Public fields

durationUs

public long durationUs

Public constructors

Mp4Extractor

public Mp4Extractor()

Mp4Extractor

public Mp4Extractor(@Mp4Extractor.Flags int flags)

Mp4Extractor

public Mp4Extractor(SubtitleParser.Factory subtitleParserFactory)

Creates a new extractor for unfragmented MP4 streams.

Parameters
SubtitleParser.Factory subtitleParserFactory

The SubtitleParser.Factory for parsing subtitles during extraction.

Mp4Extractor

public Mp4Extractor(
    SubtitleParser.Factory subtitleParserFactory,
    @Mp4Extractor.Flags int flags
)

Creates a new extractor for unfragmented MP4 streams, using the specified flags to control the extractor's behavior.

Parameters
SubtitleParser.Factory subtitleParserFactory

The SubtitleParser.Factory for parsing subtitles during extraction.

@Mp4Extractor.Flags int flags

Flags that control the extractor's behavior.

Public methods

getDurationUs

public long getDurationUs()

Returns the duration of the stream in microseconds.

Returns
long

The duration of the stream in microseconds, or TIME_UNSET if the duration is unknown.

getSampleTimestampsUs

public long[] getSampleTimestampsUs(int trackId)

Returns the list of sample timestamps of a trackId, in microseconds.

Parameters
int trackId

The id of the track to get the sample timestamps.

Returns
long[]

The corresponding sample timestmaps of the track.

getSeekPoints

public SeekMap.SeekPoints getSeekPoints(long timeUs)

Obtains seek points for the specified seek time in microseconds. The returned will contain one or two distinct seek points.

Two seek points [A, B] are returned in the case that seeking can only be performed to discrete points in time, there does not exist a seek point at exactly the requested time, and there exist seek points on both sides of it. In this case A and B are the closest seek points before and after the requested time. A single seek point is returned in all other cases.

Parameters
long timeUs

A seek time in microseconds.

Returns
SeekMap.SeekPoints

The corresponding seek points.

getSeekPoints

public SeekMap.SeekPoints getSeekPoints(long timeUs, int trackId)

Equivalent to getSeekPoints, except it adds the trackId parameter.

Parameters
long timeUs

A seek time in microseconds.

int trackId

The id of the track on which to seek for SeekPoints. May be INDEX_UNSET if the extractor is expected to define the strategy for generating .

Returns
SeekMap.SeekPoints

The corresponding seek points.

getSniffFailureDetails

public ImmutableList<SniffFailuregetSniffFailureDetails()

Returns additional details about the last call to sniff. The returned list may be empty if no additional details are available, or the last sniff call returned true.

This only contains details that were discovered before sniff returned false, it is not an exhaustive list of issues which, if resolved, would cause the file to be successfully sniffed.

init

public void init(ExtractorOutput output)

Initializes the extractor with an ExtractorOutput. Called at most once.

Parameters
ExtractorOutput output

An ExtractorOutput to receive extracted data.

isSeekable

public boolean isSeekable()

Returns whether seeking is supported.

Returns
boolean

Whether seeking is supported.

newFactory

public static ExtractorsFactory newFactory(SubtitleParser.Factory subtitleParserFactory)

Creates a factory for Mp4Extractor instances with the provided .

read

public int read(ExtractorInput input, PositionHolder seekPosition)

Extracts data read from a provided ExtractorInput. Must not be called before init.

A single call to this method will block until some progress has been made, but will not block for longer than this. Hence each call will consume only a small amount of input data.

In the common case, RESULT_CONTINUE is returned to indicate that the passed to the next read is required to provide data continuing from the position in the stream reached by the returning call. If the extractor requires data to be provided from a different position, then that position is set in seekPosition and RESULT_SEEK is returned. If the extractor reached the end of the data provided by the ExtractorInput, then RESULT_END_OF_INPUT is returned.

When this method throws an IOException, extraction may continue by providing an ExtractorInput with an unchanged read position to a subsequent call to this method.

Parameters
ExtractorInput input

The ExtractorInput from which data should be read.

PositionHolder seekPosition

If RESULT_SEEK is returned, this holder is updated to hold the position of the required data.

Returns
int

One of the RESULT_ values defined in this interface.

Throws
java.io.IOException

If an error occurred reading from or parsing the input.

release

public void release()

Releases all kept resources.

seek

public void seek(long position, long timeUs)

Notifies the extractor that a seek has occurred.

Following a call to this method, the ExtractorInput passed to the next invocation of read is required to provide data starting from position in the stream. Valid random access positions are the start of the stream and positions that can be obtained from any SeekMap passed to the ExtractorOutput.

Parameters
long position

The byte offset in the stream from which data will be provided.

long timeUs

The seek time in microseconds.

sniff

public boolean sniff(ExtractorInput input)

Returns whether this extractor can extract samples from the ExtractorInput, which must provide data from the start of the stream.

If true is returned, the input's reading position may have been modified. Otherwise, only its peek position may have been modified.

Parameters
ExtractorInput input

The ExtractorInput from which data should be peeked/read.

Returns
boolean

Whether this extractor can read the provided input.

Throws
java.io.IOException

If an error occurred reading from the input.