From 94756458b1e86bb50a44be5586c8264a493bf312 Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Sun, 19 Sep 2021 22:12:14 -0400 Subject: [PATCH] feat: Optimize caption sort order (#1661) * choose best caption * add comments * fix linter issue --- .../ft-video-player/ft-video-player.js | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/renderer/components/ft-video-player/ft-video-player.js b/src/renderer/components/ft-video-player/ft-video-player.js index 0dff6398..da990e87 100644 --- a/src/renderer/components/ft-video-player/ft-video-player.js +++ b/src/renderer/components/ft-video-player/ft-video-player.js @@ -140,6 +140,10 @@ export default Vue.extend({ return this.$store.getters.getUsingElectron }, + currentLocale: function () { + return this.$store.getters.getCurrentLocale + }, + defaultPlayback: function () { return this.$store.getters.getDefaultPlayback }, @@ -1100,6 +1104,47 @@ export default Vue.extend({ this.determineDefaultQualityDash() }, + sortCaptions: function (captionList) { + return captionList.sort((captionA, captionB) => { + const aCode = captionA.languageCode.split('-') // ex. [en,US] + const bCode = captionB.languageCode.split('-') + const aName = (captionA.label || captionA.name.simpleText) // ex: english (auto-generated) + const bName = (captionB.label || captionB.name.simpleText) + const userLocale = this.currentLocale.split(/-|_/) // ex. [en,US] + if (aCode[0] === userLocale[0]) { // caption a has same language as user's locale + if (bCode[0] === userLocale[0]) { // caption b has same language as user's locale + if (bName.search('auto') !== -1) { + // prefer caption a: b is auto-generated captions + return -1 + } else if (aName.search('auto') !== -1) { + // prefer caption b: a is auto-generated captions + return 1 + } else if (aCode[1] === userLocale[1]) { + // prefer caption a: caption a has same county code as user's locale + return -1 + } else if (bCode[1] === userLocale[1]) { + // prefer caption b: caption b has same county code as user's locale + return 1 + } else if (aCode[1] === undefined) { + // prefer caption a: no country code is better than wrong country code + return -1 + } else if (bCode[1] === undefined) { + // prefer caption b: no country code is better than wrong country code + return 1 + } + } else { + // prefer caption a: b does not match user's language + return -1 + } + } else if (bCode[0] === userLocale[0]) { + // prefer caption b: a does not match user's language + return 1 + } + // sort alphabetically + return aName.localeCompare(bName) + }) + }, + transformAndInsertCaptions: async function() { let captionList if (this.captionHybridList[0] instanceof Promise) { @@ -1109,7 +1154,7 @@ export default Vue.extend({ captionList = this.captionHybridList } - for (const caption of captionList) { + for (const caption of this.sortCaptions(captionList)) { this.player.addRemoteTextTrack({ kind: 'subtitles', src: caption.baseUrl || caption.url,