refactor mpv state mapping into mpv-state helper

This commit is contained in:
2026-02-14 15:19:37 -08:00
parent c968cf9f69
commit 4ae048bab5
3 changed files with 66 additions and 15 deletions

View File

@@ -0,0 +1,25 @@
export type MpvTrackProperty = {
type?: string;
id?: number;
selected?: boolean;
"ff-index"?: number;
};
export function resolveCurrentAudioStreamIndex(
tracks: Array<MpvTrackProperty> | null | undefined,
currentAudioTrackId: number | null,
): number | null {
if (!Array.isArray(tracks)) {
return null;
}
const audioTracks = tracks.filter((track) => track.type === "audio");
const activeTrack =
audioTracks.find((track) => track.id === currentAudioTrackId) ||
audioTracks.find((track) => track.selected === true);
const ffIndex = activeTrack?.["ff-index"];
return typeof ffIndex === "number" && Number.isInteger(ffIndex) && ffIndex >= 0
? ffIndex
: null;
}