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 = [];