Bilibili-Evolved/registry/lib/components/video/metadata/utils.ts

74 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { EpisodeInfo } from '@/components/video/video-info'
import { Tag, ViewPoint } from './types'
export function escape(x: any) {
return lodash.toString(x).replace(/[=;#\\\n]/g, r => `\\${r}`)
}
export function tagWithId(tags: Tag[]) {
return tags.map(x => `${x.tag_name}(${x.tag_id})`)
}
export function fixBgmTag(bgmTags: Tag[]) {
return bgmTags.map(
x => `${x.tag_name.match(/^发现《([^》]+)》/)?.[1] ?? x.tag_name}(${x.music_id})`,
)
}
export function bangumiSkipToViewPoints(skip: EpisodeInfo['skip'], duration: number) {
const p: ViewPoint[] = []
const { op, ed } = skip
const hasOp = op.start >= 0 && op.end > 0
const hasEd = ed.start > op.end && ed.end <= duration
if (hasOp && op.start > 0) {
// OP前的部分
p.push({ from: 0, to: op.start, content: '' })
}
if (hasOp) {
p.push({ from: op.start, to: op.end, content: 'Opening Theme' })
}
// 正片
if (hasOp && hasEd) {
// OP+ED
p.push({ from: op.end, to: ed.start, content: '' })
} else if (hasOp && !hasEd) {
// 仅OP
p.push({ from: op.end, to: duration, content: '' })
} else if (!hasOp && hasEd) {
// 仅ED
p.push({ from: 0, to: ed.start, content: '' })
} else {
// 没有OP和ED不分段
// viewPotins.push({ from: 0, to: duration, content: '' })
}
if (hasEd) {
p.push({ from: ed.start, to: ed.end, content: 'Ending Theme' })
}
if (hasEd && ed.end < duration) {
// ED后的部分
p.push({ from: ed.end, to: duration, content: '' })
}
return p
}
export function formatTime(date: Date, format: TimeFormat) {
switch (format) {
case TimeFormat.Timestmp:
return date.getTime()
case TimeFormat.Local:
return date.toLocaleString()
case TimeFormat.IOS:
return date.toISOString()
default:
break
}
return 0
}