-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
297 lines (259 loc) · 8.15 KB
/
main.js
File metadata and controls
297 lines (259 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const {
app,
BrowserWindow,
ipcMain,
Menu,
dialog,
desktopCapturer,
} = require("electron");
const path = require("path");
const fs = require("fs");
const { exec } = require("child_process");
const os = require("os");
const ffmpegPath = require("ffmpeg-static");
// 设置控制台输出编码
process.env.LANG = "zh_CN.UTF-8";
// 确保子进程使用UTF-8编码
process.env.PYTHONIOENCODING = "UTF-8";
// 创建一个帮助函数来确保正确的编码输出
const logWithEncoding = (message) => {
if (typeof message === "string") {
console.log(Buffer.from(message, "utf8").toString());
} else {
console.log(message);
}
};
// 重写错误日志函数
const errorWithEncoding = (message, error) => {
if (typeof message === "string") {
console.error(Buffer.from(message, "utf8").toString(), error);
} else {
console.error(message, error);
}
};
// 获取FFmpeg可执行文件路径
function getFfmpegPath() {
// 修复打包后的ffmpeg路径问题
// 判断是开发环境还是生产环境
if (app.isPackaged) {
// 打包后的环境,通过process.resourcesPath找到资源目录
const ffmpegName = process.platform === "win32" ? "ffmpeg.exe" : "ffmpeg";
// 检查resources目录是否存在ffmpeg
const resourcesPath = path.join(
process.resourcesPath,
"ffmpeg",
ffmpegName
);
if (fs.existsSync(resourcesPath)) {
return resourcesPath;
}
// 如果resources目录不存在,尝试在应用目录下查找
const appPath = path.join(
path.dirname(app.getAppPath()),
"ffmpeg",
ffmpegName
);
if (fs.existsSync(appPath)) {
return appPath;
}
logWithEncoding("使用默认ffmpeg路径: " + ffmpegPath);
}
return ffmpegPath;
}
// 隐藏菜单栏
Menu.setApplicationMenu(null);
let mainWindow;
function createWindow() {
// 检查preload脚本是否存在
const preloadPath = path.join(__dirname, "preload.js");
logWithEncoding("Preload脚本路径: " + preloadPath);
logWithEncoding("Preload脚本存在: " + fs.existsSync(preloadPath));
mainWindow = new BrowserWindow({
width: 600,
height: 380,
resizable: false,
maximizable: false,
fullscreenable: false,
webPreferences: {
preload: preloadPath,
contextIsolation: true,
nodeIntegration: false,
sandbox: false, // 禁用沙盒以允许更多功能
// 允许访问媒体设备和捕获系统音频
audioCapturerEnabled: true,
},
icon: path.join(__dirname, "assets/logo.ico"),
});
mainWindow.loadFile("index.html");
// 开发时打开开发者工具
// mainWindow.webContents.openDevTools();
mainWindow.on("closed", () => {
mainWindow = null;
});
}
// 添加应用启动参数,允许录制系统音频
app.commandLine.appendSwitch("enable-features", "WebRTCAudioCapturing");
app.commandLine.appendSwitch("enable-usermedia-screen-capturing");
app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
createWindow();
}
});
// 处理获取屏幕源
ipcMain.handle("get-sources", async () => {
try {
const { screen } = require("electron");
const displays = screen.getAllDisplays();
const sources = await desktopCapturer.getSources({
types: ["screen"],
thumbnailSize: { width: 100, height: 100 },
});
sources.forEach((source, index) => {
source.displaySize = `${displays[index].size.width}x${displays[index].size.height}`;
});
return sources;
} catch (error) {
errorWithEncoding("获取屏幕源出错:", error);
throw error;
}
});
// 处理保存录制文件
ipcMain.on("save-recording", async (event, buffer, format = "mp4") => {
// 根据选择的格式设置对话框选项
const isWebm = format === "webm";
const fileExtension = isWebm ? "webm" : "mp4";
const fileTypeLabel = isWebm ? "WebM 文件" : "MP4 文件";
const { filePath } = await dialog.showSaveDialog({
buttonLabel: "保存视频",
defaultPath: `recording-${Date.now()}.${fileExtension}`,
filters: [{ name: fileTypeLabel, extensions: [fileExtension] }],
});
if (filePath) {
// 创建临时WebM文件
const tempDir = os.tmpdir();
const tempWebmPath = path.join(tempDir, `temp-${Date.now()}.webm`);
logWithEncoding("临时WebM路径: " + tempWebmPath);
logWithEncoding("最终目标路径: " + filePath);
logWithEncoding("选择的格式: " + format);
// 先保存WebM文件
fs.writeFile(tempWebmPath, buffer, async (err) => {
if (err) {
errorWithEncoding("保存临时WebM文件失败:", err);
event.reply("save-recording-response", {
success: false,
message: "保存失败:" + err.message,
});
return;
}
try {
// 如果选择WebM格式,直接重命名文件
if (isWebm) {
fs.renameSync(tempWebmPath, filePath);
event.reply("save-recording-response", {
success: true,
message: "保存成功",
filePath,
});
return;
}
// 如果选择MP4格式,需要转换
// 创建临时MP4文件路径
const tempMp4Path = path.join(tempDir, `temp-${Date.now()}.mp4`);
logWithEncoding("临时MP4路径: " + tempMp4Path);
// 获取并验证FFmpeg路径
const ffmpegPath = getFfmpegPath();
logWithEncoding("使用的FFmpeg路径: " + ffmpegPath);
if (!fs.existsSync(ffmpegPath)) {
throw new Error("找不到FFmpeg可执行文件: " + ffmpegPath);
}
// 使用FFmpeg转换WebM为MP4
await new Promise((resolve, reject) => {
const command = `"${ffmpegPath}" -i "${tempWebmPath}" -c:v libx264 -preset medium -crf 23 "${tempMp4Path}"`;
logWithEncoding("执行的FFmpeg命令: " + command);
// 设置子进程的编码为UTF-8
exec(
command,
{
encoding: "utf8",
env: {
...process.env,
PYTHONIOENCODING: "UTF-8",
LANG: "zh_CN.UTF-8",
},
},
(error, stdout, stderr) => {
if (error) {
errorWithEncoding("FFmpeg错误:", error);
errorWithEncoding("FFmpeg输出:", stderr);
reject(new Error(`FFmpeg转换失败: ${error.message}`));
return;
}
resolve();
}
);
});
// 将转换后的MP4文件移动到目标位置
fs.renameSync(tempMp4Path, filePath);
// 清理临时文件
fs.unlinkSync(tempWebmPath);
event.reply("save-recording-response", {
success: true,
message: "保存成功",
filePath,
});
} catch (error) {
errorWithEncoding("处理视频文件失败:", error);
// 清理临时文件
try {
if (fs.existsSync(tempWebmPath)) {
fs.unlinkSync(tempWebmPath);
}
if (
format === "mp4" &&
fs.existsSync(path.join(tempDir, `temp-${Date.now()}.mp4`))
) {
fs.unlinkSync(path.join(tempDir, `temp-${Date.now()}.mp4`));
}
} catch (cleanupError) {
errorWithEncoding("清理临时文件失败:", cleanupError);
}
// 提供更详细的错误信息
let errorMessage = "处理视频文件失败";
if (error.message) {
errorMessage += ": " + error.message;
}
if (error.code) {
errorMessage += " (错误代码: " + error.code + ")";
}
event.reply("save-recording-response", {
success: false,
message: errorMessage,
});
}
});
} else {
event.reply("save-recording-response", {
success: false,
message: "保存取消",
});
}
});
// 处理最小化窗口请求
ipcMain.on("minimize-window", () => {
if (mainWindow) {
mainWindow.minimize();
}
});
// 处理显示窗口请求
ipcMain.on("show-window", () => {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
});