Repository navigation
Expand file tree
/
Copy pathrenderer.js
More file actions
390 lines (341 loc) · 15.1 KB
/
Copy pathrenderer.js
File metadata and controls
390 lines (341 loc) · 15.1 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* ── Constants ─────────────────────────────────────────────────────────── */
const VIDEO_EXTENSIONS = new Set([
'mp4', 'avi', 'mov', 'mkv', 'webm', 'wmv', 'flv', 'm4v',
'3gp', 'ts', 'mts', 'm2ts', 'vob', 'ogv', 'rm', 'rmvb',
'divx', 'xvid', 'mpg', 'mpeg', 'f4v', 'asf',
]);
/* ── State ─────────────────────────────────────────────────────────────── */
let files = []; // { id, filePath, name, size, status, progress, outputPath, compressedSize }
let isRunning = false;
let selectedCrf = 28;
let selectedPreset = 'medium';
let selectedCq = 26;
let selectedNvencPreset = 'p4';
let useGpu = false;
let nvencAvailable = false;
let copyMetadata = false;
/* ── DOM refs ──────────────────────────────────────────────────────────── */
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const browseBtn = document.getElementById('browseBtn');
const compressBtn = document.getElementById('compressBtn');
const clearBtn = document.getElementById('clearBtn');
const fileList = document.getElementById('fileList');
const summaryBar = document.getElementById('summaryBar');
const summaryText = document.getElementById('summaryText');
const qualityBtns = document.querySelectorAll('.quality-btn');
const gpuSwitch = document.getElementById('gpuSwitch');
const gpuChip = document.getElementById('gpuChip');
const metaSwitch = document.getElementById('metaSwitch');
/* ── Helpers ───────────────────────────────────────────────────────────── */
function uid() {
return Math.random().toString(36).slice(2, 10);
}
function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`;
}
function isVideoFile(file) {
// Check MIME type first
if (file.type && file.type.startsWith('video/')) return true;
// Fall back to extension check
const ext = (file.name || '').split('.').pop().toLowerCase();
return VIDEO_EXTENSIONS.has(ext);
}
/* ── Quality buttons ───────────────────────────────────────────────────── */
qualityBtns.forEach((btn) => {
btn.addEventListener('click', () => {
if (isRunning) return;
qualityBtns.forEach((b) => b.classList.remove('active'));
btn.classList.add('active');
selectedCrf = parseInt(btn.dataset.crf);
selectedPreset = btn.dataset.preset;
selectedCq = parseInt(btn.dataset.cq);
selectedNvencPreset = btn.dataset.nvencPreset;
});
});
/* ── GPU toggle ────────────────────────────────────────────────────────── */
gpuSwitch.addEventListener('click', () => {
if (!nvencAvailable || isRunning) return;
useGpu = !useGpu;
gpuSwitch.setAttribute('aria-checked', String(useGpu));
gpuSwitch.classList.toggle('on', useGpu);
gpuChip.textContent = useGpu ? 'NVENC' : 'Available';
gpuChip.className = 'gpu-chip ' + (useGpu ? 'active' : 'ready');
});
/* ── Metadata toggle ──────────────────────────────────────────────────── */
metaSwitch.addEventListener('click', () => {
if (isRunning) return;
copyMetadata = !copyMetadata;
metaSwitch.setAttribute('aria-checked', String(copyMetadata));
metaSwitch.classList.toggle('on', copyMetadata);
});
/* ── NVENC auto-detect on startup ──────────────────────────────────────── */
(async () => {
try {
nvencAvailable = await window.api.detectNvenc();
} catch {
nvencAvailable = false;
}
if (nvencAvailable) {
gpuChip.textContent = 'Available';
gpuChip.className = 'gpu-chip ready';
gpuSwitch.disabled = false;
gpuSwitch.title = 'Click to enable NVIDIA NVENC GPU encoding';
} else {
gpuChip.textContent = 'Not found';
gpuChip.className = 'gpu-chip unavailable';
gpuSwitch.title =
'No FFmpeg with NVENC found.\n' +
'Install FFmpeg: winget install Gyan.FFmpeg\n' +
'or: choco install ffmpeg\n' +
'Then restart the app.';
}
})();
/* ── NVENC fallback notification ───────────────────────────────────────── */
window.api.onNvencFallback(() => {
showToast('GPU encoding failed — retrying with CPU');
});
/* ── Drag & Drop ───────────────────────────────────────────────────────── */
['dragenter', 'dragover'].forEach((evt) => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.add('drag-over');
});
});
['dragleave', 'drop'].forEach((evt) => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('drag-over');
});
});
dropZone.addEventListener('drop', (e) => {
const droppedFiles = Array.from(e.dataTransfer.files);
addFiles(droppedFiles);
});
/* ── Browse button ─────────────────────────────────────────────────────── */
browseBtn.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', () => {
addFiles(Array.from(fileInput.files));
fileInput.value = '';
});
/* ── Add files to queue ────────────────────────────────────────────────── */
function addFiles(rawFiles) {
const newFiles = rawFiles.filter(isVideoFile);
if (newFiles.length < rawFiles.length) {
const skipped = rawFiles.length - newFiles.length;
showToast(`${skipped} file(s) skipped — not a video format`);
}
for (const f of newFiles) {
// Skip duplicates by path
const fPath = f.path || f.name;
if (files.some((existing) => existing.filePath === fPath)) continue;
files.push({
id: uid(),
filePath: fPath,
name: f.name,
size: f.size,
status: 'pending', // pending | compressing | done | error | cancelled
progress: 0,
outputPath: null,
compressedSize: null,
errorMsg: null,
});
}
render();
updateButtons();
}
/* ── Remove file ───────────────────────────────────────────────────────── */
function removeFile(id) {
const entry = files.find((f) => f.id === id);
if (entry && entry.status === 'compressing') {
window.api.cancelCompression(id);
}
files = files.filter((f) => f.id !== id);
render();
updateButtons();
}
/* ── Render file list ──────────────────────────────────────────────────── */
function render() {
if (files.length === 0) {
fileList.innerHTML = '';
dropZone.style.display = '';
return;
}
dropZone.style.display = 'none';
fileList.innerHTML = files
.map((f) => {
const savingsHtml = buildSavingsHtml(f);
const progressHtml = buildProgressHtml(f);
const actionsHtml = buildActionsHtml(f);
const statusClass = `status-${f.status}`;
return `
<li class="file-item ${statusClass}" data-id="${f.id}">
<div class="file-icon">${fileIcon(f.status)}</div>
<div class="file-info">
<span class="file-name" title="${escHtml(f.filePath)}">${escHtml(f.name)}</span>
<span class="file-meta">${formatBytes(f.size)}${savingsHtml}</span>
${progressHtml}
${f.errorMsg ? `<span class="file-error">${escHtml(f.errorMsg)}</span>` : ''}
</div>
<div class="file-actions">${actionsHtml}</div>
</li>`;
})
.join('');
// Wire up action buttons inside the list
fileList.querySelectorAll('[data-action]').forEach((el) => {
el.addEventListener('click', () => {
const id = el.closest('[data-id]').dataset.id;
const action = el.dataset.action;
if (action === 'remove') removeFile(id);
if (action === 'reveal') {
const entry = files.find((f) => f.id === id);
if (entry?.outputPath) window.api.revealFile(entry.outputPath);
}
});
});
}
function buildSavingsHtml(f) {
if (f.status !== 'done' || !f.compressedSize) return '';
const saved = f.size - f.compressedSize;
const pct = Math.round((saved / f.size) * 100);
return ` <span class="savings">→ ${formatBytes(f.compressedSize)} <em>(${pct}% smaller)</em></span>`;
}
function buildProgressHtml(f) {
if (f.status !== 'compressing') return '';
return `
<div class="progress-wrap" role="progressbar" aria-valuenow="${f.progress}" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar" style="width:${f.progress}%"></div>
<span class="progress-label">${f.progress}%</span>
</div>`;
}
function buildActionsHtml(f) {
const remove = `<button class="icon-btn danger" data-action="remove" title="Remove">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
</button>`;
const reveal = f.status === 'done'
? `<button class="icon-btn" data-action="reveal" title="Show in folder">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/></svg>
</button>`
: '';
return reveal + remove;
}
function fileIcon(status) {
if (status === 'done') return '<svg class="icon-done" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="20 6 9 17 4 12"/></svg>';
if (status === 'error') return '<svg class="icon-err" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>';
if (status === 'compressing')return '<div class="spinner"></div>';
return '<svg class="icon-vid" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><rect x="2" y="3" width="20" height="14" rx="2"/><path d="M8 9l5 3-5 3V9z"/></svg>';
}
function escHtml(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
/* ── Update button states ──────────────────────────────────────────────── */
function updateButtons() {
const pending = files.filter((f) => f.status === 'pending').length;
const any = files.length > 0;
compressBtn.disabled = isRunning || pending === 0;
clearBtn.disabled = isRunning || !any;
compressBtn.textContent = pending === 1
? 'Compress 1 Video'
: `Compress ${pending} Videos`;
}
/* ── Progress listener ─────────────────────────────────────────────────── */
window.api.onProgress(({ id, progress }) => {
const entry = files.find((f) => f.id === id);
if (entry) {
entry.progress = progress;
// Optimistic partial re-render: just update progress bar
const item = fileList.querySelector(`[data-id="${id}"]`);
if (item) {
const bar = item.querySelector('.progress-bar');
const lbl = item.querySelector('.progress-label');
if (bar) bar.style.width = `${progress}%`;
if (lbl) lbl.textContent = `${progress}%`;
}
}
});
/* ── Compress All ──────────────────────────────────────────────────────── */
compressBtn.addEventListener('click', () => startCompression());
async function startCompression() {
const queue = files.filter((f) => f.status === 'pending');
if (queue.length === 0 || isRunning) return;
isRunning = true;
summaryBar.hidden = true;
qualityBtns.forEach((b) => b.disabled = true);
gpuSwitch.disabled = true;
metaSwitch.disabled = true;
updateButtons();
let doneCount = 0;
let totalSaved = 0;
for (const entry of queue) {
entry.status = 'compressing';
entry.progress = 0;
render();
try {
const result = await window.api.compressVideo({
id: entry.id,
filePath: entry.filePath,
crf: selectedCrf,
preset: selectedPreset,
useGpu,
cq: selectedCq,
nvencPreset: selectedNvencPreset,
copyMetadata,
});
entry.status = 'done';
entry.progress = 100;
entry.outputPath = result.outputPath;
entry.compressedSize = result.compressedSize;
doneCount++;
totalSaved += Math.max(0, result.originalSize - result.compressedSize);
} catch (err) {
if (entry.status === 'compressing') {
// Only mark as error if it wasn't cancelled
entry.status = 'error';
entry.errorMsg = err.message || 'Compression failed';
}
}
render();
}
isRunning = false;
qualityBtns.forEach((b) => b.disabled = false);
gpuSwitch.disabled = !nvencAvailable;
metaSwitch.disabled = false;
updateButtons();
if (doneCount > 0) {
summaryText.textContent =
`${doneCount} video${doneCount > 1 ? 's' : ''} compressed — saved ${formatBytes(totalSaved)} total`;
summaryBar.hidden = false;
}
}
/* ── Clear ─────────────────────────────────────────────────────────────── */
clearBtn.addEventListener('click', () => {
if (isRunning) return;
files = [];
summaryBar.hidden = true;
render();
updateButtons();
dropZone.style.display = '';
});
/* ── Toast notification ────────────────────────────────────────────────── */
function showToast(msg) {
let toast = document.getElementById('toast');
if (!toast) {
toast = document.createElement('div');
toast.id = 'toast';
document.body.appendChild(toast);
}
toast.textContent = msg;
toast.classList.add('show');
clearTimeout(toast._timer);
toast._timer = setTimeout(() => toast.classList.remove('show'), 3000);
}