|
28 | 28 | * `success: false`. |
29 | 29 | */ |
30 | 30 |
|
31 | | -import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from "node:fs"; |
| 31 | +import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs"; |
32 | 32 | import { dirname, join } from "node:path"; |
33 | 33 | import { |
34 | 34 | encodeFramesChunkedConcat, |
@@ -148,44 +148,49 @@ async function encodeGifFromDir( |
148 | 148 | fps: input.fps, |
149 | 149 | loop: input.loop, |
150 | 150 | }; |
151 | | - const paletteResult = await runFfmpeg(buildGifPalettegenArgs(argsInput), { |
152 | | - signal: input.signal, |
153 | | - timeout: input.timeout, |
154 | | - }); |
155 | | - if (!paletteResult.success) { |
156 | | - return { |
157 | | - success: false, |
158 | | - outputPath, |
159 | | - durationMs: Date.now() - startTime, |
160 | | - framesEncoded: 0, |
161 | | - fileSize: 0, |
162 | | - error: formatFfmpegError(paletteResult.exitCode, paletteResult.stderr), |
163 | | - }; |
164 | | - } |
| 151 | + try { |
| 152 | + const paletteResult = await runFfmpeg(buildGifPalettegenArgs(argsInput), { |
| 153 | + signal: input.signal, |
| 154 | + timeout: input.timeout, |
| 155 | + }); |
| 156 | + if (!paletteResult.success) { |
| 157 | + return { |
| 158 | + success: false, |
| 159 | + outputPath, |
| 160 | + durationMs: Date.now() - startTime, |
| 161 | + framesEncoded: 0, |
| 162 | + fileSize: 0, |
| 163 | + error: formatFfmpegError(paletteResult.exitCode, paletteResult.stderr), |
| 164 | + }; |
| 165 | + } |
165 | 166 |
|
166 | | - const gifResult = await runFfmpeg(buildGifPaletteuseArgs(argsInput), { |
167 | | - signal: input.signal, |
168 | | - timeout: input.timeout, |
169 | | - }); |
170 | | - if (!gifResult.success) { |
| 167 | + const gifResult = await runFfmpeg(buildGifPaletteuseArgs(argsInput), { |
| 168 | + signal: input.signal, |
| 169 | + timeout: input.timeout, |
| 170 | + }); |
| 171 | + if (!gifResult.success) { |
| 172 | + return { |
| 173 | + success: false, |
| 174 | + outputPath, |
| 175 | + durationMs: Date.now() - startTime, |
| 176 | + framesEncoded: 0, |
| 177 | + fileSize: 0, |
| 178 | + error: formatFfmpegError(gifResult.exitCode, gifResult.stderr), |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + const fileSize = existsSync(outputPath) ? statSync(outputPath).size : 0; |
171 | 183 | return { |
172 | | - success: false, |
| 184 | + success: true, |
173 | 185 | outputPath, |
174 | 186 | durationMs: Date.now() - startTime, |
175 | | - framesEncoded: 0, |
176 | | - fileSize: 0, |
177 | | - error: formatFfmpegError(gifResult.exitCode, gifResult.stderr), |
| 187 | + framesEncoded: frameCount, |
| 188 | + fileSize, |
178 | 189 | }; |
| 190 | + } finally { |
| 191 | + // The GIF palette is a temp file; remove it after success or any encode failure. |
| 192 | + rmSync(input.palettePath, { force: true }); |
179 | 193 | } |
180 | | - |
181 | | - const fileSize = existsSync(outputPath) ? statSync(outputPath).size : 0; |
182 | | - return { |
183 | | - success: true, |
184 | | - outputPath, |
185 | | - durationMs: Date.now() - startTime, |
186 | | - framesEncoded: frameCount, |
187 | | - fileSize, |
188 | | - }; |
189 | 194 | } |
190 | 195 |
|
191 | 196 | export async function runEncodeStage(input: EncodeStageInput): Promise<EncodeStageResult> { |
|
0 commit comments