Fix external subtitle stream mapping for multi-stream containers

Compute the in-file stream index for external subtitles instead of
hardcoding -map 1:0. For single-stream files (SRT/ASS/VTT) the index
is always 0, preserving existing behavior. For multi-stream containers
like MKS, the correct track is selected by counting sibling streams
that share the same Path.

Add unit tests for GetMapArgs covering internal subs, external SRT,
multiple external files, and multi-stream MKS containers.
This commit is contained in:
Piotr Niełacny
2026-03-24 11:02:10 +01:00
parent 2a689f268b
commit a15b426e73
2 changed files with 229 additions and 2 deletions

View File

@@ -3104,8 +3104,19 @@ namespace MediaBrowser.Controller.MediaEncoding
{
if (state.SubtitleStream.IsExternal)
{
// External subtitle file is added as second FFmpeg input
args += " -map 1:0";
// External subtitle file is added as second FFmpeg input.
// For single-stream files (SRT/ASS/VTT) the in-file index is always 0.
// For multi-stream containers (MKS) we count how many streams from
// the same file appear before the selected one.
var inFileIndex = state.MediaSource.MediaStreams
.Where(s => string.Equals(s.Path, state.SubtitleStream.Path, StringComparison.Ordinal))
.TakeWhile(s => s.Index != state.SubtitleStream.Index)
.Count();
args += string.Format(
CultureInfo.InvariantCulture,
" -map 1:{0}",
inFileIndex);
}
else
{