-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain-electron.js
More file actions
166 lines (144 loc) · 5.28 KB
/
main-electron.js
File metadata and controls
166 lines (144 loc) · 5.28 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const VideoToFramesConverter = require('./VideoToFramesConverter');
let mainWindow;
let converter;
function resolveIconPath() {
// In production, resources are under process.resourcesPath
const candidates = [
path.join(__dirname, 'icon.ico'),
path.join(process.resourcesPath || '', 'icon.ico'),
path.join(app.getAppPath(), 'icon.ico')
];
for (const p of candidates) {
try {
if (p && fs.existsSync(p)) return p;
} catch (_) {}
}
// Fallback: Electron default (undefined)
return undefined;
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 900,
height: 700,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
icon: resolveIconPath(),
title: 'Frame Splitter'
});
// Load from dist-react in production, dev server in development
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('http://localhost:5173');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, 'dist-react', 'index.html'));
}
}
app.whenReady().then(() => {
// Ensure proper taskbar icon and app identity on Windows
try {
if (process.platform === 'win32') {
app.setAppUserModelId('com.kaifali.framesplitter');
}
} catch (_) {}
converter = new VideoToFramesConverter();
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
// IPC Handlers
ipcMain.handle('get-downloads-path', async () => {
// Get the Downloads folder path
return app.getPath('downloads');
});
ipcMain.handle('select-video-file', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'Video Files', extensions: ['mp4', 'avi', 'mov', 'mkv', 'wmv', 'flv', 'webm'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
}
return null;
});
ipcMain.handle('select-output-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory', 'createDirectory']
});
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
}
return null;
});
ipcMain.handle('get-video-info', async (event, videoPath) => {
try {
const metadata = await converter.getVideoMetadata(videoPath);
return { success: true, data: metadata };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('convert-video', async (event, videoPath, outputPath, options = {}) => {
try {
// Create a custom converter instance for this conversion to handle progress
const progressConverter = new VideoToFramesConverter();
// Apply custom settings if provided
if (options.frameCount) {
progressConverter.setTargetFrameCount(options.frameCount);
} else if (options.autoFrameCount) {
progressConverter.enableAutoFrameCount();
}
if (options.frameRate) {
progressConverter.setCustomFrameRate(options.frameRate);
}
// Override the progress tracking
let progressCallback = null;
const originalExtractFrame = progressConverter.extractFrame;
progressConverter.extractFrame = async function(videoPath, timestamp, outputPath, metadata) {
const result = await originalExtractFrame.call(this, videoPath, timestamp, outputPath, metadata);
if (progressCallback) {
progressCallback();
}
return result;
};
// Get metadata to determine total frames
const metadata = await progressConverter.getVideoMetadata(videoPath);
let totalFrames = options.frameCount ||
(options.autoFrameCount ? progressConverter.calculateAutoFrameCount(metadata) : 300);
// Set up progress tracking
let currentFrame = 0;
progressCallback = () => {
currentFrame++;
mainWindow.webContents.send('conversion-progress', {
current: currentFrame,
total: totalFrames,
percent: Math.round((currentFrame / totalFrames) * 100)
});
};
const result = await progressConverter.convertVideo(videoPath, outputPath, options);
return { success: true, data: result };
} catch (error) {
return { success: false, error: error.message };
}
});
// Handle file drops
ipcMain.handle('handle-file-drop', async (event, filePath) => {
try {
const metadata = await converter.getVideoMetadata(filePath);
return { success: true, data: { filePath, metadata } };
} catch (error) {
return { success: false, error: error.message };
}
});