* Lazy initialization of FFmpeg - only runs when needed, not at module load
()
| 21 | * Lazy initialization of FFmpeg - only runs when needed, not at module load |
| 22 | */ |
| 23 | function ensureFfmpeg(): void { |
| 24 | if (ffmpegInitialized) { |
| 25 | if (!ffmpegPath) { |
| 26 | throw new Error( |
| 27 | 'FFmpeg not found. Install: brew install ffmpeg (macOS) / apk add ffmpeg (Alpine) / apt-get install ffmpeg (Ubuntu)' |
| 28 | ) |
| 29 | } |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | ffmpegInitialized = true |
| 34 | |
| 35 | // Try ffmpeg-static binary |
| 36 | if (ffmpegStatic && typeof ffmpegStatic === 'string') { |
| 37 | try { |
| 38 | fsSync.accessSync(ffmpegStatic, fsSync.constants.X_OK) |
| 39 | ffmpegPath = ffmpegStatic |
| 40 | ffmpeg.setFfmpegPath(ffmpegPath) |
| 41 | logger.info('[FFmpeg] Using ffmpeg-static:', ffmpegPath) |
| 42 | return |
| 43 | } catch { |
| 44 | // Binary doesn't exist or not executable |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Try system ffmpeg (cross-platform) |
| 49 | try { |
| 50 | const cmd = process.platform === 'win32' ? 'where ffmpeg' : 'which ffmpeg' |
| 51 | const result = execSync(cmd, { encoding: 'utf-8' }).trim() |
| 52 | // On Windows, 'where' returns multiple paths - take first |
| 53 | ffmpegPath = result.split('\n')[0] |
| 54 | ffmpeg.setFfmpegPath(ffmpegPath) |
| 55 | logger.info('[FFmpeg] Using system ffmpeg:', ffmpegPath) |
| 56 | return |
| 57 | } catch { |
| 58 | // System ffmpeg not found |
| 59 | } |
| 60 | |
| 61 | // No FFmpeg found - set flag but don't throw yet |
| 62 | // Error will be thrown when user tries to use video extraction |
| 63 | logger.warn('[FFmpeg] No FFmpeg binary found at module load time') |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Extract audio from video or convert audio format using FFmpeg |
no test coverage detected