HLX.Writer (HLS Reader and Writer v0.3.0)

View Source

Module for writing HLS master and media playlists.

Summary

Functions

Adds a discontinuity to the playlist.

Add a new rendition to the master playlist.

Same as add_rendition/3, but raises an error if the operation fails.

Add a new variant to the master playlist.

Same as add_variant/3, but raises an error if the operation fails.

Closes the writer.

Creates a new HLS writer.

Same as new/1, but raises an error if the operation fails.

Writes a sample to the specified variant or rendition.

Types

mode()

@type mode() :: :live | :vod

rendition_opts()

@type rendition_opts() :: [
  type: :audio,
  track: HLX.Track.t(),
  group_id: String.t(),
  default: boolean(),
  auto_select: boolean()
]

segment_type()

@type segment_type() :: :mpeg_ts | :fmp4 | :low_latency

t()

@opaque t()

tracks()

@type tracks() :: [HLX.Track.t()]

variant_opts()

@type variant_opts() :: [tracks: [HLX.Track.t()], audio: String.t()]

Functions

add_discontinuity(writer, variant_id \\ nil)

@spec add_discontinuity(t(), String.t() | nil) :: t()

Adds a discontinuity to the playlist.

This flushes any pending segments, so make sure all the samples are written before and adds a discontinuity to the specified variant or to all variants if no variant_id is provided.

add_rendition(writer, name, opts)

@spec add_rendition(t(), String.t(), rendition_opts()) :: {:ok, t()} | {:error, any()}

Add a new rendition to the master playlist.

This adds a new EXT-X-MEDIA entry to the master playlist.

The name should be unique across variants and renditions and it's used as the name of the playlist (name.m3u8).

The following parameter may be provided:

  • type - [Required] The type should always be :audio since it's the only supported media type.
  • track - [Required] The track that defines the media.
  • group_id - [Required] The group id of the rendition.
  • default - A boolean indicating if this is the default rendition.
  • auto_select - A boolean setting the auto select.

add_rendition!(writer, name, opts)

@spec add_rendition!(t(), String.t(), rendition_opts()) :: t()

Same as add_rendition/3, but raises an error if the operation fails.

add_variant(writer, name, options)

@spec add_variant(t(), String.t(), variant_opts()) :: {:ok, t()} | {:error, any()}

Add a new variant to the master playlist.

This adds a new EXT-X-STREAM-INF entry to the master playlist.

The name should be unique across variants and renditions and it's used as the name of the playlist (<name>.m3u8).

The following parameter may be provided:

  • tracks - [Required] One or more tracks definitions, all the media are muxed in the same segment.
  • audio - Reference to a group_id of a rendition.

add_variant!(writer, name, options)

@spec add_variant!(t(), String.t(), variant_opts()) :: t()

Same as add_variant/3, but raises an error if the operation fails.

close(writer)

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

Closes the writer.

Closes the writer and flush any pending segments. if the mode is vod creates the final playlists.

new(options)

@spec new(Keyword.t()) :: {:ok, t()}

Creates a new HLS writer.

The following options can be provided:

  • type - The type of the playlist, either :master or :media. Defaults to :media.
  • mode - The mode of the playlist, either :live or :vod. Defaults to :live.
  • segment_type - The type of segments to generate, either :mpeg_ts, :fmp4 or :low_latency. Defaults to :fmp4.
  • max_segments - The maximum number of segments to keep in the playlist, ignored on vod mode. Defaults to 0 (no limit).
  • storage_dir - The directory where to store the playlists and segments. This is required.
  • segment_duration - The target duration of each segment in milliseconds. Defaults to 2000.
  • part_duration - The target duration of each part in milliseconds (only for low-latency segments). Defaults to 300.

You can provide callbacks for segment and part creation by adding the following options:

  • on_segment_created - A 2-arity function that will be called when a new segment is created. The fuction receives two arguments: the variant id and the segment info.
  • on_part_created - A 2-arity function that will be called when a new part is created. The fuction receives two arguments: the variant id and the part info.

new!(options)

@spec new!(Keyword.t()) :: t()

Same as new/1, but raises an error if the operation fails.

write_sample(writer, variant_id, sample)

@spec write_sample(t(), String.t(), HLX.Sample.t()) :: t()

Writes a sample to the specified variant or rendition.