Skip to content

Update track selection logic for missing IDs#845

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

Update track selection logic for missing IDs#845
damontecres wants to merge 1 commit into
mainfrom
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.

@Jannik-M91
Copy link
Copy Markdown
Contributor

This is a common problem when ripping Blurays with MakeMKV. For empty subtitle tracks there will be a missing ID in the container (see Media Info below).

Up to now I started remuxing all my files using MKVToolNix, but if this change makes them properly playable that would be even better.

I will pull this into my local repo and test it with the files I have :)

Media Info

General
Format                                   : Matroska
Format version                           : Version 2
File size                                : 24.2 GiB
Duration                                 : 2 h 19 min
Overall bit rate mode                    : Variable
Overall bit rate                         : 24.8 Mb/s
Frame rate                               : 23.976 FPS
Encoded date                             : 2014-09-09 17:05:05 UTC
Writing application                      : MakeMKV 1.8.13 win(x64-release)
Writing library                          : libmakemkv 1.8.13 (1.3.0/1.4.1) win(x64-release)
Cover                                    : Yes
Cover description                        : cover.jpg
Cover type                               : Cover
Attachments                              : cover.jpg

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Format settings, Slice count             : 4 slices per frame
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 2 h 19 min
Bit rate mode                            : Variable
Maximum bit rate                         : 30.0 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 23.976 (24000/1001) FPS
Standard                                 : NTSC
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Language                                 : English
Default                                  : No
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709

Audio #1
ID                                       : 2
Format                                   : DTS XLL
Format/Info                              : Digital Theater Systems
Commercial name                          : DTS-HD Master Audio
Codec ID                                 : A_DTS
Duration                                 : 2 h 19 min
Bit rate mode                            : Variable
Channel(s)                               : 8 channels
Channel layout                           : C L R LFE Lb Rb Lss Rss
Sampling rate                            : 48.0 kHz
Frame rate                               : 93.750 FPS (512 SPF)
Bit depth                                : 16 bits
Compression mode                         : Lossless
Title                                    : Surround 7.1
Language                                 : German
Default                                  : Yes
Forced                                   : No

Audio #2
ID                                       : 3
Format                                   : DTS XLL
Format/Info                              : Digital Theater Systems
Commercial name                          : DTS-HD Master Audio
Codec ID                                 : A_DTS
Duration                                 : 2 h 19 min
Bit rate mode                            : Variable
Channel(s)                               : 8 channels
Channel layout                           : C L R LFE Lb Rb Lss Rss
Sampling rate                            : 48.0 kHz
Frame rate                               : 93.750 FPS (512 SPF)
Bit depth                                : 16 bits
Compression mode                         : Lossless
Title                                    : Surround 7.1
Language                                 : English
Default                                  : No
Forced                                   : No

Text #1
ID                                       : 4
Format                                   : PGS
Codec ID                                 : S_HDMV/PGS
Codec ID/Info                            : Picture based subtitle format used on BDs/HD-DVDs
Language                                 : German
Default                                  : No
Forced                                   : No

Text #2
ID                                       : 6
Format                                   : PGS
Codec ID                                 : S_HDMV/PGS
Codec ID/Info                            : Picture based subtitle format used on BDs/HD-DVDs
Language                                 : German
Default                                  : No
Forced                                   : No

Text #3
ID                                       : 8
Format                                   : PGS
Codec ID                                 : S_HDMV/PGS
Codec ID/Info                            : Picture based subtitle format used on BDs/HD-DVDs
Language                                 : German
Default                                  : No
Forced                                   : No

Text #4
ID                                       : 10
Format                                   : PGS
Codec ID                                 : S_HDMV/PGS
Codec ID/Info                            : Picture based subtitle format used on BDs/HD-DVDs
Language                                 : German
Default                                  : No
Forced                                   : No

Text #5
ID                                       : 12
Format                                   : PGS
Codec ID                                 : S_HDMV/PGS
Codec ID/Info                            : Picture based subtitle format used on BDs/HD-DVDs
Language                                 : German
Default                                  : No
Forced                                   : No

Image
Type                                     : Cover
Title                                    : cover.jpg
Format                                   : JPEG
Muxing mode                              : Attachment
Width                                    : 640 pixels
Height                                   : 360 pixels
Color space                              : YUV
Chroma subsampling                       : 4:4:4
Bit depth                                : 8 bits
Compression mode                         : Lossy
Stream size                              : 59.8 KiB (0%)

Menu
00:00:00.000                             : en:Chapter 01
00:08:13.242                             : en:Chapter 02
00:17:43.687                             : en:Chapter 03
00:25:37.744                             : en:Chapter 04
00:36:30.062                             : en:Chapter 05
00:45:09.790                             : en:Chapter 06
00:53:59.319                             : en:Chapter 07
01:02:25.783                             : en:Chapter 08
01:11:16.397                             : en:Chapter 09
01:20:08.261                             : en:Chapter 10
01:28:10.785                             : en:Chapter 11
01:37:52.533                             : en:Chapter 12
01:45:05.507                             : en:Chapter 13
01:55:02.770                             : en:Chapter 14
02:03:38.786                             : en:Chapter 15
02:12:16.845                             : en:Chapter 16

@damontecres
Copy link
Copy Markdown
Owner Author

Unfortunately, the assumption for how the tracks are ordered is not always true. So this PR was an interesting idea, but does not pan out.

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

3 participants