ExMP4.FWriter (MP4 Reader and Writer v0.10.0)

View Source

Module responsible for writing fragmented MP4.

Summary

Types

Options to supply when creating the writer.

t()

Functions

Close the writer.

Create a new empty fragment.

Creates a new media segment.

Finalizes the current fragment and segment (if any).

Get a track by id or type.

Write a sample to the current fragment.

Types

new_opts()

@type new_opts() :: [
  major_brand: binary(),
  compatible_brands: [binary()],
  minor_version: integer(),
  creation_time: DateTime.t(),
  modification_time: DateTime.t(),
  duration: integer() | boolean(),
  moof_base_offset: boolean()
]

Options to supply when creating the writer.

t()

@type t() :: %ExMP4.FWriter{
  base_data_offset: integer(),
  current_fragments: %{required(integer()) => {ExMP4.Box.Traf.t(), [iodata()]}},
  current_segments: %{required(integer()) => ExMP4.Box.Sidx.t()},
  ftyp_box_size: integer(),
  moof_base_offset: boolean(),
  movie_box: ExMP4.Box.Moov.t() | nil,
  sequence_number: integer(),
  tracks: %{required(integer()) => ExMP4.Track.t()},
  writer_mod: module(),
  writer_state: term()
}

writer_options()

@type writer_options() :: any()

Functions

close(writer)

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

Close the writer.

create_fragment(writer)

@spec create_fragment(t()) :: t()

Create a new empty fragment.

After adding samples, the fragment should be finalized, with flush_fragment/1.

create_segment(writer)

@spec create_segment(t()) :: t()

Creates a new media segment.

Calling this function is optional. If not called, no Segment Index Box (sidx) will be added.

Segments and Fragments

Current implementation restricts each segment to contain exactly one fragment. This is why there is no separate flush_segment function - the segment is automatically closed when the single fragment is completed.

flush_fragment(writer)

@spec flush_fragment(t()) :: t()

Finalizes the current fragment and segment (if any).

new(writer_opts, tracks, opts \\ [], module \\ ExMP4.FragDataWriter.File)

@spec new(writer_options(), [ExMP4.Track.t()], new_opts(), module()) ::
  {:ok, t()} | {:error, term()}

Create a new fragmented mp4 writer.

The tracks are assigned an id starting from 1.

The following options can be provided:

  • major_brand - Set the major brand

  • compatible_brands - Set the compatible brands

  • creation_time - Set the creation time

  • modification_time - Set the modification time

  • duration - Set the total duration if known. The value can be true, false or an integer.

    If true, the total duration of the presentation is calculated when closing the writer and the mehd box is set to include the fragment duration. Note that this needs the output target to support seeking (not suitable for live streaming.)

    If false, the total duration is not calculated and the mehd box is not included. This is suitable for real time or for presentations where the total duration is not available.

    If an integer, it's the total duration in the movie timescale and it'll be set in the mehd box.

  • moof_base_offset - if true, it indicates that the base‐data‐offset for the track fragments is the position of the first byte of the enclosing Movie Fragment Box. Defaults to: false.

The last argument is an optional module implementing ExMP4.FragDataWriter.

new!(writer_opts, tracks, opts \\ [], module \\ ExMP4.FragDataWriter.File)

@spec new!(writer_options(), [ExMP4.Track.t()], new_opts(), module()) :: t()

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

track(writer, id_or_type)

@spec track(t(), integer() | atom()) :: ExMP4.Track.t() | nil

Get a track by id or type.

write_sample(writer, sample)

@spec write_sample(t(), ExMP4.Sample.t()) :: t()

Write a sample to the current fragment.