* 从 WebM 视频提取第一帧 PNG
(webmBuffer: Buffer)
| 66 | |
| 67 | /** |
| 68 | * 从 WebM 视频提取第一帧 PNG |
| 69 | */ |
| 70 | async function extractWebmFirstFrame(webmBuffer: Buffer): Promise<Buffer | null> { |
| 71 | const uniqueId = Date.now().toString() + '_' + Math.random().toString(36).slice(2); |
| 72 | const webmPath = path.join(XMSL_TEMP_DIR, `sticker_${uniqueId}.webm`); |
| 73 | const pngPath = path.join(XMSL_TEMP_DIR, `sticker_${uniqueId}.png`); |
| 74 | |
| 75 | try { |
| 76 | fs.writeFileSync(webmPath, webmBuffer); |
| 77 | |
| 78 | await execFileAsync('ffmpeg', [ |
| 79 | '-i', webmPath, |
| 80 | '-vf', 'select=eq(n\\,0)', |
| 81 | '-vframes', '1', |
| 82 | '-y', |
| 83 | pngPath |
| 84 | ]); |
| 85 | |
| 86 | if (fs.existsSync(pngPath)) { |
| 87 | return fs.readFileSync(pngPath); |
| 88 | } |
| 89 | return null; |
| 90 | } catch (error) { |
| 91 | console.error('[xmsl] WebM 第一帧提取失败:', error); |
| 92 | return null; |
| 93 | } finally { |
| 94 | try { fs.unlinkSync(webmPath); } catch {} |
| 95 | try { fs.unlinkSync(pngPath); } catch {} |
| 96 | } |
| 97 | } |
| 98 |
no test coverage detected