Skip to content

Fast language loading optimization: LoadOptions.preferredAudioLanguage / preferredSubtitleLanguage — select a track by language during the load open #73

Closed

Description

What should the engine do?

Add preferredAudioLanguage: String? (and preferredSubtitleLanguage: String?) to LoadOptions. During the normal load open — where the engine already enumerates tracks and their languages (TrackInfo.language) — pick the first audio track whose language matches (BCP-47/ISO-639, case-insensitive, e.g. "en"/"eng"), falling back to the current default when there's no match. A language-based equivalent of the existing index-based audioSourceStreamIndex, resolved internally so the caller never needs the index up front.

Motivating media or use case

A host with a "prefer XXX language audio" setting wants the right track on first frame for any source — Jellyfin, direct CDN/.strm, local, SMB — without a metadata sidecar. Today the only ways to choose by language are to supply an index (audioSourceStreamIndex) or switch after load (selectAudioTrack, which restarts the pipeline). Getting the index means learning the track list before load — from server metadata (only when something like Jellyfin describes the file) or by running a separate full probe(url:) open first. On a remote source that pre-probe is an entire extra find_stream_info over the network (multi-second on a big remux), purely to discover an index the engine resolves moments later in its own load open. And since the engine remuxes a single audio track into the loopback HLS, AVPlayer's own language preference can't help — the choice must be made at the engine's load. A preferredAudioLanguage option collapses this to one open: no pre-probe, no second open, no restart, identical with or without a metadata server.

Suggested semantics: case-insensitive, accept en or eng vs TrackInfo.language; no match ⇒ keep default (don't fail load); explicit audioSourceStreamIndex wins over language; subtitle variant mirrors the existing native-subtitle path; resolved in the existing load open so zero extra round-trips.

Area

Public API surface

Host app / integration context

direct-URL/CDN, with a user preferred-audio-language toggle

Would you be willing to open a PR?

Maybe, with guidance

Activity

  1. ghost added
    enhancement
    on Jun 26, 2026
  2. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Both halves are now resolved, with the API split along the line you drew.

    Audio shipped in 4.3.0 as LoadOptions.preferredAudioLanguages (ordered; ISO 639-1 / 639-2 codes or English names). The engine resolves the first-frame audio track from its single internal probe: an explicit audioSourceStreamIndex still wins, else the first track matching a preference in order (case-insensitive, ISO 639-1/2 B+T and English-name synonyms), else the container default. No pre-probe, no second open, no selectAudioTrack reload. It is a plural ordered list rather than a single string, which is a superset of the preferredAudioLanguage you asked for.

    Subtitles ship now in 4.4.0 as LoadOptions.preferredSubtitleLanguages, mirroring the audio API. At the end of a successful load the engine activates the first subtitle track whose language matches a preference (same matching rules), publishes it via the new activeSubtitleTrackIndex, and anchors the side demuxer at the resume position. No match leaves subtitles off.

    One honest note on the asymmetry, since you framed both as one request: the subtitle half does not save a round-trip the way audio does. Audio must be resolved before the session starts, because the chosen track is muxed into the loopback HLS at the first frame, so a late pick really does force a pre-probe or a reload. Subtitles are rendered post-load by a side demuxer at no reload or pre-probe cost, so a host could already do this itself after reading subtitleTracks. preferredSubtitleLanguages is therefore convenience and API symmetry rather than a round-trip fix, but it does collapse both preferences to one uniform option resolved inside the load.

    var options = LoadOptions()
    options.preferredAudioLanguages = ["en", "de"]
    options.preferredSubtitleLanguages = ["en"]
    try await engine.load(url: url, options: options)
    // First frame plays English audio; English subtitles auto-activate.
    // engine.activeAudioTrackIndex / engine.activeSubtitleTrackIndex reflect the picks.
  3. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Follow-up: 4.5.0 sharpens the subtitle side.

    preferredSubtitleLanguages now activates the best track within the first matching language rather than the first one: full subtitles rank over SDH / forced / commentary (from container dispositions), text over bitmap, with preference order still dominating rank. So a "prefer English subtitles" host lands on the full English track even when a forced or SDH English track sorts earlier in the file.

    TrackInfo also gains isForced / isHearingImpaired / isCommentary (alongside the existing isDefault), read from FFmpeg dispositions for both audio and subtitle tracks, so you can rank or filter subtitleTracks yourself the same way. The pure subtitlePickRank and isBitmapSubtitleCodec helpers are public too.

    Release: https://github.com/superuser404notfound/AetherEngine/releases/tag/4.5.0

  4. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Both halves shipped: audio in 4.3.0 (preferredAudioLanguages, muxed at the first frame) and subtitle in 4.4.0 (preferredSubtitleLanguages), with disposition-ranked selection (full over SDH / forced / commentary) added in 4.5.0. Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions