Preserve extension quality with original segments and reliable refinement

This commit is contained in:
Towsty
2026-09-08 06:25:40 -05:00
parent c2b6032e49
commit 7665285a0d
16 changed files with 222 additions and 98 deletions
+23
View File
@@ -0,0 +1,23 @@
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)
}