View Source ExMP4.Reader (MP4 Reader and Writer v0.2.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.
  • major_brand
  • major_brand_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

Functions

Close the reader and free resources.

Get the duration of the stream.

Create a new reader from an mp4 file.

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()],
  duration: non_neg_integer(),
  major_brand: binary(),
  major_brand_version: integer(),
  reader_mod: module(),
  reader_state: any(),
  timescale: non_neg_integer(),
  tracks: %{required(non_neg_integer()) => ExMP4.Track.t()}
}

Struct describing

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.

@spec new(Path.t()) :: {:ok, t()} | {:error, any()}

Create a new reader from an mp4 file.

@spec new!(Path.t()) :: 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.