Skip to content

Let's use AI to Earn!

feat: 恢复ffmpeg

yikaJasonMicd3b16759abb4
diff --git a/electron-builder.json b/electron-builder.json
index a19d5361..cbbe9d5a 100644
--- a/electron-builder.json
+++ b/electron-builder.json
@@ -7,6 +7,7 @@
"output": "release/${version}"
},
"files": ["dist-electron", "dist"],
+ "beforePack": "scripts/cleanup-ffmpeg.cjs",
"mac": {
"identity": null,
"artifactName": "${productName}-${version}-${arch}.${ext}",
diff --git a/electron/util/ffmpeg/index.ts b/electron/util/ffmpeg/index.ts
new file mode 100644
index 00000000..e734e405
--- /dev/null
+++ b/electron/util/ffmpeg/index.ts
@@ -0,0 +1,40 @@
+/*
+ * @Author: nevin
+ * @Date: 2025-02-14 16:36:05
+ * @LastEditTime: 2025-02-23 20:07:52
+ * @LastEditors: nevin
+ * @Description: ffmpeg工具
+ */
+import ffmpeg from 'fluent-ffmpeg'; // 一个封装了 ffmpeg API 的库,当然可以选择不安装,直接使用字符串拼接的方式调用
+import os from 'os';
+import { app } from 'electron';
+import path from 'path';
+import { fileURLToPath } from 'node:url';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+const platform = os.platform();
+const arch = os.arch();
+
+// 获取ffmpeg二进制文件的路径,不同平台不一样
+function setFFmpegPath() {
+ const ffPath = app.isPackaged
+ ? path.join(process.resourcesPath, 'bin')
+ : path.join(__dirname, `../../scripts/file/ff/${platform}/${arch}`);
+
+ ffmpeg.setFfprobePath(
+ path.join(ffPath, `ffprobe${platform === 'win32' ? '.exe' : ''}`),
+ );
+ ffmpeg.setFfmpegPath(
+ path.join(ffPath, `ffmpeg${platform === 'win32' ? '.exe' : ''}`),
+ );
+}
+setFFmpegPath();
+
+export default class FFmpeg {
+ ffmpeg: ffmpeg.FfmpegCommand;
+
+ constructor(inputPath: string) {
+ this.ffmpeg = ffmpeg(inputPath);
+ }
+}
diff --git a/electron/util/ffmpeg/video.ts b/electron/util/ffmpeg/video.ts
new file mode 100644
index 00000000..17c5b343
--- /dev/null
+++ b/electron/util/ffmpeg/video.ts
@@ -0,0 +1,59 @@
+/*
+ * @Author: nevin
+ * @Date: 2025-02-14 18:23:16
+ * @LastEditTime: 2025-03-24 20:55:11
+ * @LastEditors: nevin
+ * @Description:
+ */
+import { ScreenshotsConfig } from 'fluent-ffmpeg';
+import FFmpeg from './index';
+import path, { dirname } from 'path';
+import { fileURLToPath } from 'url';
+import { FileUtils } from '../file';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = dirname(__filename);
+
+export class FFmpegVideoUtil {
+ /**
+ * 获取视频封面
+ * @param videoFilePath 视频文件路径
+ * @param time 可选参数,指定要截取的时间点(格式为 '00:00:01')
+ * @returns
+ */
+ static async getVideoCover(videoFilePath: string, time?: string) {
+ return new Promise<string>((resolve, reject) => {
+ try {
+ const ffmpeg = new FFmpeg(videoFilePath);
+ console.log('------ outputDir', FileUtils.getAppDataPath());
+
+ const outputDir = path.join(FileUtils.getAppDataPath()!, 'screenshots');
+
+ const option: ScreenshotsConfig = {
+ count: 1,
+ folder: outputDir,
+ filename: 'thumbnail.png',
+ };
+
+ if (time) option.timemarks = [time];
+ ffmpeg.ffmpeg.takeScreenshots(option);
+
+ ffmpeg.ffmpeg.on('end', () => {
+ console.log('截取成功');
+ resolve(
+ outputDir.replace(/\\/g, '/') +
+ '/thumbnail.png'.replace(/\\/g, '/'),
+ );
+ });
+
+ ffmpeg.ffmpeg.on('error', (err) => {
+ console.log('---- getVideoCover on error---- 截取失败', err);
+ resolve('');
+ });
+ } catch (error) {
+ console.log('---- getVideoCover --- 截取失败', error);
+ resolve('');
+ }
+ });
+ }
+}
diff --git a/electron/util/ffprobe_static.ts b/electron/util/ffprobe_static.ts
new file mode 100644
index 00000000..ccb00d15
--- /dev/null
+++ b/electron/util/ffprobe_static.ts
@@ -0,0 +1,68 @@
+import os from 'os';
+import path from 'path';
+import fs from 'fs';
+
+const platform = os.platform();
+const arch = os.arch();
+
+// 检查平台支持
+if (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32') {
+ console.error('ffprobe unsupported platform.');
+ // process.exit(1);
+}
+
+// 检查 macOS 架构支持
+if (platform === 'darwin' && arch !== 'x64' && arch !== 'arm64') {
+ console.error('ffprobe unsupported architecture.');
+ // process.exit(1);
+}
+
+// 获取应用程序的根目录
+const getAppRootDir = (): string => {
+ // 在开发环境中
+ if (process.env.NODE_ENV === 'development') {
+ return path.join(process.cwd(), 'node_modules', 'ffprobe-static', 'bin');
+ }
+ // 在生产环境中
+ if (process.type === 'renderer') {
+ return path.join(process.resourcesPath, 'app.asar');
+ }
+ return path.join(process.resourcesPath, 'app.asar.unpacked');
+};
+
+// 构建 ffprobe 路径
+let ffprobePath: string;
+
+if (platform === 'win32') {
+ ffprobePath = path.join(getAppRootDir(), 'win32', 'x64', 'ffprobe.exe');
+} else if (platform === 'darwin') {
+ ffprobePath = path.join(
+ getAppRootDir(),
+ 'darwin',
+ arch === 'arm64' ? 'arm64' : 'x64',
+ 'ffprobe',
+ );
+} else {
+ ffprobePath = path.join(
+ getAppRootDir(),
+ 'linux',
+ arch === 'arm64' ? 'arm64' : 'x64',
+ 'ffprobe',
+ );
+}
+
+console.log('FFprobe 路径:', ffprobePath);
+
+// 检查文件是否存在
+try {
+ if (!fs.existsSync(ffprobePath)) {
+ // TODO: 打包之后会直接走这个逻辑,直接退出
+ console.log('FFprobe 可执行文件不存在:', ffprobePath);
+ // process.exit(1);
+ }
+} catch (err) {
+ console.log('检查 FFprobe 文件时出错:', err);
+ // process.exit(1);
+}
+
+export { ffprobePath };
diff --git a/electron/util/file.ts b/electron/util/file.ts
index 415f7a0d..e99aa769 100644
--- a/electron/util/file.ts
+++ b/electron/util/file.ts
@@ -1,11 +1,16 @@
import fs from 'fs';
import mimeTypes from 'mime-types';
import ffmpeg from 'fluent-ffmpeg';
+import { ffprobePath } from './ffprobe_static';
import path from 'path';
import { net } from 'electron';
import { v4 as uuidv4 } from 'uuid';
// 文件工具类
+// 设置 ffprobe 路径
+console.log('ffprobe 路径:', ffprobePath);
+ffmpeg.setFfprobePath(ffprobePath);
+
export interface FileInfo {
streams: Array<{
codec_type: string;
diff --git a/scripts/cleanup-ffmpeg.cjs b/scripts/cleanup-ffmpeg.cjs
new file mode 100644
index 00000000..204814da
--- /dev/null
+++ b/scripts/cleanup-ffmpeg.cjs
@@ -0,0 +1,50 @@
+/*
+ * @Author: nevin
+ * @Date: 2025-02-23 17:22:25
+ * @LastEditTime: 2025-02-23 20:16:13
+ * @LastEditors: nevin
+ * @Description: ff 组件复制脚本
+ */
+const fs = require('fs');
+const path = require('path');
+const { platform, arch } = process; // 获取当前构建平台
+// 获取当前的CPU架构
+
+function getFFmpegPath() {
+ const res = {
+ ffmpegPath: '',
+ ffprobePath: '',
+ };
+
+ res.ffmpegPath = path.join(
+ __dirname,
+ './file',
+ `ff/${platform}/${arch}/ffmpeg`,
+ );
+ res.ffprobePath = path.join(
+ __dirname,
+ './file',
+ `ff/${platform}/${arch}/ffprobe`,
+ );
+
+ return res;
+}
+
+exports.default = async function (context) {
+ const binPath = path.join(__dirname, '../public/bin');
+
+ const pathInfo = getFFmpegPath();
+
+ const fileNameEnd = platform === 'win32' ? '.exe' : '';
+
+ // 复制ffmpeg
+ fs.copyFileSync(
+ pathInfo.ffmpegPath + fileNameEnd,
+ path.join(binPath, `ffmpeg${fileNameEnd}`),
+ );
+
+ fs.copyFileSync(
+ pathInfo.ffprobePath + fileNameEnd,
+ path.join(binPath, `ffprobe${fileNameEnd}`),
+ );
+};
diff --git a/scripts/file/ff/darwin/arm64/ffmpeg b/scripts/file/ff/darwin/arm64/ffmpeg
new file mode 100644
index 00000000..202a3f85
Binary files /dev/null and b/scripts/file/ff/darwin/arm64/ffmpeg differ
diff --git a/scripts/file/ff/darwin/arm64/ffprobe b/scripts/file/ff/darwin/arm64/ffprobe
new file mode 100644
index 00000000..2c213a87
Binary files /dev/null and b/scripts/file/ff/darwin/arm64/ffprobe differ
diff --git a/scripts/file/ff/darwin/x64/ffmpeg b/scripts/file/ff/darwin/x64/ffmpeg
new file mode 100644
index 00000000..779b629c
Binary files /dev/null and b/scripts/file/ff/darwin/x64/ffmpeg differ
diff --git a/scripts/file/ff/darwin/x64/ffprobe b/scripts/file/ff/darwin/x64/ffprobe
new file mode 100644
index 00000000..c0a07932
Binary files /dev/null and b/scripts/file/ff/darwin/x64/ffprobe differ