From 7424292435abe3a1afcadbc37d2855f78d73d611 Mon Sep 17 00:00:00 2001 From: Vitaly Gashkov Date: Wed, 20 Nov 2024 22:41:55 +0500 Subject: [PATCH] fix: improve audio filter when no language is selected --- lib/track.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/track.js b/lib/track.js index 2bfa3be..587cb73 100644 --- a/lib/track.js +++ b/lib/track.js @@ -46,9 +46,15 @@ const createAudioCodecFilter = (audios) => createCodecFilter(audios); const createAudioLanguageFilter = (audios) => { return (languages = [], maxTracksPerLanguage) => { if (!languages.length) { - for (const audio of audios) { - const alreadyAdded = languages.includes(audio.language); - if (!alreadyAdded) languages.push(audio.language); + const audiosWithLanguage = audios.filter((track) => track.language); + if (audiosWithLanguage.length) { + for (const audio of audiosWithLanguage) { + const alreadyAdded = languages.includes(audio.language); + if (!alreadyAdded) languages.push(audio.language); + } + } else { + audios.sort((a, b) => b.bitrate.bps - a.bitrate.bps); + return audios.slice(0, maxTracksPerLanguage); } } const filtered = [];