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:
parent
7113f32a87
commit
77b70702d2
101 changed files with 1957 additions and 158 deletions
|
@ -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
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
32
packages/core-utils/src/string/chapters.ts
Normal file
32
packages/core-utils/src/string/chapters.ts
Normal 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
|
||||
}
|
1
packages/core-utils/src/string/index.ts
Normal file
1
packages/core-utils/src/string/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './chapters.js'
|
Loading…
Add table
Add a link
Reference in a new issue