Extract chapters from the description if they are missing (#3167)

This commit is contained in:
absidue 2023-02-08 19:44:35 +01:00 committed by GitHub
parent bd6f143918
commit e197f9871a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 30 deletions

View File

@ -391,7 +391,7 @@ export default defineComponent({
this.channelSubscriptionCountText = ''
}
const chapters = []
let chapters = []
if (!this.hideChapters) {
const rawChapters = result.player_overlays?.decorated_player_bar?.player_bar?.markers_map?.get({ marker_key: 'DESCRIPTION_CHAPTERS' })?.value.chapters
if (rawChapters) {
@ -406,7 +406,11 @@ export default defineComponent({
thumbnail: chapter.thumbnail[0].url
})
}
} else {
chapters = this.extractChaptersFromDescription(this.videoDescription)
}
if (chapters.length > 0) {
this.addChaptersEndSeconds(chapters, result.basic_info.duration)
// prevent vue from adding reactivity which isn't needed
@ -735,35 +739,9 @@ export default defineComponent({
break
}
const chapters = []
let chapters = []
if (!this.hideChapters) {
// HH:MM:SS Text
// MM:SS Text
// HH:MM:SS - Text // separator is one of '-', '', '•', '—'
// MM:SS - Text
// HH:MM:SS - HH:MM:SS - Text // end timestamp is ignored, separator is one of '-', '', '—'
// HH:MM - HH:MM - Text // end timestamp is ignored
const chapterMatches = result.description.matchAll(/^(?<timestamp>((?<hours>\d+):)?(?<minutes>\d+):(?<seconds>\d+))(\s*[–—-]\s*(?:\d+:){1,2}\d+)?\s+([–—•-]\s*)?(?<title>.+)$/gm)
for (const { groups } of chapterMatches) {
let start = 60 * Number(groups.minutes) + Number(groups.seconds)
if (groups.hours) {
start += 3600 * Number(groups.hours)
}
// replace previous chapter with current one if they have an identical start time
if (chapters.length > 0 && chapters[chapters.length - 1].startSeconds === start) {
chapters.pop()
}
chapters.push({
title: groups.title.trim(),
timestamp: groups.timestamp,
startSeconds: start,
endSeconds: 0
})
}
chapters = this.extractChaptersFromDescription(result.description)
if (chapters.length > 0) {
this.addChaptersEndSeconds(chapters, result.lengthSeconds)
@ -883,6 +861,42 @@ export default defineComponent({
})
},
/**
* @param {string} description
*/
extractChaptersFromDescription: function (description) {
const chapters = []
// HH:MM:SS Text
// MM:SS Text
// HH:MM:SS - Text // separator is one of '-', '', '•', '—'
// MM:SS - Text
// HH:MM:SS - HH:MM:SS - Text // end timestamp is ignored, separator is one of '-', '', '—'
// HH:MM - HH:MM - Text // end timestamp is ignored
const chapterMatches = description.matchAll(/^(?<timestamp>((?<hours>\d+):)?(?<minutes>\d+):(?<seconds>\d+))(\s*[–—-]\s*(?:\d+:){1,2}\d+)?\s+([–—•-]\s*)?(?<title>.+)$/gm)
for (const { groups } of chapterMatches) {
let start = 60 * Number(groups.minutes) + Number(groups.seconds)
if (groups.hours) {
start += 3600 * Number(groups.hours)
}
// replace previous chapter with current one if they have an identical start time
if (chapters.length > 0 && chapters[chapters.length - 1].startSeconds === start) {
chapters.pop()
}
chapters.push({
title: groups.title.trim(),
timestamp: groups.timestamp,
startSeconds: start,
endSeconds: 0
})
}
return chapters
},
addChaptersEndSeconds: function (chapters, videoLengthSeconds) {
for (let i = 0; i < chapters.length - 1; i++) {
chapters[i].endSeconds = chapters[i + 1].startSeconds

View File

@ -122,7 +122,7 @@
/>
<watch-video-chapters
v-if="!hideChapters && !isLoading && videoChapters.length > 0"
:compact="backendPreference === 'invidious'"
:compact="!videoChapters[0].thumbnail"
:chapters="videoChapters"
:current-chapter-index="videoCurrentChapterIndex"
class="watchVideo"