Ship extension queues, library extend, and smoother seam stitching.

Users can pre-queue extensions, extend from the library with p2 titles, blend seams with xfade, and a unreadable catalog is no longer replaced with an empty one.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-08-26 00:01:25 -05:00
co-authored by Cursor
parent 22d99f18db
commit f5c709924b
7 changed files with 653 additions and 79 deletions
+143 -4
View File
@@ -92,6 +92,132 @@ export async function probeHasAudio(filePath: string) {
}
}
const XFADE_DURATION = 0.12
async function probeDuration(filePath: string) {
const out = await runFfprobe([
'-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=nokey=1:noprint_wrappers=1',
filePath
])
const duration = Number(out)
if (!Number.isFinite(duration) || duration <= 0) {
throw new Error('Could not read video duration')
}
return duration
}
async function probeFps(filePath: string) {
const out = await runFfprobe([
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=avg_frame_rate,r_frame_rate',
'-of', 'default=nokey=1:noprint_wrappers=1',
filePath
])
const line = out.split(/\r?\n/).map(item => item.trim()).find(Boolean) || ''
const [num, den] = line.split('/').map(Number)
if (Number.isFinite(num) && Number.isFinite(den) && den > 0) return num / den
const fps = Number(line)
return Number.isFinite(fps) && fps > 0 ? fps : 24
}
async function probeSize(filePath: string) {
const out = await runFfprobe([
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height',
'-of', 'csv=p=0',
filePath
])
const [width, height] = out.split(',').map(Number)
return {
width: Number.isFinite(width) && width > 0 ? width : 1344,
height: Number.isFinite(height) && height > 0 ? height : 768
}
}
async function trimLeadingFrame(inputPath: string, outputPath: string) {
const fps = await probeFps(inputPath)
const hasAudio = await probeHasAudio(inputPath)
const args = [
'-y',
'-i', inputPath,
'-vf', 'trim=start_frame=1,setpts=PTS-STARTPTS'
]
if (hasAudio) {
args.push('-af', `atrim=start=${(1 / fps).toFixed(6)},asetpts=PTS-STARTPTS`, '-c:a', 'aac')
} else {
args.push('-an')
}
args.push(
'-c:v', 'libx264',
'-preset', 'fast',
'-crf', '17',
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart',
outputPath
)
await runFfmpeg(args, 180000)
if (!existsSync(outputPath)) {
throw new Error('FFmpeg did not write the trimmed extension segment')
}
}
async function xfadeStitch(part1Path: string, part2Path: string, outputPath: string) {
const [duration1, duration2, fps, size, audio1, audio2] = await Promise.all([
probeDuration(part1Path),
probeDuration(part2Path),
probeFps(part1Path),
probeSize(part1Path),
probeHasAudio(part1Path),
probeHasAudio(part2Path)
])
const fade = Math.min(
XFADE_DURATION,
Math.max(0.04, duration1 - 0.05),
Math.max(0.04, duration2 - 0.05)
)
const offset = Math.max(0, Number((duration1 - fade).toFixed(3)))
const fpsLabel = Number.isInteger(fps) ? String(fps) : fps.toFixed(3)
const prepare = `fps=${fpsLabel},scale=${size.width}:${size.height}:flags=bicubic,format=yuv420p,setpts=PTS-STARTPTS`
const videoFilter = `[0:v]${prepare}[v0];[1:v]${prepare}[v1];[v0][v1]xfade=transition=fade:duration=${fade.toFixed(3)}:offset=${offset},setpts=PTS-STARTPTS[v]`
const encode = [
'-c:v', 'libx264',
'-preset', 'fast',
'-crf', '17',
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart'
]
if (audio1 && audio2) {
await runFfmpeg([
'-y',
'-i', part1Path,
'-i', part2Path,
'-filter_complex', `${videoFilter};[0:a]asetpts=PTS-STARTPTS[a0];[1:a]asetpts=PTS-STARTPTS[a1];[a0][a1]acrossfade=d=${fade.toFixed(3)}:c1=tri:c2=tri[a]`,
'-map', '[v]',
'-map', '[a]',
...encode,
'-c:a', 'aac',
outputPath
], 300000)
} else {
await runFfmpeg([
'-y',
'-i', part1Path,
'-i', part2Path,
'-filter_complex', videoFilter,
'-map', '[v]',
...encode,
outputPath
], 300000)
}
if (!existsSync(outputPath)) {
throw new Error('FFmpeg did not write the blended video')
}
}
export async function concatMp4(part1Path: string, part2Path: string, outputPath: string) {
const listPath = join(dirname(outputPath), 'concat_list.txt')
writeFileSync(listPath, `file '${concatPath(part1Path)}'\nfile '${concatPath(part2Path)}'\n`)
@@ -111,7 +237,7 @@ export async function concatMp4(part1Path: string, part2Path: string, outputPath
'-map', '[a]',
'-c:v', 'libx264',
'-preset', 'fast',
'-crf', '18',
'-crf', '17',
'-pix_fmt', 'yuv420p',
'-c:a', 'aac',
'-movflags', '+faststart',
@@ -126,7 +252,7 @@ export async function concatMp4(part1Path: string, part2Path: string, outputPath
'-map', '[v]',
'-c:v', 'libx264',
'-preset', 'fast',
'-crf', '18',
'-crf', '17',
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart',
outputPath
@@ -145,11 +271,24 @@ export async function concatMp4(part1Path: string, part2Path: string, outputPath
export async function stitchExtension(params: { part1Path: string; part2: Buffer; tmpDir: string }) {
const part2Path = join(params.tmpDir, 'part2.mp4')
const trimmedPath = join(params.tmpDir, 'part2_trim.mp4')
const finalPath = join(params.tmpDir, 'final_stitched_video.mp4')
writeFileSync(part2Path, params.part2)
await concatMp4(params.part1Path, part2Path, finalPath)
try {
await trimLeadingFrame(part2Path, trimmedPath)
await xfadeStitch(params.part1Path, trimmedPath, finalPath)
} catch (blendError) {
try {
const trimmedReady = existsSync(trimmedPath)
await concatMp4(params.part1Path, trimmedReady ? trimmedPath : part2Path, finalPath)
} catch (concatError) {
const blendMessage = blendError instanceof Error ? blendError.message : String(blendError)
const concatMessage = concatError instanceof Error ? concatError.message : String(concatError)
throw new Error(`Seam blend failed (${blendMessage}). Concat fallback also failed (${concatMessage}).`)
}
}
const stitched = readFileSync(finalPath)
for (const extra of [part2Path, join(params.tmpDir, 'concat_list.txt'), join(params.tmpDir, 'last_frame.png')]) {
for (const extra of [part2Path, trimmedPath, join(params.tmpDir, 'concat_list.txt'), join(params.tmpDir, 'last_frame.png')]) {
try { unlinkSync(extra) } catch { /* ignore */ }
}
return stitched