@ponderdev/justfits 1.3.0 → 1.4.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/src/plan.js CHANGED
@@ -30,30 +30,70 @@ const MB = 1000 * 1000;
30
30
  // happens rather than the number on the tin.
31
31
  const BASE64 = 1.37;
32
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
- }));
33
+ /*
34
+ * The sentences this file and the engine say to a person go through
35
+ * planWords.t and planWords.tn, so the window can have them in its language.
36
+ * Until someone hands over other words they are the English, filled in
37
+ * exactly as a template string would be, which is what the command line
38
+ * prints: it stays English on purpose (its output is read by scripts and
39
+ * assistants), so it never calls useWords and its output does not change by a
40
+ * byte. The main side calls plan.useWords(words) with its kit language, so
41
+ * what it sends the window is translated.
42
+ */
43
+ const englishFill = (text, vars) => String(text).replace(/\{(\w+)\}/g, (m, k) => (vars && Object.prototype.hasOwnProperty.call(vars, k) ? String(vars[k]) : m));
44
+ const english = {
45
+ t: (source, vars) => englishFill(source, vars), // strings: passes on
46
+ tn: (n, one, other, vars = {}) => englishFill(n === 1 ? one : other, Object.assign({ n: String(n) }, vars)),
47
+ };
48
+ let planWords = english;
49
+
50
+ /** Hand over the words to say things in: { t, tn }, as kit/lang.cjs gives them. */
51
+ function useWords(w) {
52
+ planWords = w && typeof w.t === 'function' && typeof w.tn === 'function' ? { t: w.t, tn: w.tn } : english;
53
+ return planWords;
54
+ }
55
+
56
+ /** The words in use now, for the engine, which says things through the same hook. */
57
+ function words() {
58
+ return planWords;
59
+ }
60
+
61
+ // The list, said in the words given: TARGETS below is it in English, for the
62
+ // command line; targets() is it in the words handed over, for the window.
63
+ // Brand names (Gmail, Signal, Discord Nitro) are not sentences, so they stay.
64
+ function targetList(w) {
65
+ return [
66
+ { id: 'discord', group: 'Discord', label: w.t('Discord, free account'), mb: 10,
67
+ note: w.t('The usual one. A boosted server raises it for everyone in that server.') },
68
+ { id: 'discord-basic', group: 'Discord', label: 'Discord Nitro Basic', mb: 50 },
69
+ { id: 'discord-boost3', group: 'Discord', label: w.t('Discord, level 3 boosted server'), mb: 100,
70
+ note: w.t('Server boosts count even if you have no Nitro yourself.') },
71
+ { id: 'discord-nitro', group: 'Discord', label: 'Discord Nitro', mb: 500 },
72
+
73
+ { id: 'gmail', group: w.t('Email'), label: 'Gmail', limitMb: 25, encoded: true },
74
+ { id: 'outlook', group: w.t('Email'), label: w.t('Outlook and Microsoft 365'), limitMb: 20, encoded: true },
75
+
76
+ { id: 'whatsapp', group: w.t('Messaging'), label: w.t('WhatsApp video'), mb: 16,
77
+ note: w.t('Sending it as a document instead allows up to 2GB.') },
78
+ { id: 'signal', group: w.t('Messaging'), label: 'Signal', mb: 100 },
79
+ { id: 'imessage', group: w.t('Messaging'), label: 'iMessage', mb: 100 },
80
+ { id: 'slack', group: w.t('Messaging'), label: 'Slack', mb: 1000 },
81
+ { id: 'telegram', group: w.t('Messaging'), label: 'Telegram', mb: 2000 },
82
+ ].map((t) => ({
83
+ ...t,
84
+ bytes: t.encoded ? Math.floor((t.limitMb * MB) / BASE64) : t.mb * MB,
85
+ note: t.note || (t.encoded
86
+ ? w.t('{limit}MB limit, but base64 encoding adds about 37% in transit, so the file has to be smaller than that.', { limit: t.limitMb })
87
+ : ''),
88
+ }));
89
+ }
90
+
91
+ const TARGETS = targetList(english);
92
+
93
+ /** The targets in the words handed over (the window's language). */
94
+ function targets() {
95
+ return targetList(planWords);
96
+ }
57
97
 
58
98
  // mp4 spends a couple of percent on headers, indexes and muxing that the
59
99
  // bitrate maths cannot see, and x264 does not hit a requested bitrate exactly.
