| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-14 18:23:16 |
| 4 | * @LastEditTime: 2025-03-24 20:55:11 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { ScreenshotsConfig } from 'fluent-ffmpeg'; |
| 9 | import FFmpeg from './index'; |
| 10 | import path, { dirname } from 'path'; |
| 11 | import { fileURLToPath } from 'url'; |
| 12 | import { FileUtils } from '../file'; |
| 13 | |
| 14 | const __filename = fileURLToPath(import.meta.url); |
| 15 | const __dirname = dirname(__filename); |
| 16 | |
| 17 | export class FFmpegVideoUtil { |
| 18 | /** |
| 19 | * 获取视频封面 |
| 20 | * @param videoFilePath 视频文件路径 |
| 21 | * @param time 可选参数,指定要截取的时间点(格式为 '00:00:01') |
| 22 | * @returns |
| 23 | */ |
| 24 | static async getVideoCover(videoFilePath: string, time?: string) { |
| 25 | return new Promise<string>((resolve, reject) => { |
| 26 | try { |
| 27 | const ffmpeg = new FFmpeg(videoFilePath); |
| 28 | console.log('------ outputDir', FileUtils.getAppDataPath()); |
| 29 | |
| 30 | const outputDir = path.join(FileUtils.getAppDataPath()!, 'screenshots'); |
| 31 | |
| 32 | const option: ScreenshotsConfig = { |
| 33 | count: 1, |
| 34 | folder: outputDir, |
| 35 | filename: 'thumbnail.png', |
| 36 | }; |
| 37 | |
| 38 | if (time) option.timemarks = [time]; |
| 39 | ffmpeg.ffmpeg.takeScreenshots(option); |
| 40 | |
| 41 | ffmpeg.ffmpeg.on('end', () => { |
| 42 | console.log('截取成功'); |
| 43 | resolve( |
| 44 | outputDir.replace(/\\/g, '/') + |
| 45 | '/thumbnail.png'.replace(/\\/g, '/'), |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | ffmpeg.ffmpeg.on('error', (err) => { |
| 50 | console.log('---- getVideoCover on error---- 截取失败', err); |
| 51 | resolve(''); |
| 52 | }); |
| 53 | } catch (error) { |
| 54 | console.log('---- getVideoCover --- 截取失败', error); |
| 55 | resolve(''); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | } |
| 60 |