-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
328 lines (272 loc) · 10.3 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
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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
require('@electron/remote/main').initialize();
const { indexVideos, addActiveDownload, removeActiveDownload } = require('./videoIndexer');
const path = require('path');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const crypto = require('crypto');
const VideoDownloader = require('./downloader');
const downloader = new VideoDownloader();
const activeDownloads = new Map();
let mainWindow;
let indexedFolders = [];
let startupVideoPath = null;
const isDevelopment = process.env.NODE_ENV === 'development';
let defaultThumbPath;
if(isDevelopment)
defaultThumbPath = './default-thumbnail.jpg';
else defaultThumbPath = path.join(process.resourcesPath, 'default-thumbnail.jpg');
const thumbsDir = path.join(app.getPath('userData'), 'thumbnails');
if (process.argv.length > 1) {
// Получаем путь к видео из аргументов
startupVideoPath = process.argv[1];
// Для разработки в Electron
if (startupVideoPath.endsWith('electron.exe')) {
startupVideoPath = process.argv[2];
}
}
if (!fs.existsSync(thumbsDir)) {
fs.mkdirSync(thumbsDir);
if (fs.existsSync(defaultThumbPath)) {
fs.copyFileSync(defaultThumbPath, path.join(thumbsDir, 'default-thumbnail.jpg'));
}
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
webSecurity: false,
allowRunningInsecureContent: true,
},
icon: isDevelopment ? './assets/icon.ico' : path.join(process.resourcesPath, 'assets/icon.ico')
});
require('@electron/remote/main').enable(mainWindow.webContents);
mainWindow.setMenu(null);
mainWindow.loadFile('app.html');
//mainWindow.webContents.openDevTools();
if(!isDevelopment) {
mainWindow.webContents.on('did-finish-load', () => {
if (startupVideoPath && fs.existsSync(startupVideoPath)) {
mainWindow.webContents.send('startup-video', startupVideoPath);
}
});
}
}
// В main.js
app.setAsDefaultProtocolClient('localtube');
// Обработка открытия файлов в macOS
app.on('open-file', (event, path) => {
event.preventDefault();
if (mainWindow) {
mainWindow.webContents.send('startup-video', path);
} else {
startupVideoPath = path;
}
});
// Обработка второго экземпляра приложения в Windows
app.on('second-instance', (event, commandLine) => {
if (commandLine.length >= 2) {
const videoPath = commandLine[1];
if (mainWindow) {
mainWindow.webContents.send('startup-video', videoPath);
mainWindow.focus();
}
}
});
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Load settings from settings.json
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
if (fs.existsSync(settingsPath)) {
indexedFolders = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Навигация между страницами
ipcMain.on('navigate', (event, pageName) => {
mainWindow.webContents.send('navigate', pageName);
});
ipcMain.on('get-folders', (event) => {
event.reply('load-folders', indexedFolders);
});
ipcMain.on('add-folder', (event) => {
dialog.showOpenDialog({
properties: ['openDirectory']
}).then(result => {
if (!result.canceled) {
const newFolder = result.filePaths[0];
if (!indexedFolders.includes(newFolder)) {
indexedFolders.push(newFolder);
saveSettings();
event.reply('load-folders', indexedFolders);
}
}
});
});
ipcMain.on('remove-folder', (event, folder) => {
indexedFolders = indexedFolders.filter(f => f !== folder);
saveSettings();
event.reply('load-folders', indexedFolders);
});
ipcMain.on('index-videos', (event) => {
let downloadPath;
if(isDevelopment)
downloadPath = path.join(__dirname, 'downloads');
else downloadPath = path.join(process.execPath, '..', 'downloads');
const allFolders = [...indexedFolders];
if (fs.existsSync(downloadPath) && !allFolders.includes(downloadPath)) {
allFolders.unshift(downloadPath);
}
const videos = indexVideos(allFolders);
const filteredVideos = videos.filter(video =>
!Array.from(activeDownloads.values()).some(download =>
download.path === video
)
);
event.reply('video-list', filteredVideos);
});
ipcMain.on('open-video', (event, videoPath) => {
if (fs.existsSync(videoPath)) {
mainWindow.webContents.send('navigate', 'player');
// Используем setTimeout, чтобы дать время на загрузку страницы плеера
setTimeout(() => {
mainWindow.webContents.send('load-video', videoPath);
}, 100);
} else {
mainWindow.webContents.send('video-not-found', videoPath);
}
});
ipcMain.handle('get-video-info', async (event, videoUrl) => {
try {
return await downloader.getVideoInfo(videoUrl);
} catch (error) {
throw error;
}
});
ipcMain.handle('download-video', async (event, { videoUrl, videoFormat, audioFormat }) => {
try {
const downloadId = Date.now().toString();
const tempPath = path.join(app.getPath('temp'), `${downloadId}.mp4`);
// Добавляем загрузку в активные
activeDownloads.set(downloadId, {
id: downloadId,
url: videoUrl,
status: 'pending',
progress: 0,
tempPath
});
// Сообщаем всем окнам об обновлении списка загрузок
BrowserWindow.getAllWindows().forEach(win => {
win.webContents.send('download-update', Array.from(activeDownloads.values()));
});
// Начинаем загрузку
activeDownloads.get(downloadId).status = 'downloading';
updateDownloadStatus(downloadId);
const result = await downloader.downloadVideo(videoUrl, videoFormat, audioFormat);
// Обновляем статус на добавление метаданных
activeDownloads.get(downloadId).status = 'meta';
updateDownloadStatus(downloadId);
// После успешной загрузки
activeDownloads.delete(downloadId);
updateDownloadStatus(downloadId, true);
return result;
} catch (error) {
throw error;
}
});
function updateDownloadStatus(downloadId, finished = false) {
BrowserWindow.getAllWindows().forEach(win => {
if (finished) {
win.webContents.send('download-finished', downloadId);
} else {
win.webContents.send('download-update', Array.from(activeDownloads.values()));
}
});
}
ipcMain.handle('get-active-downloads', () => {
return Array.from(activeDownloads.values());
});
function saveSettings() {
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
fs.writeFileSync(settingsPath, JSON.stringify(indexedFolders, null, 2));
}
ipcMain.on('get-user-data-path', (event) => {
event.returnValue = app.getPath('userData');
});
// Кеш превью
const thumbnailCache = new Map();
ipcMain.handle('get-video-thumbnail', async (event, videoPath) => {
const videoHash = crypto.createHash('md5').update(videoPath).digest('hex');
const thumbnailPath = path.join(thumbsDir, `${videoHash}.jpg`);
const defaultLocalThumb = path.join(thumbsDir, 'default-thumbnail.jpg');
// Проверяем наличие дефолтного изображения в папке thumbnails
if (!fs.existsSync(defaultLocalThumb) && fs.existsSync(defaultThumbPath)) {
fs.copyFileSync(defaultThumbPath, defaultLocalThumb);
}
// Проверяем кеш
if (thumbnailCache.has(videoPath)) {
return thumbnailCache.get(videoPath);
}
// Проверяем существование файла превью
if (fs.existsSync(thumbnailPath)) {
thumbnailCache.set(videoPath, thumbnailPath);
return thumbnailPath;
}
// Создаем превью через ffmpeg
return new Promise((resolve, reject) => {
// Сначала проверяем наличие видеопотока
ffmpeg.ffprobe(videoPath, (err, metadata) => {
if (err) {
console.error('Error probing video:', err);
return resolve(defaultLocalThumb);
}
const hasVideoStream = metadata.streams.some(stream => stream.codec_type === 'video');
if (!hasVideoStream) {
console.error('No video stream found');
return resolve(defaultLocalThumb);
}
ffmpeg(videoPath)
.on('end', () => {
thumbnailCache.set(videoPath, thumbnailPath);
resolve(thumbnailPath);
})
.on('error', (err) => {
console.error('Error creating thumbnail:', err);
resolve(defaultLocalThumb);
})
.screenshots({
timestamps: ['10%'],
filename: path.basename(thumbnailPath),
folder: thumbsDir,
size: '320x180'
});
});
});
});
// Очистка кеша при низкой памяти
app.on('web-contents-created', (event, contents) => {
contents.on('destroyed', () => {
thumbnailCache.clear();
});
});
function checkMemoryUsage() {
const used = process.memoryUsage();
if (used.heapUsed > 500 * 1024 * 1024) { // 500MB
thumbnailCache.clear();
gc && gc();
}
}
setInterval(checkMemoryUsage, 30000);