@@ -155,8 +195,8 @@ function pickFrame(videoKbps, srcW, srcH, srcFps) {
155
195
  * second pass can aim lower without starting the reasoning again.
156
196
  */
157
197
  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');
198
+ if (!(durationSec > 0)) throw new Error(planWords.t('Cannot plan an encode without a duration'));
199
+ if (!(targetBytes > 0)) throw new Error(planWords.t('Cannot plan an encode without a target size'));
160
200
 
161
201
  const budgetBytes = targetBytes * HEADROOM * correction;
162
202
  const totalKbps = (budgetBytes * 8) / durationSec / 1000;
@@ -187,6 +227,47 @@ function planVideo({ durationSec, targetBytes, hasAudio = true, width, height, f
187
227
  };
188
228
  }
189
229
 
230
+ /*
231
+ * Sound on its own: a song, a voice memo, a recording. The same budget as a
232
+ * video's, all of it spent on one AAC track.
233
+ *
234
+ * The container (the m4a's headers and index) weighs a few KB that the
235
+ * bitrate maths cannot see, so it is taken off the budget first. Never above
236
+ * the source's own bitrate (more would only make a bigger file of the same
237
+ * sound) and never above 192kbps, past which nobody hears the difference.
238
+ * Stereo from 64kbps; under that one clean channel beats two muddy ones, but
239
+ * only a source that has two to begin with is made mono.
240
+ */
241
+ const AUDIO_CONTAINER_BYTES = 4000;
242
+ const AUDIO_MAX_KBPS = 192;
243
+ const AUDIO_STEREO_FROM = 64;
244
+ // Under this AAC is not worth encoding, the same floor audioFor uses.
245
+ const AUDIO_FLOOR_KBPS = 16;
246
+
247
+ function planAudio({ durationSec, targetBytes, channels = 2, sourceKbps = 0, correction = 1 }) {
248
+ if (!(durationSec > 0)) throw new Error(planWords.t('Cannot plan an encode without a duration'));
249
+ if (!(targetBytes > 0)) throw new Error(planWords.t('Cannot plan an encode without a target size'));
250
+
251
+ const budgetBytes = Math.max(0, targetBytes * HEADROOM * correction - AUDIO_CONTAINER_BYTES);
252
+ const budgetKbps = Math.floor((budgetBytes * 8) / durationSec / 1000);
253
+
254
+ // Too long for the target at any quality worth producing: the same answer
255
+ // planVideo gives at its floor, so the window and the command line say the
256
+ // same thing about it.
257
+ if (budgetKbps < AUDIO_FLOOR_KBPS) {
258
+ return { sound: true, kbps: AUDIO_FLOOR_KBPS, channels: 1, rough: true, hopeless: true };
259
+ }
260
+
261
+ const cap = sourceKbps > 0 ? Math.min(AUDIO_MAX_KBPS, Math.floor(sourceKbps)) : AUDIO_MAX_KBPS;
262
+ const kbps = Math.max(AUDIO_FLOOR_KBPS, Math.min(budgetKbps, cap));
263
+ return {
264
+ sound: true,
265
+ kbps,
266
+ channels: channels >= 2 && kbps >= AUDIO_STEREO_FROM ? 2 : 1,
267
+ hopeless: false,
268
+ };
269
+ }
270
+
190
271
  /**
191
272
  * After an encode lands, how much to scale the next attempt by.
192
273
  *
@@ -235,9 +316,23 @@ function isDeviceName(outPath) {
235
316
  // window and on the command line. Empty when the picture will be fine.
236
317
  function lengthNote(p, size) {
237
318
  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.`;
319
+ if (p.sound) {
320
+ if (!p.hopeless) return '';
321
+ return size
322
+ ? planWords.t('This is too long for {size}: the sound will be thin and muffled, and it may still come out over. A bigger target or a shorter recording would sound far better.', { size })
323
+ : planWords.t('This is too long for that size: the sound will be thin and muffled, and it may still come out over. A bigger target or a shorter recording would sound far better.');
324
+ }
325
+ // Whole sentences, with and without the size, so each reads right in any language.
326
+ if (p.hopeless) {
327
+ return size
328
+ ? planWords.t('This is too long for {size}: expect a small, blurry picture, and it may still come out over. A bigger target or a shorter clip would look far better.', { size })
329
+ : planWords.t('This is too long for that size: expect a small, blurry picture, and it may still come out over. A bigger target or a shorter clip would look far better.');
330
+ }
331
+ if (p.rough) {
332
+ return size
333
+ ? planWords.t('This is long for {size}, so the picture will be softer than the original.', { size })
334
+ : planWords.t('This is long for that size, so the picture will be softer than the original.');
335
+ }
241
336
  return '';
242
337
  }
243
338
 
@@ -246,9 +341,13 @@ module.exports = {
246
341
  lengthNote,
247
342
  BASE64,
248
343
  TARGETS,
344
+ targets,
345
+ useWords,
346
+ words,
249
347
  HEADROOM,
250
348
  planVideo,
251
349
  planImage,
350
+ planAudio,
252
351
  correctionFor,
253
352
  scaleToShortEdge,
254
353
  audioFor,
package/src/preload.cjs CHANGED
@@ -32,4 +32,15 @@ contextBridge.exposeInMainWorld('fits', {
32
32
  // through webUtils now, which only exists on this side of the bridge.
33
33
  pathFor: (file) => webUtils.getPathForFile(file),
34
34
  onProgress: (fn) => ipcRenderer.on('progress', (_e, p) => fn(p)),
35
+ // The window's language (the kit's, kit/lang.cjs): telling main which one
36
+ // the page is in, so its messages follow, and opening the tool's own issue
37
+ // page for a wrong translation. Neither takes a URL: main builds the one
38
+ // address it opens.
39
+ language: (code) => ipcRenderer.invoke('kit:language', code),
40
+ translationMistake: (code) => ipcRenderer.invoke('kit:translation-mistake', code),
41
+ // Main reloads the window when a language is picked: a page's own reload
42
+ // is a navigation, which a window that cancels navigation would refuse.
43
+ reloadWindow: () => ipcRenderer.invoke('kit:reload'),
44
+ // A sibling tool's page, by its id only: main opens ponderdev.com/<id> for a family id, nothing else.
45
+ openSibling: (id) => ipcRenderer.invoke('kit:open-sibling', id),
35
46
  });