-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
169 lines (142 loc) · 4.49 KB
/
main.js
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
const os = require('os');
const { app, BrowserWindow, screen, ipcMain, dialog } = require('electron');
const { execSync, exec } = require('child_process');
const ffmpeg = require('ffmpeg-static-electron');
const fs = require('fs');
let mainWindow;
let drawWindow;
let dpiScale = 1;
let filename = 'output';
const makeTransparentWindow = () => {
const screenWindow = screen.getPrimaryDisplay().workAreaSize;
dpiScale = screen.getPrimaryDisplay().scaleFactor;
const transparentWindow = new BrowserWindow({
width: screenWindow.width,
height: screenWindow.height,
webPreferences: {
nodeIntegration: true,
},
transparent: true,
frame: false,
show: false,
});
transparentWindow.loadFile('./pages/transparent.html');
transparentWindow.setResizable(false);
// transparentWindow.webContents.openDevTools();
drawWindow = transparentWindow;
return transparentWindow;
};
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 1200,
height: 600,
show: true,
webPreferences: {
nodeIntegration: true,
},
});
mainWindow.loadFile('./pages/index.html');
// Open DevTools
mainWindow.webContents.openDevTools();
};
ipcMain.on('OPEN_CAPTURE_WINDOW', () => {
const transparentWindow = makeTransparentWindow();
transparentWindow.show();
mainWindow.hide();
});
ipcMain.on('UPDATE_FILE_NAME', (__message, value) => {
console.log({ value });
filename = value;
});
app
.whenReady()
.then(createWindow)
.catch(err => {
console.error(err);
});
let ffmpegProcess;
const logError = (error, stdout, stderr) => {
if (error) {
console.error({ error, stderr });
}
};
const stopRecording = ipcEvent => () => {
console.log('STOP RECORDING');
drawWindow.hide();
ffmpegProcess.stdin.write('q');
mainWindow.webContents.send('SET_LOADING', true);
mainWindow.show();
setTimeout(() => mainWindow.webContents.send('LOAD_VIDEO'), 1000);
mainWindow.webContents.send('SET_LOADING', false);
};
ipcMain.on('CANCEL_RECORDING', () => {
console.log('STOP RECORDING');
drawWindow.hide();
mainWindow.show();
});
ipcMain.on('SAVE_GIF', async (event, { startTime, endTime }) => {
console.log('save gif');
const FPS = 24;
const SCALE = 1080;
const videoFilepath = `${filename}.mkv`;
const { filePath } = await dialog.showSaveDialog(mainWindow, {
title: 'Save Gif',
filters: [{ name: 'Gif', extensions: ['gif'] }],
});
if (!filePath) {
console.log('no file path');
event.reply('SAVE_GIF_DONE');
return null;
}
console.log('saving to', filePath);
const duration = endTime - startTime;
console.log('palatte generation');
// create palette
const palatte = execSync(
`${ffmpeg.path} -y -ss ${startTime} -t ${duration} -i ${videoFilepath} -vf "fps=${FPS}, scale=${SCALE}:-1:flags=lanczos,palettegen" palette.png`,
logError,
);
console.log('gif creation');
const gif = execSync(
`${ffmpeg.path} -y -ss ${startTime} -t ${duration} -i ${videoFilepath} -i palette.png -q 0 -filter_complex "fps=${FPS},scale=${SCALE}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=3:diff_mode=rectangle" ${filePath}`,
logError,
);
console.log('save gif finished');
event.reply('SAVE_GIF_DONE');
});
ipcMain.on(
'START_RECORDING',
(event, { x, y, width, height, clientScreenDifX, clientScreenDifY }) => {
console.log('START RECORDING');
drawWindow.minimize();
const outputFile = `${filename}.mkv`;
const xValue = Math.floor((x - clientScreenDifX) * dpiScale);
const yValue = Math.floor((y - clientScreenDifY) * dpiScale);
const heightValue = Math.floor(height * dpiScale);
const widthValue = Math.floor(width * dpiScale);
if (os.platform() === 'darwin') {
// on mac
ffmpegProcess = exec(
`${ffmpeg.path} -y -f avfoundation -pix_fmt yuyv422 -framerate 24 -capture_cursor 1 -capture_mouse_clicks 1 -i "1:none" -vf "crop=${widthValue}:${heightValue}:${xValue}:${yValue}" ${outputFile}`,
logError,
);
}
if (os.platform() === 'win32') {
// on pc
ffmpegProcess = exec(
`${ffmpeg.path} -y -f gdigrab -framerate 30 -offset_x ${xValue} -offset_y ${yValue} -video_size ${widthValue}x${heightValue} -i desktop ${outputFile}`,
logError,
);
}
// TODO SUPPORT OTHER SYSTEMS
drawWindow.on('focus', stopRecording(event));
},
);
ipcMain.on('CLEAR_FILE', event => {
fs.unlink(`${filename}.mkv`, err => {
if (err) {
console.error(err);
}
event.reply('CLEAR_FILE_SUCCESS');
});
});