Dala.Media.Subtitle (dala v0.2.0)

Copy Markdown View Source

Timestamp-synchronized subtitle rendering.

Supports SRT and WebVTT formats. Subtitles are rendered as GPU overlays synchronized with the media clock.

Architecture:

Subtitle File  Parser  Cue Timeline  Clock Sync  GPU Overlay

Example

{:ok, sub} = Dala.Media.Subtitle.load("subtitles.srt")

# In your screen's handle_info:
def handle_info({:clock, :tick, %{timestamp_us: ts}}, socket) do
  case Dala.Media.Subtitle.active_cue(sub, ts) do
    nil -> {:noreply, socket}
    cue -> {:noreply, render_subtitle(socket, cue)}
  end
end

Summary

Functions

Find the active cue for a given timestamp (in microseconds).

Get all cues that fall within a time range.

Parse SRT content into a list of cues.

Parse WebVTT content into a list of cues.

Format a cue for GPU overlay rendering.

Types

cue()

@type cue() :: %{
  id: non_neg_integer(),
  start_ms: non_neg_integer(),
  end_ms: non_neg_integer(),
  text: String.t(),
  style: map()
}

parse_result()

@type parse_result() :: {:ok, [cue()]} | {:error, term()}

Functions

active_cue(cues, timestamp_us)

@spec active_cue([cue()], non_neg_integer()) :: cue() | nil

Find the active cue for a given timestamp (in microseconds).

cues_in_range(cues, start_us, end_us)

@spec cues_in_range([cue()], non_neg_integer(), non_neg_integer()) :: [cue()]

Get all cues that fall within a time range.

parse_srt(content)

@spec parse_srt(String.t()) :: parse_result()

Parse SRT content into a list of cues.

parse_vtt(arg1)

@spec parse_vtt(String.t()) :: parse_result()

Parse WebVTT content into a list of cues.

to_overlay(cue, opts \\ [])

@spec to_overlay(
  cue(),
  keyword()
) :: map()

Format a cue for GPU overlay rendering.