Skip to content

Update track selection logic for missing IDs#845

Draft
damontecres wants to merge 1 commit intomainfrom
fix/track-selection-missing-ids
Draft

Update track selection logic for missing IDs#845
damontecres wants to merge 1 commit intomainfrom
fix/track-selection-missing-ids

Conversation

@damontecres
Copy link
Copy Markdown
Owner

Description

This is a major rewrite of the audio/subtitle track selection logic.

Instead of trying to match the server's index to a track's ID, the implementation now calculates the desired index's offset and uses that to select a track.

This means there is the assumption that that the indexes for each MediaStream in the MediaSourceInfo will be ordered as:

  1. External subtitles
  2. Video streams
  3. Audio streams
  4. Embedded subtitles

Note: this assumption is not new, the previous code assumed this as well, but wasn't as upfront about it.

Thus for a file with 1 external subtitle, 1 video, 2 audio, & 3 embedded subtitles and you want the second audo track (index=3):

  1. Count everything before the first audio (2)
  2. Subtract that from the desired index: 3-2=1
  3. Find the 1 indexed audio track in the actual file, ie the second audio track

Related issues

Maybes fixes #791

Testing

Existing unit tests pass. Added additional test cases for a missing ID

I also crafted an mp4 and rewrote the track IDs so there was a gap in the IDs (none with ID=2). Before this PR, Wholphin was unable to switch audio tracks (the official app failed as well). With these change, it can.

I was unable to craft a similar mkv that played successfully and I believe this is because the mkv spec doesn't allow for missing IDs. This is why I'm unsure if #791 is fully resolved by this PR since the OP's issue occurs with a mkv.

Media info
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 1 013 MiB
Duration                                 : 1 h 6 min
Overall bit rate                         : 2 134 kb/s
Frame rate                               : 29.970 FPS
Writing application                      : Lavf62.3.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L3
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 1 h 6 min
Bit rate                                 : 1 656 kb/s
Width                                    : 720 pixels
Height                                   : 480 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 29.970 FPS
Minimum frame rate                       : 18.182 FPS
Maximum frame rate                       : 83.333 FPS
Standard                                 : NTSC
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.160
Stream size                              : 786 MiB (78%)
Writing library                          : x264 core 148
Encoding settings                        : cabac=1 / ref=2 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=6 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=30 / rc=crf / mbtree=1 / crf=1.0 / qcomp=0.60 / qpmin=0 / qpmax=34 / qpstep=4 / vbv_maxrate=1666 / vbv_bufsize=3332 / crf_max=25.0 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00
Language                                 : English
Tagged date                              : 2026-02-07 20:08:08 UTC
Menus                                    : 5
mdhd_Duration                            : 3981065
Codec configuration box                  : avcC

Audio #1
ID                                       : 3
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 1 h 6 min
Bit rate mode                            : Constant
Bit rate                                 : 341 kb/s
Channel(s)                               : 6 channels
Channel layout                           : C L R Ls Rs LFE
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 162 MiB (16%)
Title                                    : Surround 5.1
Language                                 : English
Default                                  : Yes
Alternate group                          : 1
Tagged date                              : 2026-02-07 20:08:08 UTC
Menus                                    : 5

Audio #2
ID                                       : 4
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 1 h 6 min
Bit rate mode                            : Constant
Bit rate                                 : 132 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 61.7 MiB (6%)
Title                                    : Stereo
Language                                 : Spanish
Default                                  : No
Alternate group                          : 1
Tagged date                              : 2026-02-07 20:08:08 UTC
Menus                                    : 5

Screenshots

N/A

AI or LLM usage

None

@damontecres damontecres added bug Something isn't working playback Related to media playback labels Feb 8, 2026
@DrHurt
Copy link
Copy Markdown

DrHurt commented Feb 10, 2026

Looking forward to testing this. Hope it makes it into next build.

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

Labels

bug Something isn't working playback Related to media playback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] - Loading subtitles changes audio track language and locks it until the video is stopped completely

2 participants