@ponderdev/justfits 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +240 -2
- package/THIRD-PARTY-NOTICES.md +120 -0
- package/agents/skills/justfits/SKILL.md +102 -0
- package/licenses/ffmpeg-license.txt +127 -0
- package/licenses/gpl-2.0.txt +338 -0
- package/licenses/gpl-3.0.txt +674 -0
- package/package.json +51 -4
- package/scripts/link-skill.cjs +99 -0
- package/src/binaries.cjs +106 -0
- package/src/cli.cjs +603 -0
- package/src/engine.cjs +358 -0
- package/src/heif.cjs +156 -0
- package/src/kit/scan.cjs +105 -0
- package/src/kit/window.cjs +93 -0
- package/src/main.cjs +265 -0
- package/src/plan.js +257 -0
- package/src/preload.cjs +35 -0
- package/src/renderer/app.js +635 -0
- package/src/renderer/index.html +57 -0
- package/src/renderer/kit/kit.css +767 -0
- package/src/renderer/kit/kit.js +740 -0
- package/src/renderer/licenses.html +1204 -0
- package/src/renderer/styles.css +9 -0
- package/src/scan.cjs +43 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* PonderDev kit 1.0.0, built from kit/src. Do not edit: run bin/kit-sync. */
|
|
2
|
+
'use strict';
|
|
3
|
+
/*
|
|
4
|
+
* kit, main side: every Just tool window's frame (decided 2026-11-20).
|
|
5
|
+
*
|
|
6
|
+
* On Windows and Linux the app draws its own title bar in its own dark
|
|
7
|
+
* background, with the system's minimise, maximise and close buttons drawn
|
|
8
|
+
* over it in matching colours (Electron's titleBarOverlay), so no tool shows
|
|
9
|
+
* a white frame around a dark app. On a Mac the traffic lights sit over the
|
|
10
|
+
* page as before (hiddenInset).
|
|
11
|
+
*
|
|
12
|
+
* const { frame, dress } = require('./kit/window.cjs');
|
|
13
|
+
* win = new BrowserWindow({ width: 720, height: 780, ...frame(), webPreferences: {...} });
|
|
14
|
+
* dress(win);
|
|
15
|
+
*
|
|
16
|
+
* frame() gives the window's options: the frame, and backgroundColor (the
|
|
17
|
+
* kit's --bg, so nothing flashes white while the page loads). dress(win) puts
|
|
18
|
+
* the strip the title bar is drawn on into every page the window loads, a
|
|
19
|
+
* page of the tool's own or one without the kit (the licences page): the
|
|
20
|
+
* window's title on the left, room for the system buttons on the right, and
|
|
21
|
+
* the whole strip drags the window. A page that already has a .titlebar
|
|
22
|
+
* keeps it, and gets the title written into it.
|
|
23
|
+
*
|
|
24
|
+
* On Linux frame() also gives the window the tool's own name as its app id
|
|
25
|
+
* ("Just Cut"). A Linux desktop names a window it has no menu entry for by its
|
|
26
|
+
* app id alone (GNOME's dock does), so an AppImage run straight from Downloads
|
|
27
|
+
* shows "Just Cut" there, not "com.ponderdev.justcut". A menu entry finds the
|
|
28
|
+
* window by StartupWMClass, which the tool's Linux build sets to the same name.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
// The kit's --bg and --text (kit/src/tokens.css; the kit's tests compare them).
|
|
32
|
+
const BG = '#12131a';
|
|
33
|
+
const TEXT = '#eceef5';
|
|
34
|
+
const MUTED = '#9aa0b4';
|
|
35
|
+
// The strip's height on Windows and Linux: the system buttons' own height.
|
|
36
|
+
const HEIGHT = 32;
|
|
37
|
+
|
|
38
|
+
// Electron reads the app id from the desktop name (minus ".desktop") each
|
|
39
|
+
// time it makes a window, so this has to run before the first window: frame()
|
|
40
|
+
// is called in the window's options. Only in Electron's main process.
|
|
41
|
+
function linuxName(app, platform = process.platform) {
|
|
42
|
+
if (platform !== 'linux' || !app || typeof app.setDesktopName !== 'function') return;
|
|
43
|
+
app.setDesktopName(`${app.getName()}.desktop`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function frame(platform = process.platform) {
|
|
47
|
+
if (platform === 'linux' && process.type === 'browser') linuxName(require('electron').app, platform);
|
|
48
|
+
if (platform === 'darwin') return { titleBarStyle: 'hiddenInset', backgroundColor: BG };
|
|
49
|
+
return {
|
|
50
|
+
titleBarStyle: 'hidden',
|
|
51
|
+
titleBarOverlay: { color: BG, symbolColor: TEXT, height: HEIGHT },
|
|
52
|
+
backgroundColor: BG,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// The strip, for any page. env(titlebar-area-*) is where the page may draw
|
|
57
|
+
// beside the system buttons; the fallbacks are for a window without them.
|
|
58
|
+
const CSS = `
|
|
59
|
+
.titlebar { height: ${HEIGHT}px; flex: none; display: flex; align-items: center; -webkit-app-region: drag;
|
|
60
|
+
background: ${BG}; position: relative; z-index: 10; }
|
|
61
|
+
.titlebar .k-titlebar-name { padding-left: calc(env(titlebar-area-x, 0px) + 14px); max-width: env(titlebar-area-width, 100%);
|
|
62
|
+
font: 12px/1 system-ui, -apple-system, "Segoe UI", Ubuntu, sans-serif; color: ${MUTED};
|
|
63
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; user-select: none; }
|
|
64
|
+
body.k-doc > .titlebar, body > .titlebar:first-child { margin: 0; }
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
// Runs in the page: a .titlebar first in the body (made if the page has none),
|
|
68
|
+
// with the window's title in it. DOM calls only, so a page's own
|
|
69
|
+
// Content-Security-Policy (no scripts, no inline styles) does not stop it.
|
|
70
|
+
const PUT = `(() => {
|
|
71
|
+
let bar = document.querySelector('.titlebar');
|
|
72
|
+
if (!bar) { bar = document.createElement('div'); bar.className = 'titlebar'; document.body.prepend(bar); }
|
|
73
|
+
if (!bar.querySelector('.k-titlebar-name')) {
|
|
74
|
+
const name = document.createElement('span');
|
|
75
|
+
name.className = 'k-titlebar-name';
|
|
76
|
+
bar.append(name);
|
|
77
|
+
}
|
|
78
|
+
bar.querySelector('.k-titlebar-name').textContent = document.title;
|
|
79
|
+
return true;
|
|
80
|
+
})()`;
|
|
81
|
+
|
|
82
|
+
function dress(win, platform = process.platform) {
|
|
83
|
+
if (platform === 'darwin') return;
|
|
84
|
+
const contents = win.webContents;
|
|
85
|
+
contents.on('dom-ready', () => {
|
|
86
|
+
contents.insertCSS(CSS).catch(() => {});
|
|
87
|
+
contents.executeJavaScript(PUT).catch(() => {});
|
|
88
|
+
});
|
|
89
|
+
// A page that changes its title (a file's name, say) keeps the strip true.
|
|
90
|
+
win.on('page-title-updated', () => { contents.executeJavaScript(PUT).catch(() => {}); });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { frame, dress, linuxName, BG, TEXT, HEIGHT, CSS };
|
package/src/main.cjs
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Just Fits: makes a file small enough to send.
|
|
5
|
+
*
|
|
6
|
+
* This file is only wiring. The arithmetic is in plan.js and the ffmpeg work
|
|
7
|
+
* is in engine.cjs, both of which run without Electron so they can be tested
|
|
8
|
+
* directly.
|
|
9
|
+
*
|
|
10
|
+
* Two rules hold throughout: the original file is only ever read, and the app
|
|
11
|
+
* never touches the network.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const { app, BrowserWindow, screen, ipcMain, dialog, shell } = require('electron');
|
|
15
|
+
// The frame: a dark title bar of the app's own on Windows and Linux, the traffic lights on a Mac.
|
|
16
|
+
const { frame, dress } = require('./kit/window.cjs');
|
|
17
|
+
|
|
18
|
+
const path = require('node:path');
|
|
19
|
+
|
|
20
|
+
// The tool's own version, from its package.json. Not app.getVersion(): run by
|
|
21
|
+
// a test or a harness, Electron reports its own version instead.
|
|
22
|
+
const APP_VERSION = (() => {
|
|
23
|
+
try { return require(path.join(__dirname, '..', 'package.json')).version; } catch (e) { return app.getVersion(); }
|
|
24
|
+
})();
|
|
25
|
+
const fs = require('node:fs');
|
|
26
|
+
|
|
27
|
+
const engine = require('./engine.cjs');
|
|
28
|
+
const plan = require('./plan.js');
|
|
29
|
+
const binaries = require('./binaries.cjs');
|
|
30
|
+
const { scan, places } = require('./scan.cjs');
|
|
31
|
+
|
|
32
|
+
let win = null;
|
|
33
|
+
let ctl = null;
|
|
34
|
+
let licenceWindow = null;
|
|
35
|
+
|
|
36
|
+
// Windows starts an Open window in Downloads, which is usually empty and,
|
|
37
|
+
// filtered to video and images, shows nothing at all. So it starts where the
|
|
38
|
+
// last file came from instead, by dialog or by drop, else in Documents.
|
|
39
|
+
let lastOpenDir = null;
|
|
40
|
+
|
|
41
|
+
/* --- the bundled binaries --------------------------------------------- */
|
|
42
|
+
|
|
43
|
+
function vendorDir() {
|
|
44
|
+
return app.isPackaged
|
|
45
|
+
? path.join(process.resourcesPath, 'ffmpeg')
|
|
46
|
+
: path.join(__dirname, '..', 'vendor', 'ffmpeg');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The app must never fall back to a system ffmpeg: someone who downloaded a
|
|
50
|
+
// finished app should not discover it needs a video encoder installing. The
|
|
51
|
+
// command line is the opposite and does fall back, which is why the two share
|
|
52
|
+
// a resolver with a flag rather than each having their own.
|
|
53
|
+
function binary(name) {
|
|
54
|
+
return binaries.resolve(name, [vendorDir()], false);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* --- wiring ------------------------------------------------------------ */
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
* Exported so test/window.test.cjs can register the same handlers against its
|
|
61
|
+
* own window. Without that the test would be checking a renderer that has
|
|
62
|
+
* nothing to talk to, and would miss a handler going missing here.
|
|
63
|
+
*/
|
|
64
|
+
// The folder each gathered file came in with (null for a file on its own),
|
|
65
|
+
// and where the current run is putting results (scan.cjs places).
|
|
66
|
+
const gathered = new Map();
|
|
67
|
+
let placing = null;
|
|
68
|
+
|
|
69
|
+
function registerHandlers() {
|
|
70
|
+
ipcMain.handle('targets', () => plan.TARGETS);
|
|
71
|
+
|
|
72
|
+
ipcMain.handle('probe', async (_e, file) => {
|
|
73
|
+
let ffprobe;
|
|
74
|
+
try {
|
|
75
|
+
ffprobe = binary('ffprobe');
|
|
76
|
+
} catch (err) {
|
|
77
|
+
return { ok: false, message: String(err.message || err) };
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const info = await engine.probe(ffprobe, file);
|
|
81
|
+
lastOpenDir = path.dirname(file);
|
|
82
|
+
return { ok: true, info };
|
|
83
|
+
} catch (err) {
|
|
84
|
+
// Read fine but refused on purpose: its own sentence already names the file.
|
|
85
|
+
if (err.code === 'animated-webp') return { ok: false, message: err.message };
|
|
86
|
+
return { ok: false, message: probeMessage(file, String(err.message || err)) };
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// run: a number the window gives each press of Compress, so a folder's
|
|
91
|
+
// results folder is chosen once per run ("Holiday (fitted)", then
|
|
92
|
+
// "Holiday (fitted) 2" the next time). Which folder a file came in with is
|
|
93
|
+
// main's own record from gather, never the page's word.
|
|
94
|
+
ipcMain.handle('fit', async (_e, { file, targetBytes, beside, run }) => {
|
|
95
|
+
ctl = engine.control();
|
|
96
|
+
const onProgress = (p) => {
|
|
97
|
+
if (win && !win.isDestroyed()) win.webContents.send('progress', p);
|
|
98
|
+
};
|
|
99
|
+
let where = null;
|
|
100
|
+
try {
|
|
101
|
+
const info = await engine.probe(binary('ffprobe'), file);
|
|
102
|
+
if (!placing || placing.run !== run || placing.beside !== Boolean(beside)) placing = { run, beside: Boolean(beside), place: places({ beside }) };
|
|
103
|
+
where = placing.place({ path: path.resolve(file), from: gathered.get(path.resolve(file)) || null });
|
|
104
|
+
if (where.outDir) await fs.promises.mkdir(where.outDir, { recursive: true });
|
|
105
|
+
// Already small enough, in a folder's results folder: copied in under its
|
|
106
|
+
// -fits name, so the results folder holds the whole folder (decided 2026-11-20).
|
|
107
|
+
if (where.folder && info.bytes <= targetBytes) {
|
|
108
|
+
const to = engine.outputPath(path.join(where.outDir, path.basename(file)), path.extname(file), fs.existsSync, where.suffix);
|
|
109
|
+
await fs.promises.copyFile(file, to, fs.constants.COPYFILE_EXCL);
|
|
110
|
+
return { ok: true, copied: true, path: to, bytes: info.bytes, before: info.bytes, kind: info.kind, folder: where.folder };
|
|
111
|
+
}
|
|
112
|
+
const args = { ffmpeg: binary('ffmpeg'), file, info, targetBytes, onProgress, ctl, outDir: where.outDir, suffix: where.suffix };
|
|
113
|
+
const result = info.kind === 'image' ? await engine.fitImage(args) : await engine.fitVideo(args);
|
|
114
|
+
return { ok: true, ...result, before: info.bytes, kind: info.kind, folder: where.folder, note: info.kind === 'video' ? plan.lengthNote(result.plan) : '' };
|
|
115
|
+
} catch (err) {
|
|
116
|
+
// Stopped or failed: a results folder made for this file and left empty
|
|
117
|
+
// goes again, up to the results folder itself, so nothing empty is left.
|
|
118
|
+
if (where && where.folder) {
|
|
119
|
+
for (let dir = where.outDir; dir.startsWith(where.folder); dir = path.dirname(dir)) {
|
|
120
|
+
try { await fs.promises.rmdir(dir); } catch { break; }
|
|
121
|
+
if (dir === where.folder) break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (err && err.cancelled) return { ok: false, cancelled: true };
|
|
125
|
+
return { ok: false, message: String(err.message || err) };
|
|
126
|
+
} finally {
|
|
127
|
+
ctl = null;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
ipcMain.handle('cancel', () => {
|
|
132
|
+
if (!ctl) return false;
|
|
133
|
+
ctl.cancel();
|
|
134
|
+
return true;
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
ipcMain.handle('reveal', (_e, file) => shell.showItemInFolder(file));
|
|
138
|
+
|
|
139
|
+
// Opens in the real browser, never in an app window. The addresses are fixed
|
|
140
|
+
// here; the page never hands main a URL to open.
|
|
141
|
+
ipcMain.handle('open-site', () => shell.openExternal('https://ponderdev.com'));
|
|
142
|
+
ipcMain.handle('open-how-it-works', () => shell.openExternal('https://ponderdev.com/tools/just-fits/how-it-works'));
|
|
143
|
+
|
|
144
|
+
// The skill that ships with the app, for the start screen's Copy skill. Read
|
|
145
|
+
// beside the app's own files, so it is the same text inside the packaged app
|
|
146
|
+
// (electron-builder packs agents/) as from source.
|
|
147
|
+
ipcMain.handle('skill-text', () => fs.promises.readFile(path.join(__dirname, '..', 'agents', 'skills', 'justfits', 'SKILL.md'), 'utf8'));
|
|
148
|
+
|
|
149
|
+
// The GPL wants its notices readable by whoever has the app, not just by
|
|
150
|
+
// whoever finds the repo, so they open in a window of their own.
|
|
151
|
+
ipcMain.handle('open-licences', () => {
|
|
152
|
+
if (licenceWindow && !licenceWindow.isDestroyed()) return licenceWindow.focus();
|
|
153
|
+
licenceWindow = new BrowserWindow({
|
|
154
|
+
width: 720,
|
|
155
|
+
height: 680,
|
|
156
|
+
title: 'Licences and credits',
|
|
157
|
+
...frame(),
|
|
158
|
+
webPreferences: { contextIsolation: true, nodeIntegration: false },
|
|
159
|
+
});
|
|
160
|
+
dress(licenceWindow);
|
|
161
|
+
licenceWindow.setMenuBarVisibility(false);
|
|
162
|
+
licenceWindow.loadFile(path.join(__dirname, 'renderer', 'licenses.html'));
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Files and folders, several at once. Windows and Linux cannot offer files
|
|
166
|
+
// and folders in one window (asked for both, they show a folder picker), so
|
|
167
|
+
// there it picks files, and the start screen's Choose a folder (pick-folder)
|
|
168
|
+
// brings a folder in.
|
|
169
|
+
ipcMain.handle('pick-file', async () => {
|
|
170
|
+
const r = await dialog.showOpenDialog(win, {
|
|
171
|
+
defaultPath: lastOpenDir || app.getPath('documents'),
|
|
172
|
+
properties: process.platform === 'darwin' ? ['openFile', 'openDirectory', 'multiSelections'] : ['openFile', 'multiSelections'],
|
|
173
|
+
filters: [{
|
|
174
|
+
name: 'Video and images',
|
|
175
|
+
extensions: ['mp4', 'mov', 'mkv', 'webm', 'avi', 'm4v', 'jpg', 'jpeg', 'png', 'heic', 'webp', 'gif'],
|
|
176
|
+
}],
|
|
177
|
+
});
|
|
178
|
+
if (r.canceled || !r.filePaths.length) return [];
|
|
179
|
+
lastOpenDir = path.dirname(r.filePaths[0]);
|
|
180
|
+
return r.filePaths;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Folders only, for Windows and Linux, where pick-file cannot offer them.
|
|
184
|
+
ipcMain.handle('pick-folder', async () => {
|
|
185
|
+
const r = await dialog.showOpenDialog(win, {
|
|
186
|
+
defaultPath: lastOpenDir || app.getPath('documents'),
|
|
187
|
+
properties: ['openDirectory', 'multiSelections'],
|
|
188
|
+
});
|
|
189
|
+
if (r.canceled || !r.filePaths.length) return [];
|
|
190
|
+
lastOpenDir = r.filePaths[0];
|
|
191
|
+
return r.filePaths;
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// What several paths, or a folder, hold (scan.cjs). single: one path that is
|
|
195
|
+
// not a folder, which the window takes the way it takes one file.
|
|
196
|
+
ipcMain.handle('gather', async (_e, paths) => {
|
|
197
|
+
const list = Array.isArray(paths) ? paths.filter((p) => typeof p === 'string' && p) : [];
|
|
198
|
+
const found = await scan(list);
|
|
199
|
+
for (const f of found.files) gathered.set(f.path, f.from);
|
|
200
|
+
if (found.files[0]) lastOpenDir = path.dirname(found.files[0].path);
|
|
201
|
+
return { ...found, single: list.length === 1 && found.folders === 0 };
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ffprobe's reason starts with the whole path ("C:\Users\...\clip.mp4:
|
|
206
|
+
// Invalid data found when processing input"), which in the window is long,
|
|
207
|
+
// technical and wraps across the error box. The window says it as a sentence
|
|
208
|
+
// that names the file. The command line keeps ffprobe's words, for scripts.
|
|
209
|
+
function probeMessage(file, reason) {
|
|
210
|
+
const name = path.basename(String(file || ''));
|
|
211
|
+
if (/no such file/i.test(reason)) return `Could not find ${name}. It may have been moved or deleted.`;
|
|
212
|
+
if (/permission denied|operation not permitted/i.test(reason)) return `Just Fits is not allowed to open ${name}.`;
|
|
213
|
+
return `${name} is not a video or picture that Just Fits can read.`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
// A laptop at 1366x768 has about 728 pixels of height left for a window once
|
|
218
|
+
// the taskbar (or the menu bar and Dock) is taken off, and a window opened
|
|
219
|
+
// taller than that put the bottom of the screen, the main button included,
|
|
220
|
+
// out of reach under the taskbar. So the window, and its minimum, are fitted
|
|
221
|
+
// to the work area of the screen it opens on.
|
|
222
|
+
function fitToScreen(opts) {
|
|
223
|
+
const area = screen.getPrimaryDisplay().workAreaSize;
|
|
224
|
+
const fit = { ...opts };
|
|
225
|
+
for (const [key, side] of [['width', 'width'], ['height', 'height'], ['minWidth', 'width'], ['minHeight', 'height']]) {
|
|
226
|
+
if (typeof fit[key] === 'number') fit[key] = Math.min(fit[key], area[side]);
|
|
227
|
+
}
|
|
228
|
+
return fit;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function createWindow() {
|
|
232
|
+
win = new BrowserWindow(fitToScreen({
|
|
233
|
+
width: 720,
|
|
234
|
+
height: 780,
|
|
235
|
+
minWidth: 560,
|
|
236
|
+
minHeight: 640,
|
|
237
|
+
...frame(),
|
|
238
|
+
webPreferences: {
|
|
239
|
+
preload: path.join(__dirname, 'preload.cjs'),
|
|
240
|
+
contextIsolation: true,
|
|
241
|
+
nodeIntegration: false,
|
|
242
|
+
// The version, from package.json, for the start screen and the footer.
|
|
243
|
+
// A sandboxed preload cannot read package.json, so it comes on argv.
|
|
244
|
+
additionalArguments: ['--app-version=' + APP_VERSION],
|
|
245
|
+
},
|
|
246
|
+
}));
|
|
247
|
+
dress(win);
|
|
248
|
+
win.setMenuBarVisibility(false);
|
|
249
|
+
win.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Guarded so requiring this file from a test registers the handlers without
|
|
253
|
+
// also opening a window. An env var rather than require.main, because under
|
|
254
|
+
// `electron .` from source Electron does not make this file require.main, so
|
|
255
|
+
// that check opened nothing.
|
|
256
|
+
if (process.env.JUST_TEST_NO_WINDOW !== '1') {
|
|
257
|
+
registerHandlers();
|
|
258
|
+
app.whenReady().then(createWindow);
|
|
259
|
+
app.on('window-all-closed', () => app.quit());
|
|
260
|
+
app.on('activate', () => {
|
|
261
|
+
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
module.exports = { registerHandlers, binary, probeMessage };
|
package/src/plan.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* The arithmetic that decides how to make a file fit.
|
|
5
|
+
*
|
|
6
|
+
* Pure functions. No ffmpeg, no filesystem, no Electron. Whether your video
|
|
7
|
+
* comes in under the line is the one thing this app has to get right, so the
|
|
8
|
+
* maths behind it is testable directly rather than trusted by eye.
|
|
9
|
+
*
|
|
10
|
+
* Everything here works in kbps and bytes. The caller turns the result into
|
|
11
|
+
* ffmpeg arguments.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Decimal megabytes, not mebibytes. Services are vague about which they mean,
|
|
15
|
+
// and being wrong in this direction leaves us slightly under the limit rather
|
|
16
|
+
// than slightly over it, which is the only direction that matters.
|
|
17
|
+
const MB = 1000 * 1000;
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* Where people actually get stuck, with the real numbers.
|
|
21
|
+
*
|
|
22
|
+
* Checked August 2026. They move, so they live in one list rather than
|
|
23
|
+
* scattered through the app, and the custom box is always there for when a
|
|
24
|
+
* platform changes its mind.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// Email attachments are base64 encoded in transit, which inflates them by
|
|
28
|
+
// about 37 percent. A provider advertising 25MB will bounce a 25MB file, so
|
|
29
|
+
// the ones marked `encoded` aim at the size the file has to be before that
|
|
30
|
+
// happens rather than the number on the tin.
|
|
31
|
+
const BASE64 = 1.37;
|
|
32
|
+
|
|
33
|
+
const TARGETS = [
|
|
34
|
+
{ id: 'discord', group: 'Discord', label: 'Discord, free account', mb: 10,
|
|
35
|
+
note: 'The usual one. A boosted server raises it for everyone in that server.' },
|
|
36
|
+
{ id: 'discord-basic', group: 'Discord', label: 'Discord Nitro Basic', mb: 50 },
|
|
37
|
+
{ id: 'discord-boost3', group: 'Discord', label: 'Discord, level 3 boosted server', mb: 100,
|
|
38
|
+
note: 'Server boosts count even if you have no Nitro yourself.' },
|
|
39
|
+
{ id: 'discord-nitro', group: 'Discord', label: 'Discord Nitro', mb: 500 },
|
|
40
|
+
|
|
41
|
+
{ id: 'gmail', group: 'Email', label: 'Gmail', limitMb: 25, encoded: true },
|
|
42
|
+
{ id: 'outlook', group: 'Email', label: 'Outlook and Microsoft 365', limitMb: 20, encoded: true },
|
|
43
|
+
|
|
44
|
+
{ id: 'whatsapp', group: 'Messaging', label: 'WhatsApp video', mb: 16,
|
|
45
|
+
note: 'Sending it as a document instead allows up to 2GB.' },
|
|
46
|
+
{ id: 'signal', group: 'Messaging', label: 'Signal', mb: 100 },
|
|
47
|
+
{ id: 'imessage', group: 'Messaging', label: 'iMessage', mb: 100 },
|
|
48
|
+
{ id: 'slack', group: 'Messaging', label: 'Slack', mb: 1000 },
|
|
49
|
+
{ id: 'telegram', group: 'Messaging', label: 'Telegram', mb: 2000 },
|
|
50
|
+
].map((t) => ({
|
|
51
|
+
...t,
|
|
52
|
+
bytes: t.encoded ? Math.floor((t.limitMb * MB) / BASE64) : t.mb * MB,
|
|
53
|
+
note: t.note || (t.encoded
|
|
54
|
+
? `${t.limitMb}MB limit, but base64 encoding adds about 37% in transit, so the file has to be smaller than that.`
|
|
55
|
+
: ''),
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
// mp4 spends a couple of percent on headers, indexes and muxing that the
|
|
59
|
+
// bitrate maths cannot see, and x264 does not hit a requested bitrate exactly.
|
|
60
|
+
// Aim under the line rather than at it.
|
|
61
|
+
const HEADROOM = 0.95;
|
|
62
|
+
|
|
63
|
+
/*
|
|
64
|
+
* How much of the budget goes to sound.
|
|
65
|
+
*
|
|
66
|
+
* Speech stays intelligible far below the point where video falls apart, so
|
|
67
|
+
* audio gives way first. The last two rungs are mono, because at that budget
|
|
68
|
+
* one clean channel beats two muddy ones.
|
|
69
|
+
*/
|
|
70
|
+
const AUDIO_LADDER = [
|
|
71
|
+
{ from: 700, kbps: 128, channels: 2 },
|
|
72
|
+
{ from: 400, kbps: 96, channels: 2 },
|
|
73
|
+
{ from: 200, kbps: 64, channels: 2 },
|
|
74
|
+
{ from: 110, kbps: 48, channels: 1 },
|
|
75
|
+
{ from: 0, kbps: 32, channels: 1 },
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
// Ladder rungs are the short edge, so a portrait clip steps down the same way
|
|
79
|
+
// a landscape one does.
|
|
80
|
+
const SHORT_EDGES = [2160, 1440, 1080, 720, 540, 432, 360, 270];
|
|
81
|
+
|
|
82
|
+
// Bits per pixel per frame.
|
|
83
|
+
//
|
|
84
|
+
// 0.08 is the low end of what YouTube recommends for an upload at a given
|
|
85
|
+
// resolution, and it is the bar a rung has to clear to be chosen. It was 0.09
|
|
86
|
+
// at first, which pushed a 30 second 1080p clip into a 10MB target down to
|
|
87
|
+
// 540p when 720p had 2400kbps available: comfortably inside YouTube's own
|
|
88
|
+
// 2500-4000 range for 720p30. Below 0.05 h264 smears on anything that moves,
|
|
89
|
+
// so that is the point where a smaller frame genuinely does look better.
|
|
90
|
+
const GOOD_BPP = 0.08;
|
|
91
|
+
const MIN_BPP = 0.05;
|
|
92
|
+
|
|
93
|
+
// Under this there is no point pretending. The caller warns the user.
|
|
94
|
+
const FLOOR_KBPS = 60;
|
|
95
|
+
|
|
96
|
+
function even(n) {
|
|
97
|
+
return Math.max(2, Math.round(n / 2) * 2);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Scale to a short edge, never upwards, always to even dimensions. */
|
|
101
|
+
function scaleToShortEdge(srcW, srcH, edge) {
|
|
102
|
+
const src = Math.min(srcW, srcH);
|
|
103
|
+
if (!src || edge >= src) return { width: even(srcW), height: even(srcH) };
|
|
104
|
+
const k = edge / src;
|
|
105
|
+
return { width: even(srcW * k), height: even(srcH * k) };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function audioFor(totalKbps, hasAudio) {
|
|
109
|
+
if (!hasAudio) return { kbps: 0, channels: 0 };
|
|
110
|
+
const rung = AUDIO_LADDER.find((r) => totalKbps >= r.from) || AUDIO_LADDER[AUDIO_LADDER.length - 1];
|
|
111
|
+
// Never let sound eat more than a quarter of the budget. Below 16kbps AAC
|
|
112
|
+
// is not worth encoding, so a budget that tight is hopeless anyway and
|
|
113
|
+
// planVideo flags it as such rather than this quietly papering over it.
|
|
114
|
+
const cap = Math.floor(totalKbps * 0.25);
|
|
115
|
+
return { kbps: Math.max(16, Math.min(rung.kbps, cap)), channels: rung.channels };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function bppFor(videoKbps, width, height, fps) {
|
|
119
|
+
return (videoKbps * 1000) / (width * height * fps);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Pick the largest frame that still gets enough bits, dropping frame rate
|
|
124
|
+
* before resolution gets silly.
|
|
125
|
+
*/
|
|
126
|
+
function pickFrame(videoKbps, srcW, srcH, srcFps) {
|
|
127
|
+
const rates = srcFps > 32 ? [srcFps, 30] : [srcFps];
|
|
128
|
+
const shortEdge = Math.min(srcW, srcH);
|
|
129
|
+
// Leaving the source alone is always the first option. Without this a clip
|
|
130
|
+
// whose height falls between two rungs gets shrunk for no reason at all.
|
|
131
|
+
const rungs = [shortEdge, ...SHORT_EDGES.filter((e) => e < shortEdge)];
|
|
132
|
+
|
|
133
|
+
for (const bar of [GOOD_BPP, MIN_BPP]) {
|
|
134
|
+
for (const fps of rates) {
|
|
135
|
+
for (const edge of rungs) {
|
|
136
|
+
const { width, height } = scaleToShortEdge(srcW, srcH, edge);
|
|
137
|
+
if (bppFor(videoKbps, width, height, fps) >= bar) {
|
|
138
|
+
return { width, height, fps, rough: bar === MIN_BPP };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Nothing clears even the low bar. Take the smallest rung and be honest
|
|
145
|
+
// about it rather than shipping a plan that pretends otherwise.
|
|
146
|
+
const edge = rungs[rungs.length - 1];
|
|
147
|
+
const { width, height } = scaleToShortEdge(srcW, srcH, edge);
|
|
148
|
+
return { width, height, fps: Math.min(srcFps, 30), rough: true };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Work out one encode.
|
|
153
|
+
*
|
|
154
|
+
* `correction` carries what the previous attempt actually produced, so a
|
|
155
|
+
* second pass can aim lower without starting the reasoning again.
|
|
156
|
+
*/
|
|
157
|
+
function planVideo({ durationSec, targetBytes, hasAudio = true, width, height, fps = 30, correction = 1 }) {
|
|
158
|
+
if (!(durationSec > 0)) throw new Error('Cannot plan an encode without a duration');
|
|
159
|
+
if (!(targetBytes > 0)) throw new Error('Cannot plan an encode without a target size');
|
|
160
|
+
|
|
161
|
+
const budgetBytes = targetBytes * HEADROOM * correction;
|
|
162
|
+
const totalKbps = (budgetBytes * 8) / durationSec / 1000;
|
|
163
|
+
|
|
164
|
+
const audio = audioFor(totalKbps, hasAudio);
|
|
165
|
+
const videoKbps = Math.floor(totalKbps - audio.kbps);
|
|
166
|
+
|
|
167
|
+
// Too long for the target at any quality worth producing.
|
|
168
|
+
if (videoKbps < FLOOR_KBPS) {
|
|
169
|
+
const frame = pickFrame(Math.max(videoKbps, 1), width, height, fps);
|
|
170
|
+
return {
|
|
171
|
+
videoKbps: Math.max(videoKbps, 20),
|
|
172
|
+
audioKbps: audio.kbps,
|
|
173
|
+
audioChannels: audio.channels,
|
|
174
|
+
...frame,
|
|
175
|
+
rough: true,
|
|
176
|
+
hopeless: true,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const frame = pickFrame(videoKbps, width, height, fps);
|
|
181
|
+
return {
|
|
182
|
+
videoKbps,
|
|
183
|
+
audioKbps: audio.kbps,
|
|
184
|
+
audioChannels: audio.channels,
|
|
185
|
+
...frame,
|
|
186
|
+
hopeless: false,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* After an encode lands, how much to scale the next attempt by.
|
|
192
|
+
*
|
|
193
|
+
* x264 overshoots on hard content and undershoots on easy content, so rather
|
|
194
|
+
* than guessing better we measure and correct. The extra 2 percent stops a
|
|
195
|
+
* near miss from needing a third go.
|
|
196
|
+
*/
|
|
197
|
+
function correctionFor(actualBytes, targetBytes, previous = 1) {
|
|
198
|
+
if (!(actualBytes > 0)) return previous;
|
|
199
|
+
const next = previous * (targetBytes / actualBytes) * 0.98;
|
|
200
|
+
// Never chase below a tenth of the original guess: something else is wrong.
|
|
201
|
+
return Math.max(0.1, Math.min(next, previous));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A still image, where quality is a dial rather than a bitrate. */
|
|
205
|
+
function planImage({ targetBytes, width, height, attempt = 0 }) {
|
|
206
|
+
// JPEG quality in ffmpeg's -q:v scale, where 2 is best and 31 is worst.
|
|
207
|
+
const quality = [3, 6, 10, 16, 24][Math.min(attempt, 4)];
|
|
208
|
+
const shrink = [1, 1, 0.8, 0.6, 0.45][Math.min(attempt, 4)];
|
|
209
|
+
return {
|
|
210
|
+
quality,
|
|
211
|
+
width: even(width * shrink),
|
|
212
|
+
height: even(height * shrink),
|
|
213
|
+
lastResort: attempt >= 4,
|
|
214
|
+
targetBytes,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/*
|
|
219
|
+
* Windows opens a device, not a file, for these names with any extension and
|
|
220
|
+
* in any case (con.mp4, NUL, lpt1.txt), so the result would vanish while the
|
|
221
|
+
* command reported success. Refused on every platform, since a script written
|
|
222
|
+
* on a Mac may well be run on Windows. Windows also ignores trailing dots and
|
|
223
|
+
* spaces, so "nul. " is NUL too.
|
|
224
|
+
*/
|
|
225
|
+
function isDeviceName(outPath) {
|
|
226
|
+
if (typeof outPath !== 'string') return false;
|
|
227
|
+
// Windows reads a name only up to its first dot and ignores spaces before
|
|
228
|
+
// it, so "CON .mp4" and "con.take2.mp4" open the device as surely as "CON".
|
|
229
|
+
// The same rule in every tool, so a name refused by one is refused by all.
|
|
230
|
+
const head = outPath.split(/[\\/]/).pop().split('.')[0].replace(/ +$/, '');
|
|
231
|
+
return /^(con|prn|aux|nul|conin\$|conout\$|com[1-9¹²³]|lpt[1-9¹²³])$/i.test(head);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The sentence for a video too long for its target, the same words in the
|
|
235
|
+
// window and on the command line. Empty when the picture will be fine.
|
|
236
|
+
function lengthNote(p, size) {
|
|
237
|
+
if (!p) return '';
|
|
238
|
+
const forSize = size ? `for ${size}` : 'for that size';
|
|
239
|
+
if (p.hopeless) return `This is too long ${forSize}: expect a small, blurry picture, and it may still come out over. A bigger target or a shorter clip would look far better.`;
|
|
240
|
+
if (p.rough) return `This is long ${forSize}, so the picture will be softer than the original.`;
|
|
241
|
+
return '';
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
module.exports = {
|
|
245
|
+
MB,
|
|
246
|
+
lengthNote,
|
|
247
|
+
BASE64,
|
|
248
|
+
TARGETS,
|
|
249
|
+
HEADROOM,
|
|
250
|
+
planVideo,
|
|
251
|
+
planImage,
|
|
252
|
+
correctionFor,
|
|
253
|
+
scaleToShortEdge,
|
|
254
|
+
audioFor,
|
|
255
|
+
bppFor,
|
|
256
|
+
isDeviceName,
|
|
257
|
+
};
|
package/src/preload.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The only surface the page gets. No node, no filesystem, no network.
|
|
4
|
+
const { contextBridge, ipcRenderer, webUtils } = require('electron');
|
|
5
|
+
|
|
6
|
+
// The tool's version, put on argv by main (a sandboxed preload cannot read
|
|
7
|
+
// package.json), for the start screen and the footer.
|
|
8
|
+
const version = (process.argv.find((a) => a.startsWith('--app-version=')) || '').slice('--app-version='.length);
|
|
9
|
+
|
|
10
|
+
contextBridge.exposeInMainWorld('fits', {
|
|
11
|
+
version,
|
|
12
|
+
// Fetched from main rather than required in here: preloads are sandboxed
|
|
13
|
+
// and can only require Electron's own modules. Keeping the sandbox is worth
|
|
14
|
+
// more than the convenience, and it means one copy of the list either way.
|
|
15
|
+
targets: () => ipcRenderer.invoke('targets'),
|
|
16
|
+
probe: (file) => ipcRenderer.invoke('probe', file),
|
|
17
|
+
fit: (file, targetBytes, options = {}) => ipcRenderer.invoke('fit', { file, targetBytes, beside: Boolean(options.beside), run: Number(options.run) || 0 }),
|
|
18
|
+
cancel: () => ipcRenderer.invoke('cancel'),
|
|
19
|
+
reveal: (file) => ipcRenderer.invoke('reveal', file),
|
|
20
|
+
openSite: () => ipcRenderer.invoke('open-site'),
|
|
21
|
+
openHowItWorks: () => ipcRenderer.invoke('open-how-it-works'),
|
|
22
|
+
skillText: () => ipcRenderer.invoke('skill-text'),
|
|
23
|
+
openLicences: () => ipcRenderer.invoke('open-licences'),
|
|
24
|
+
// Several paths at once: files, or folders on a Mac.
|
|
25
|
+
pickFile: () => ipcRenderer.invoke('pick-file'),
|
|
26
|
+
// Folders alone, where the file picker cannot take them (Windows, Linux).
|
|
27
|
+
pickFolder: () => ipcRenderer.invoke('pick-folder'),
|
|
28
|
+
folderButton: process.platform !== 'darwin',
|
|
29
|
+
platform: process.platform,
|
|
30
|
+
gather: (paths) => ipcRenderer.invoke('gather', paths),
|
|
31
|
+
// File.path was removed in Electron 32. Dropped files have to be resolved
|
|
32
|
+
// through webUtils now, which only exists on this side of the bridge.
|
|
33
|
+
pathFor: (file) => webUtils.getPathForFile(file),
|
|
34
|
+
onProgress: (fn) => ipcRenderer.on('progress', (_e, p) => fn(p)),
|
|
35
|
+
});
|