Add retry logic with timeout to extractLastFrame to handle temporary file access issues in shot scripts

This commit is contained in:
Towsty
2026-09-08 20:07:19 -05:00
parent 1e0cff9dea
commit da8d0e4c38
+15 -2
View File
@@ -70,6 +70,11 @@ export async function extractLastFrame(inputPath: string, outputPath: string) {
throw new Error('Source video is missing') throw new Error('Source video is missing')
} }
const maxTotalTimeMs = 5000
const start = Date.now()
let delayMs = 100
while (Date.now() - start < maxTotalTimeMs) {
const attempts: string[][] = [] const attempts: string[][] = []
try { try {
const duration = await probeDuration(inputPath) const duration = await probeDuration(inputPath)
@@ -88,11 +93,19 @@ export async function extractLastFrame(inputPath: string, outputPath: string) {
await runFfmpeg(args, 60000) await runFfmpeg(args, 60000)
if (existsSync(outputPath) && statSync(outputPath).size >= 64) return if (existsSync(outputPath) && statSync(outputPath).size >= 64) return
} catch { } catch {
// Try the next seek strategy. // Try next strategy
} }
} }
throw new Error('Could not extract the last frame. The video looks incomplete — interrupting Comfy mid-encode usually causes this. Earlier saved parts are still in the library.') const elapsed = Date.now() - start
if (elapsed >= maxTotalTimeMs) break
const wait = Math.min(delayMs, maxTotalTimeMs - elapsed)
await new Promise(resolve => setTimeout(resolve, wait))
delayMs = Math.min(delayMs * 2, maxTotalTimeMs - (Date.now() - start))
}
throw new Error('Could not extract the last frame within the timeout. The video may still be encoding or is incomplete.')
} }
export async function extractFirstFrame(inputPath: string, outputPath: string) { export async function extractFirstFrame(inputPath: string, outputPath: string) {