1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Add video chapters support

This commit is contained in:
Chocobozzz 2023-08-28 10:55:04 +02:00
parent 7113f32a87
commit 77b70702d2
No known key found for this signature in database
GPG key ID: 583A612D890159BE
101 changed files with 1957 additions and 158 deletions

View file

@ -1,4 +1,4 @@
function findCommonElement <T> (array1: T[], array2: T[]) {
export function findCommonElement <T> (array1: T[], array2: T[]) {
for (const a of array1) {
for (const b of array2) {
if (a === b) return a
@ -9,19 +9,19 @@ function findCommonElement <T> (array1: T[], array2: T[]) {
}
// Avoid conflict with other toArray() functions
function arrayify <T> (element: T | T[]) {
export function arrayify <T> (element: T | T[]) {
if (Array.isArray(element)) return element
return [ element ]
}
// Avoid conflict with other uniq() functions
function uniqify <T> (elements: T[]) {
export function uniqify <T> (elements: T[]) {
return Array.from(new Set(elements))
}
// Thanks: https://stackoverflow.com/a/12646864
function shuffle <T> (elements: T[]) {
export function shuffle <T> (elements: T[]) {
const shuffled = [ ...elements ]
for (let i = shuffled.length - 1; i > 0; i--) {
@ -33,9 +33,13 @@ function shuffle <T> (elements: T[]) {
return shuffled
}
export {
uniqify,
findCommonElement,
shuffle,
arrayify
export function sortBy (obj: any[], key1: string, key2?: string) {
return obj.sort((a, b) => {
const elem1 = key2 ? a[key1][key2] : a[key1]
const elem2 = key2 ? b[key1][key2] : b[key1]
if (elem1 < elem2) return -1
if (elem1 === elem2) return 0
return 1
})
}

View file

@ -45,11 +45,13 @@ function isLastWeek (d: Date) {
// ---------------------------------------------------------------------------
export const timecodeRegexString = `((\\d+)[h:])?((\\d+)[m:])?((\\d+)s?)?`
function timeToInt (time: number | string) {
if (!time) return 0
if (typeof time === 'number') return time
const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
const reg = new RegExp(`^${timecodeRegexString}$`)
const matches = time.match(reg)
if (!matches) return 0

View file

@ -5,3 +5,4 @@ export * from './plugins/index.js'
export * from './renderer/index.js'
export * from './users/index.js'
export * from './videos/index.js'
export * from './string/index.js'

View file

@ -0,0 +1,32 @@
import { timeToInt, timecodeRegexString } from '../common/date.js'
const timecodeRegex = new RegExp(`^(${timecodeRegexString})\\s`)
export function parseChapters (text: string) {
if (!text) return []
const lines = text.split(/\r?\n|\r|\n/g)
let foundChapters = false
const chapters: { timecode: number, title: string }[] = []
for (const line of lines) {
const matched = line.match(timecodeRegex)
if (!matched) {
// Stop chapters parsing
if (foundChapters) break
continue
}
foundChapters = true
const timecodeText = matched[1]
const timecode = timeToInt(timecodeText)
const title = line.replace(matched[0], '')
chapters.push({ timecode, title })
}
return chapters
}

View file

@ -0,0 +1 @@
export * from './chapters.js'