View Source ExMP4.Reader (MP4 Reader and Writer v0.6.0)

This module contains function to read mp4 sources.

The following fields are public:

  • duration - The duration of the mp4 mapped in :timescale unit.
  • timescale - The timescale of the mp4.
  • fragmented? - The MP4 file is fragmented.
  • creation_time - Creation date time of the presentation.
  • modification_time - Modification date time of the presentation.
  • major_brand
  • minor_version
  • compatible_brands

Examples

# Read a file
{:ok, reader} = ExMP4.Reader.new("some_mp4_file.mp4")

IO.inspect("Duration: #{reader.duration}")
IO.inspect("Timescale: #{reader.timescale}")
IO.inspect("Major Brand: #{reader.major_brand}")

# Get tracks information
for track <- ExMP4.Reader.tracks(reader) do
  IO.inspect("Track information :")
  IO.inspect("====================")
  IO.inspect("  Id: #{track.id}")
  IO.inspect("  Type: #{track.type}")
  IO.inspect("  Media: #{track.media}")
  IO.inspect("  Duration: #{track.duration}")
  IO.inspect("  Timescale: #{track.timescale}")
  IO.inspect("")
end

# Read samples from the first track
track = ExMP4.Reader.tracks(reader) |> List.first()
Enum.each(0..(track.sample_count - 1), fn sample_id ->
  sample = ExMP4.Reader.read_sample(reader, track.id, sample_id)
  IO.inspect("Size of the sample: #{byte_size(sample.content)}")
end)

MP4 Source

In the last example, the source of the mp4 is a file, to read from other sources (binary, http server, ...etc.), you need to write a module that implements the ExMP4.Read behaviour.

Summary

Types

Stream options.

t()

Struct describing an MP4 reader.

Functions

Close the reader and free resources.

Get the duration of the stream.

The same as new/1, but raises if it fails.

Read a sample from the specified track.

Stream the samples' metadata.

Get all the available tracks.

Types

@type stream_opts() :: [{:tracks, [non_neg_integer()]}]

Stream options.

  • tracks - stream only the specified tracks.
@type t() :: %ExMP4.Reader{
  compatible_brands: [binary()],
  creation_time: DateTime.t(),
  duration: non_neg_integer(),
  fragmented?: boolean(),
  location: integer(),
  major_brand: binary(),
  minor_version: integer(),
  modification_time: DateTime.t(),
  reader_mod: module(),
  reader_state: any(),
  timescale: non_neg_integer(),
  tracks: %{required(non_neg_integer()) => ExMP4.Track.t()}
}

Struct describing an MP4 reader.

Functions

@spec close(t()) :: :ok

Close the reader and free resources.

Link to this function

duration(reader, unit_or_timescale \\ :millisecond)

View Source
@spec duration(t(), ExMP4.Helper.timescale()) :: non_neg_integer()

Get the duration of the stream.

Link to this function

new(input, module \\ ExMP4.DataReader.File)

View Source
@spec new(any(), module()) :: {:ok, t()} | {:error, any()}

Create a new MP4 reader.

The input may be a file name, a binary {:binary, data} or any other input with an module implementing ExMP4.DataReader behaviour.

Link to this function

new!(filename, module \\ ExMP4.DataReader.File)

View Source
@spec new!(any(), module()) :: t()

The same as new/1, but raises if it fails.

Link to this function

read_sample(reader, track_id, sample_id)

View Source
@spec read_sample(t(), ExMP4.Track.id(), ExMP4.Sample.id()) :: ExMP4.Sample.t()

Read a sample from the specified track.

The first sample_id of any track starts at 0. The sample_count field of track provides the total number of samples on the track.

Retrieving samples by their id is slow since it scans all the metadata to get the specified sample, a better approach is to stream all the samples using stream/2.

Link to this function

samples(metadata_stream, reader)

View Source
@spec samples(Enumerable.t(), t()) :: Enumerable.t()

Get samples.

Link to this function

stream(reader, opts \\ [])

View Source
@spec stream(t(), stream_opts()) :: Enumerable.t()

Stream the samples' metadata.

The samples are retrieved ordered by their dts value.

@spec tracks(t()) :: [ExMP4.Track.t()]

Get all the available tracks.