-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorder.js
More file actions
368 lines (311 loc) · 10.4 KB
/
Copy pathrecorder.js
File metadata and controls
368 lines (311 loc) · 10.4 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const { BrowserWindow, desktopCapturer, app } = require('electron');
const fs = require('fs');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
// Set FFmpeg path
// 在打包后的应用中,需要使用正确的资源路径
let ffmpegPath;
// 检查是否是打包后的应用
if (app.isPackaged) {
// 打包后,不使用 ffmpeg-static 模块
const platform = process.platform;
const ffmpegName = platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg';
// 尝试多个可能的路径
const possiblePaths = [
// 方案1: extraResources 配置的路径
path.join(process.resourcesPath, 'ffmpeg', ffmpegName),
// 方案2: app.asar.unpacked 路径
path.join(process.resourcesPath, 'app.asar.unpacked', 'node_modules', 'ffmpeg-static', ffmpegName),
// 方案3: 直接在 resources 下
path.join(process.resourcesPath, ffmpegName)
];
console.log('Searching for FFmpeg in packaged app...');
console.log('Resources path:', process.resourcesPath);
for (const testPath of possiblePaths) {
console.log('Trying path:', testPath);
if (fs.existsSync(testPath)) {
ffmpegPath = testPath;
console.log('Found FFmpeg at:', ffmpegPath);
break;
}
}
if (!ffmpegPath) {
console.error('FFmpeg not found in any expected location!');
console.error('Tried paths:', possiblePaths);
// 使用第一个路径作为回退(即使不存在,也能显示预期路径)
ffmpegPath = possiblePaths[0];
}
} else {
// 开发环境,使用 ffmpeg-static
const ffmpegStatic = require('ffmpeg-static');
ffmpegPath = ffmpegStatic;
console.log('Development mode, using ffmpeg-static:', ffmpegPath);
}
console.log('Final FFmpeg path:', ffmpegPath);
console.log('FFmpeg exists:', fs.existsSync(ffmpegPath));
ffmpeg.setFfmpegPath(ffmpegPath);
class ScreenRecorder {
constructor() {
this.recordingWindow = null;
this.isRecording = false;
this.isPaused = false;
this.settings = null;
this.bounds = null;
this.recordingData = null;
this.ffmpegCommand = null; // 存储 FFmpeg 命令引用以便清理
this.ipcListeners = []; // 存储 IPC 监听器以便清理
}
async initialize() {
// Create hidden recording window
this.recordingWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
await this.recordingWindow.loadFile('src/recording.html');
// Set up IPC listeners
this.setupListeners();
}
setupListeners() {
const { ipcMain } = require('electron');
// 存储监听器引用以便清理
const recordingStartedHandler = (event, data) => {
this.isRecording = true;
if (this.onStatusChange) {
this.onStatusChange({ status: 'recording' });
}
};
const recordingPausedHandler = () => {
this.isPaused = true;
if (this.onStatusChange) {
this.onStatusChange({ status: 'paused' });
}
};
const recordingResumedHandler = () => {
this.isPaused = false;
if (this.onStatusChange) {
this.onStatusChange({ status: 'recording' });
}
};
const recordingStoppedHandler = async (event, buffer) => {
this.isRecording = false;
this.isPaused = false;
await this.processRecording(buffer);
};
const recordingErrorHandler = (event, data) => {
console.error('Recording error:', data.error);
this.cleanup();
if (this.onStatusChange) {
this.onStatusChange({ status: 'error', error: data.error });
}
};
// 注册监听器
ipcMain.on('recording-started', recordingStartedHandler);
ipcMain.on('recording-paused', recordingPausedHandler);
ipcMain.on('recording-resumed', recordingResumedHandler);
ipcMain.on('recording-stopped', recordingStoppedHandler);
ipcMain.on('recording-error', recordingErrorHandler);
// 保存监听器引用以便清理
this.ipcListeners = [
{ event: 'recording-started', handler: recordingStartedHandler },
{ event: 'recording-paused', handler: recordingPausedHandler },
{ event: 'recording-resumed', handler: recordingResumedHandler },
{ event: 'recording-stopped', handler: recordingStoppedHandler },
{ event: 'recording-error', handler: recordingErrorHandler }
];
}
async startRecording(bounds, settings) {
if (this.isRecording) {
throw new Error('Recording already in progress');
}
this.settings = settings;
this.bounds = bounds;
try {
// Get available sources
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: { width: 1920, height: 1080 }
});
if (sources.length === 0) {
throw new Error('No screen sources available');
}
// Use primary screen
const primarySource = sources[0];
// Send start command to recording window
this.recordingWindow.webContents.send('start-capture', {
sourceId: primarySource.id,
bounds: bounds
});
return { success: true };
} catch (error) {
console.error('Error starting recording:', error);
throw error;
}
}
pauseRecording() {
if (!this.isRecording || this.isPaused) {
return { success: false };
}
this.recordingWindow.webContents.send('pause-capture');
return { success: true };
}
resumeRecording() {
if (!this.isRecording || !this.isPaused) {
return { success: false };
}
this.recordingWindow.webContents.send('resume-capture');
return { success: true };
}
stopRecording() {
if (!this.isRecording) {
return { success: false };
}
this.recordingWindow.webContents.send('stop-capture');
return { success: true };
}
async processRecording(buffer) {
try {
const outputDir = this.settings.outputPath;
const resolution = this.getResolution(this.settings.resolution);
// Generate filenames
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('T')[0] + '_' +
new Date().toTimeString().split(' ')[0].replace(/:/g, '-');
const tempPath = path.join(outputDir, `temp-${timestamp}.webm`);
const outputPath = path.join(outputDir, `recording-${timestamp}.mp4`);
// Save temporary WebM file
fs.writeFileSync(tempPath, buffer);
// Convert to MP4 with FFmpeg (with cropping)
await this.convertToMP4(tempPath, outputPath, resolution, this.bounds);
// Delete temporary file
try {
fs.unlinkSync(tempPath);
} catch (err) {
console.error('Error deleting temp file:', err);
}
if (this.onStatusChange) {
this.onStatusChange({
status: 'completed',
outputPath: outputPath
});
}
return outputPath;
} catch (error) {
console.error('Error processing recording:', error);
if (this.onStatusChange) {
this.onStatusChange({ status: 'error', error: error.message });
}
throw error;
}
}
convertToMP4(inputPath, outputPath, resolution, bounds) {
return new Promise((resolve, reject) => {
this.ffmpegCommand = ffmpeg(inputPath)
.videoCodec('libx264')
.audioCodec('aac')
.fps(30)
.outputOptions([
'-preset fast',
'-crf 23',
'-pix_fmt yuv420p',
'-movflags +faststart'
]);
// 如果有选中区域,使用 crop 滤镜裁剪视频
if (bounds) {
// FFmpeg crop 滤镜: crop=width:height:x:y
const cropFilter = `crop=${bounds.width}:${bounds.height}:${bounds.x}:${bounds.y}`;
// 裁剪后再缩放到目标分辨率
const scaleFilter = `scale=${resolution.width}:${resolution.height}`;
// 组合滤镜
this.ffmpegCommand.videoFilters([cropFilter, scaleFilter]);
} else {
// 如果没有选中区域,只缩放
this.ffmpegCommand.size(`${resolution.width}x${resolution.height}`);
}
this.ffmpegCommand
.on('start', (cmd) => {
console.log('FFmpeg command:', cmd);
})
.on('progress', (progress) => {
if (this.onStatusChange) {
this.onStatusChange({
status: 'processing',
progress: progress.percent
});
}
})
.on('end', () => {
console.log('Conversion completed');
this.ffmpegCommand = null; // 清除引用
resolve();
})
.on('error', (err) => {
console.error('FFmpeg error:', err);
this.ffmpegCommand = null; // 清除引用
reject(err);
})
.save(outputPath);
});
}
getResolution(resolutionSetting) {
const resolutions = {
'720p': { width: 1280, height: 720 },
'1080p': { width: 1920, height: 1080 }
};
return resolutions[resolutionSetting] || resolutions['1080p'];
}
cleanup() {
this.isRecording = false;
this.isPaused = false;
this.bounds = null;
// 终止正在运行的 FFmpeg 进程
if (this.ffmpegCommand) {
try {
this.ffmpegCommand.kill('SIGKILL');
console.log('FFmpeg process killed');
} catch (err) {
console.error('Error killing FFmpeg process:', err);
}
this.ffmpegCommand = null;
}
}
destroy() {
console.log('Destroying ScreenRecorder...');
// 停止正在进行的录制
if (this.isRecording) {
this.stopRecording();
}
// 清理资源
this.cleanup();
// 移除所有 IPC 监听器
if (this.ipcListeners && this.ipcListeners.length > 0) {
const { ipcMain } = require('electron');
this.ipcListeners.forEach(({ event, handler }) => {
ipcMain.removeListener(event, handler);
});
this.ipcListeners = [];
console.log('IPC listeners removed');
}
// 关闭录制窗口
if (this.recordingWindow && !this.recordingWindow.isDestroyed()) {
// 通知录制窗口停止所有流
try {
this.recordingWindow.webContents.send('cleanup-streams');
} catch (err) {
console.error('Error sending cleanup signal:', err);
}
this.recordingWindow.close();
this.recordingWindow = null;
console.log('Recording window closed');
}
console.log('ScreenRecorder destroyed');
}
getStatus() {
return {
isRecording: this.isRecording,
isPaused: this.isPaused
};
}
}
module.exports = ScreenRecorder;