24 lines
1.0 KiB
TypeScript
24 lines
1.0 KiB
TypeScript
import { existsSync, mkdirSync, readdirSync, linkSync, copyFileSync, writeFileSync } from 'node:fs'
|
|
import { dirname, join } from 'node:path'
|
|
|
|
/** Each saved clip owns its sources, so deleting a parent cannot break extensions. */
|
|
export function videoSourcePaths(videoPath: string) {
|
|
const dir = join(dirname(videoPath), 'sources')
|
|
if (existsSync(dir)) {
|
|
const paths = readdirSync(dir).filter(name => /^\d{6}\.mp4$/.test(name)).sort().map(name => join(dir, name))
|
|
if (paths.length) return paths
|
|
}
|
|
// Legacy clips become a fixed baseline; their past compression is irreversible.
|
|
return [videoPath]
|
|
}
|
|
|
|
export function preserveVideoSources(videoPath: string, parents: string[], segment: Buffer) {
|
|
const dir = join(dirname(videoPath), 'sources')
|
|
mkdirSync(dir, { recursive: true })
|
|
parents.forEach((source, index) => {
|
|
const dest = join(dir, `${String(index).padStart(6, '0')}.mp4`)
|
|
try { linkSync(source, dest) } catch { copyFileSync(source, dest) }
|
|
})
|
|
writeFileSync(join(dir, `${String(parents.length).padStart(6, '0')}.mp4`), segment)
|
|
}
|