feat: Optimize caption sort order (#1661)

* choose best caption

* add comments

* fix linter issue
This commit is contained in:
ChunkyProgrammer 2021-09-19 22:12:14 -04:00 committed by GitHub
parent 439521893f
commit 94756458b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 1 deletions

View File

@ -140,6 +140,10 @@ export default Vue.extend({
return this.$store.getters.getUsingElectron return this.$store.getters.getUsingElectron
}, },
currentLocale: function () {
return this.$store.getters.getCurrentLocale
},
defaultPlayback: function () { defaultPlayback: function () {
return this.$store.getters.getDefaultPlayback return this.$store.getters.getDefaultPlayback
}, },
@ -1100,6 +1104,47 @@ export default Vue.extend({
this.determineDefaultQualityDash() 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() { transformAndInsertCaptions: async function() {
let captionList let captionList
if (this.captionHybridList[0] instanceof Promise) { if (this.captionHybridList[0] instanceof Promise) {
@ -1109,7 +1154,7 @@ export default Vue.extend({
captionList = this.captionHybridList captionList = this.captionHybridList
} }
for (const caption of captionList) { for (const caption of this.sortCaptions(captionList)) {
this.player.addRemoteTextTrack({ this.player.addRemoteTextTrack({
kind: 'subtitles', kind: 'subtitles',
src: caption.baseUrl || caption.url, src: caption.baseUrl || caption.url,