|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Extract a preview frame from each demo GIF for quick visual inspection. |
| 4 | + * Saves individual PNG frames to demo-previews/ directory. |
| 5 | + * |
| 6 | + * Requires: ffmpeg, gifsicle (for frame delay info) |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * node preview-gifs.js # default: 3s before end |
| 10 | + * node preview-gifs.js --before 5 # 5s before end |
| 11 | + */ |
| 12 | + |
| 13 | +const { execSync } = require('child_process'); |
| 14 | +const { readdirSync, existsSync, mkdirSync, rmSync } = require('fs'); |
| 15 | +const { join, basename } = require('path'); |
| 16 | + |
| 17 | +const rootDir = join(__dirname, '..', '..'); |
| 18 | +const previewDir = join(rootDir, 'demo-previews'); |
| 19 | + |
| 20 | +// Parse CLI args |
| 21 | +const args = process.argv.slice(2); |
| 22 | +let beforeSeconds = 3; |
| 23 | + |
| 24 | +for (let i = 0; i < args.length; i++) { |
| 25 | + if (args[i] === '--before' && args[i + 1]) { |
| 26 | + beforeSeconds = parseFloat(args[++i]); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Find all demo GIFs |
| 31 | +function findGifs() { |
| 32 | + const gifs = []; |
| 33 | + for (const entry of readdirSync(rootDir)) { |
| 34 | + if (!/^\d{2}-/.test(entry)) continue; |
| 35 | + const imagesDir = join(rootDir, entry, 'images'); |
| 36 | + if (!existsSync(imagesDir)) continue; |
| 37 | + for (const file of readdirSync(imagesDir)) { |
| 38 | + if (file.endsWith('-demo.gif')) { |
| 39 | + gifs.push({ path: join(imagesDir, file), chapter: entry }); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return gifs.sort((a, b) => a.path.localeCompare(b.path)); |
| 44 | +} |
| 45 | + |
| 46 | +// Get frame delays from a GIF |
| 47 | +function getFrameDelays(gifPath) { |
| 48 | + const output = execSync(`gifsicle --info "${gifPath}"`, { encoding: 'utf8', maxBuffer: 50 * 1024 * 1024 }); |
| 49 | + const delays = []; |
| 50 | + const delayRegex = /delay (\d+(?:\.\d+)?)s/g; |
| 51 | + let match; |
| 52 | + while ((match = delayRegex.exec(output)) !== null) { |
| 53 | + delays.push(parseFloat(match[1])); |
| 54 | + } |
| 55 | + return delays; |
| 56 | +} |
| 57 | + |
| 58 | +// Find frame index at N seconds before the end |
| 59 | +function frameAtSecondsBeforeEnd(delays, seconds) { |
| 60 | + const totalTime = delays.reduce((a, b) => a + b, 0); |
| 61 | + const targetTime = totalTime - seconds; |
| 62 | + if (targetTime <= 0) return 0; |
| 63 | + |
| 64 | + let cumulative = 0; |
| 65 | + for (let i = 0; i < delays.length; i++) { |
| 66 | + cumulative += delays[i]; |
| 67 | + if (cumulative >= targetTime) return i; |
| 68 | + } |
| 69 | + return delays.length - 1; |
| 70 | +} |
| 71 | + |
| 72 | +// Main |
| 73 | +if (existsSync(previewDir)) rmSync(previewDir, { recursive: true }); |
| 74 | +mkdirSync(previewDir, { recursive: true }); |
| 75 | + |
| 76 | +const gifs = findGifs(); |
| 77 | +if (gifs.length === 0) { |
| 78 | + console.log('No demo GIFs found'); |
| 79 | + process.exit(0); |
| 80 | +} |
| 81 | + |
| 82 | +console.log(`\nExtracting frames (${beforeSeconds}s before end) from ${gifs.length} GIFs...\n`); |
| 83 | + |
| 84 | +let count = 0; |
| 85 | +for (const { path: gif, chapter } of gifs) { |
| 86 | + const name = basename(gif, '.gif'); |
| 87 | + const delays = getFrameDelays(gif); |
| 88 | + const frameIndex = frameAtSecondsBeforeEnd(delays, beforeSeconds); |
| 89 | + const prefix = chapter.replace(/^(\d+)-.+/, '$1'); |
| 90 | + const outName = `${prefix}-${name}.png`; |
| 91 | + const outPath = join(previewDir, outName); |
| 92 | + |
| 93 | + try { |
| 94 | + execSync( |
| 95 | + `ffmpeg -y -i "${gif}" -vf "select=eq(n\\,${frameIndex})" -vframes 1 -update 1 "${outPath}" 2>/dev/null`, |
| 96 | + { stdio: 'pipe' } |
| 97 | + ); |
| 98 | + console.log(` ✓ ${outName} (frame #${frameIndex}/${delays.length})`); |
| 99 | + count++; |
| 100 | + } catch (e) { |
| 101 | + console.log(` ✗ ${name}: extraction failed`); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +console.log(`\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`); |
| 106 | +console.log(`✓ ${count} preview frames saved to demo-previews/`); |
| 107 | +console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`); |
| 108 | +console.log(`\nOpen in Finder: open demo-previews/`); |
0 commit comments