Add ffmpeg-based video thumbnails for file tiles
@@ -4346,7 +4346,7 @@ function setThumbnailPending (iconEl, pending)
4346
4346
function getThumbnailKind (name)
4347
4347
{
4348
4348
const ext = extOf(name || '');
4349
if (IMAGE_EXTS.has(ext)) return 'image';
4349
if (IMAGE_EXTS.has(ext) || VIDEO_EXTS.has(ext)) return 'image';
4350
4350
if (CODE_EXTS.has(ext)) return 'text';
4351
4351
return 'badge';
4352
4352
}
@@ -4346,7 +4346,7 @@ function setThumbnailPending (iconEl, pending)4346
function getThumbnailKind (name)4346
function getThumbnailKind (name)4347
{4347
{4348
const ext = extOf(name || '');4348
const ext = extOf(name || '');4349
if (IMAGE_EXTS.has(ext)) return 'image';4349
if (IMAGE_EXTS.has(ext) || VIDEO_EXTS.has(ext)) return 'image';4350
if (CODE_EXTS.has(ext)) return 'text';4350
if (CODE_EXTS.has(ext)) return 'text';4351
return 'badge';4351
return 'badge';4352
}4352
}@@ -3,6 +3,7 @@
3
3
const fs = require('fs');
4
4
const path = require('path');
5
5
const crypto = require('crypto');
6
const { spawn } = require('child_process');
6
7
const sharp = require('sharp');
7
8
8
9
const { loadConfig, getEffectivePaths } = require('./config');
@@ -16,6 +17,9 @@ const LOOP_DELAY_MS = 5000;
16
17
const IMAGE_EXTS = new Set([
17
18
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
18
19
]);
20
const VIDEO_EXTS = new Set([
21
'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg',
22
]);
19
23
20
24
let loopTimer = null;
21
25
let loopStarted = false;
@@ -61,6 +65,85 @@ function isImagePath (filePath)
61
65
return IMAGE_EXTS.has(ext);
62
66
}
63
67
68
function isVideoPath (filePath)
69
{
70
const ext = path.extname(filePath).slice(1).toLowerCase();
71
return VIDEO_EXTS.has(ext);
72
}
73
74
function ffmpegPath ()
75
{
76
return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
77
}
78
79
function ffprobePath ()
80
{
81
return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
82
}
83
84
function runCommand (command, args)
85
{
86
return new Promise((resolve, reject) =>
87
{
88
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
89
let stdout = '';
90
let stderr = '';
91
92
child.stdout.on('data', (chunk) =>
93
{
94
stdout += chunk.toString();
95
});
96
97
child.stderr.on('data', (chunk) =>
98
{
99
stderr += chunk.toString();
100
});
101
102
child.on('error', (err) =>
103
{
104
if (err && err.code === 'ENOENT')
105
{
106
reject(new Error(`${command} is not installed or not available in PATH.`));
107
return;
108
}
109
reject(err);
110
});
111
112
child.on('close', (code) =>
113
{
114
if (code === 0)
115
{
116
resolve({ stdout, stderr });
117
return;
118
}
119
reject(new Error(stderr.trim() || `${command} exited with code ${code}.`));
120
});
121
});
122
}
123
124
async function probeDurationSeconds (sourceFile)
125
{
126
const { stdout } = await runCommand(ffprobePath(), [
127
'-v', 'error',
128
'-print_format', 'json',
129
'-show_format',
130
sourceFile,
131
]);
132
133
let payload;
134
try
135
{
136
payload = JSON.parse(stdout || '{}');
137
} catch
138
{
139
return null;
140
}
141
142
const duration = Number(payload && payload.format && payload.format.duration);
143
if (!Number.isFinite(duration) || duration <= 0) return null;
144
return duration;
145
}
146
64
147
async function exists (target)
65
148
{
66
149
try
@@ -205,6 +288,46 @@ async function generateImageThumb (sourceFile, thumbPath)
205
288
.toFile(thumbPath);
206
289
}
207
290
291
async function generateVideoThumb (sourceFile, thumbPath)
292
{
293
await ensureDir(path.dirname(thumbPath));
294
295
const duration = await probeDurationSeconds(sourceFile).catch(() => null);
296
let captureAt = 1;
297
if (duration && duration > 0)
298
{
299
captureAt = duration * 0.1;
300
if (duration > 0.2) captureAt = Math.min(captureAt, duration - 0.1);
301
captureAt = Math.max(0, captureAt);
302
}
303
304
const tmpPngPath = thumbPath + '.tmp.png';
305
try
306
{
307
await runCommand(ffmpegPath(), [
308
'-hide_banner',
309
'-loglevel', 'error',
310
'-y',
311
'-ss', captureAt.toFixed(3),
312
'-i', sourceFile,
313
'-frames:v', '1',
314
'-an',
315
'-sn',
316
tmpPngPath,
317
]);
318
319
await sharp(tmpPngPath)
320
.rotate()
321
.resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })
322
.webp({ quality: 80 })
323
.toFile(thumbPath);
324
}
325
finally
326
{
327
await removeFileIfExists(tmpPngPath);
328
}
329
}
330
208
331
function effectiveRoots ()
209
332
{
210
333
const config = loadConfig();
@@ -292,7 +415,12 @@ async function syncThumbnailCacheOnce ()
292
415
},
293
416
async (filePath) =>
294
417
{
295
if (!isImagePath(filePath)) return;
418
const kind = isImagePath(filePath)
419
? 'image'
420
: isVideoPath(filePath)
421
? 'video'
422
: null;
423
if (!kind) return;
296
424
297
425
let stat;
298
426
try
@@ -312,40 +440,65 @@ async function syncThumbnailCacheOnce ()
312
440
prev &&
313
441
Number(prev.size) === Number(stat.size) &&
314
442
Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
315
prev.thumbRelPath === thumbRelPath;
443
prev.thumbRelPath === thumbRelPath &&
444
(prev.kind === kind || !prev.kind);
316
445
317
446
if (unchangedByStat && await exists(thumbPath))
318
447
{
319
nextManifest.files[filePath] = prev;
448
nextManifest.files[filePath] = {
449
...prev,
450
kind,
451
};
320
452
return;
321
453
}
322
454
323
let sha256;
324
try
325
{
326
sha256 = await sha256File(filePath);
327
} catch
328
{
329
return;
330
}
331
332
const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);
333
if (!unchangedByHash)
455
if (kind === 'image')
334
456
{
457
let sha256;
335
458
try
336
459
{
337
await generateImageThumb(filePath, thumbPath);
460
sha256 = await sha256File(filePath);
338
461
} catch
339
462
{
340
463
return;
341
464
}
465
466
const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);
467
if (!unchangedByHash)
468
{
469
try
470
{
471
await generateImageThumb(filePath, thumbPath);
472
} catch
473
{
474
return;
475
}
476
}
477
478
nextManifest.files[filePath] = {
479
sha256,
480
size: stat.size,
481
mtimeMs: stat.mtimeMs,
482
thumbRelPath,
483
kind,
484
};
485
return;
486
}
487
488
try
489
{
490
await generateVideoThumb(filePath, thumbPath);
491
} catch
492
{
493
return;
342
494
}
343
495
344
496
nextManifest.files[filePath] = {
345
sha256,
497
sha256: null,
346
498
size: stat.size,
347
499
mtimeMs: stat.mtimeMs,
348
500
thumbRelPath,
501
kind,
349
502
};
350
503
},
351
504
);
@@ -3,6 +3,7 @@3
const fs = require('fs');3
const fs = require('fs');4
const path = require('path');4
const path = require('path');5
const crypto = require('crypto');5
const crypto = require('crypto');6
const { spawn } = require('child_process');6
const sharp = require('sharp');7
const sharp = require('sharp');7
8
8
const { loadConfig, getEffectivePaths } = require('./config');9
const { loadConfig, getEffectivePaths } = require('./config');@@ -16,6 +17,9 @@ const LOOP_DELAY_MS = 5000;16
const IMAGE_EXTS = new Set([17
const IMAGE_EXTS = new Set([17
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',18
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',18
]);19
]);20
const VIDEO_EXTS = new Set([21
'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg',22
]);19
23
20
let loopTimer = null;24
let loopTimer = null;21
let loopStarted = false;25
let loopStarted = false;@@ -61,6 +65,85 @@ function isImagePath (filePath)61
return IMAGE_EXTS.has(ext);65
return IMAGE_EXTS.has(ext);62
}66
}63
67
68
function isVideoPath (filePath)69
{70
const ext = path.extname(filePath).slice(1).toLowerCase();71
return VIDEO_EXTS.has(ext);72
}73
74
function ffmpegPath ()75
{76
return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';77
}78
79
function ffprobePath ()80
{81
return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';82
}83
84
function runCommand (command, args)85
{86
return new Promise((resolve, reject) =>87
{88
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });89
let stdout = '';90
let stderr = '';91
92
child.stdout.on('data', (chunk) =>93
{94
stdout += chunk.toString();95
});96
97
child.stderr.on('data', (chunk) =>98
{99
stderr += chunk.toString();100
});101
102
child.on('error', (err) =>103
{104
if (err && err.code === 'ENOENT')105
{106
reject(new Error(`${command} is not installed or not available in PATH.`));107
return;108
}109
reject(err);110
});111
112
child.on('close', (code) =>113
{114
if (code === 0)115
{116
resolve({ stdout, stderr });117
return;118
}119
reject(new Error(stderr.trim() || `${command} exited with code ${code}.`));120
});121
});122
}123
124
async function probeDurationSeconds (sourceFile)125
{126
const { stdout } = await runCommand(ffprobePath(), [127
'-v', 'error',128
'-print_format', 'json',129
'-show_format',130
sourceFile,131
]);132
133
let payload;134
try135
{136
payload = JSON.parse(stdout || '{}');137
} catch138
{139
return null;140
}141
142
const duration = Number(payload && payload.format && payload.format.duration);143
if (!Number.isFinite(duration) || duration <= 0) return null;144
return duration;145
}146
64
async function exists (target)147
async function exists (target)65
{148
{66
try149
try@@ -205,6 +288,46 @@ async function generateImageThumb (sourceFile, thumbPath)205
.toFile(thumbPath);288
.toFile(thumbPath);206
}289
}207
290
291
async function generateVideoThumb (sourceFile, thumbPath)292
{293
await ensureDir(path.dirname(thumbPath));294
295
const duration = await probeDurationSeconds(sourceFile).catch(() => null);296
let captureAt = 1;297
if (duration && duration > 0)298
{299
captureAt = duration * 0.1;300
if (duration > 0.2) captureAt = Math.min(captureAt, duration - 0.1);301
captureAt = Math.max(0, captureAt);302
}303
304
const tmpPngPath = thumbPath + '.tmp.png';305
try306
{307
await runCommand(ffmpegPath(), [308
'-hide_banner',309
'-loglevel', 'error',310
'-y',311
'-ss', captureAt.toFixed(3),312
'-i', sourceFile,313
'-frames:v', '1',314
'-an',315
'-sn',316
tmpPngPath,317
]);318
319
await sharp(tmpPngPath)320
.rotate()321
.resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })322
.webp({ quality: 80 })323
.toFile(thumbPath);324
}325
finally326
{327
await removeFileIfExists(tmpPngPath);328
}329
}330
208
function effectiveRoots ()331
function effectiveRoots ()209
{332
{210
const config = loadConfig();333
const config = loadConfig();@@ -292,7 +415,12 @@ async function syncThumbnailCacheOnce ()292
},415
},293
async (filePath) =>416
async (filePath) =>294
{417
{295
if (!isImagePath(filePath)) return;418
const kind = isImagePath(filePath)419
? 'image'420
: isVideoPath(filePath)421
? 'video'422
: null;423
if (!kind) return;296
424
297
let stat;425
let stat;298
try426
try@@ -312,40 +440,65 @@ async function syncThumbnailCacheOnce ()312
prev &&440
prev &&313
Number(prev.size) === Number(stat.size) &&441
Number(prev.size) === Number(stat.size) &&314
Number(prev.mtimeMs) === Number(stat.mtimeMs) &&442
Number(prev.mtimeMs) === Number(stat.mtimeMs) &&315
prev.thumbRelPath === thumbRelPath;443
prev.thumbRelPath === thumbRelPath &&444
(prev.kind === kind || !prev.kind);316
445
317
if (unchangedByStat && await exists(thumbPath))446
if (unchangedByStat && await exists(thumbPath))318
{447
{319
nextManifest.files[filePath] = prev;448
nextManifest.files[filePath] = {449
...prev,450
kind,451
};320
return;452
return;321
}453
}322
454
323
let sha256;455
if (kind === 'image')324
try325
{326
sha256 = await sha256File(filePath);327
} catch328
{329
return;330
}331
332
const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);333
if (!unchangedByHash)334
{456
{457
let sha256;335
try458
try336
{459
{337
await generateImageThumb(filePath, thumbPath);460
sha256 = await sha256File(filePath);338
} catch461
} catch339
{462
{340
return;463
return;341
}464
}465
466
const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);467
if (!unchangedByHash)468
{469
try470
{471
await generateImageThumb(filePath, thumbPath);472
} catch473
{474
return;475
}476
}477
478
nextManifest.files[filePath] = {479
sha256,480
size: stat.size,481
mtimeMs: stat.mtimeMs,482
thumbRelPath,483
kind,484
};485
return;486
}487
488
try489
{490
await generateVideoThumb(filePath, thumbPath);491
} catch492
{493
return;342
}494
}343
495
344
nextManifest.files[filePath] = {496
nextManifest.files[filePath] = {345
sha256,497
sha256: null,346
size: stat.size,498
size: stat.size,347
mtimeMs: stat.mtimeMs,499
mtimeMs: stat.mtimeMs,348
thumbRelPath,500
thumbRelPath,501
kind,349
};502
};350
},503
},351
);504
);@@ -720,6 +720,32 @@ describe('page load smoke test', () =>
720
720
dom.window.close();
721
721
});
722
722
723
test('shows pending icon spinner when video thumbnail cache is not ready', async () =>
724
{
725
listFixture = {
726
'/data': [
727
{
728
name: 'clip.mp4',
729
path: '/data/clip.mp4',
730
isDir: false,
731
size: 5 * 1024 * 1024,
732
mtime: Date.now(),
733
},
734
],
735
};
736
thumbnailStatusFixture['/data/clip.mp4'] = 404;
737
738
const { dom, runtimeErrors } = await bootClient();
739
expect(runtimeErrors).toEqual([]);
740
741
await new Promise(resolve => dom.window.setTimeout(resolve, 80));
742
const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/clip.mp4"]');
743
expect(icon).toBeTruthy();
744
expect(icon.classList.contains('thumb-pending')).toBe(true);
745
746
dom.window.close();
747
});
748
723
749
test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
724
750
{
725
751
listFixture = {
@@ -720,6 +720,32 @@ describe('page load smoke test', () =>720
dom.window.close();
720
dom.window.close();
721
});
721
});
722
722
723
test('shows pending icon spinner when video thumbnail cache is not ready', async () =>
724
{
725
listFixture = {
726
'/data': [
727
{
728
name: 'clip.mp4',
729
path: '/data/clip.mp4',
730
isDir: false,
731
size: 5 * 1024 * 1024,
732
mtime: Date.now(),
733
},
734
],
735
};
736
thumbnailStatusFixture['/data/clip.mp4'] = 404;
737
738
const { dom, runtimeErrors } = await bootClient();
739
expect(runtimeErrors).toEqual([]);
740
741
await new Promise(resolve => dom.window.setTimeout(resolve, 80));
742
const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/clip.mp4"]');
743
expect(icon).toBeTruthy();
744
expect(icon.classList.contains('thumb-pending')).toBe(true);
745
746
dom.window.close();
747
});
748
723
test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
749
test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
724
{
750
{
725
listFixture = {
751
listFixture = {
@@ -37,6 +37,12 @@ function writeTinySvg (target)
37
37
fs.writeFileSync(target, svg, 'utf8');
38
38
}
39
39
40
function writeExecutable (target, content)
41
{
42
fs.writeFileSync(target, content, { encoding: 'utf8', mode: 0o755 });
43
fs.chmodSync(target, 0o755);
44
}
45
40
46
describe('thumbnail cache worker', () =>
41
47
{
42
48
let tempDir;
@@ -69,6 +75,8 @@ describe('thumbnail cache worker', () =>
69
75
{
70
76
delete process.env.FILEMANAGER_PUBLIC_DIR;
71
77
delete process.env.FILEMANAGER_THUMBS_DIR;
78
delete process.env.FFMPEG_PATH;
79
delete process.env.FFPROBE_PATH;
72
80
mockPaths = [];
73
81
try
74
82
{
@@ -200,4 +208,40 @@ describe('thumbnail cache worker', () =>
200
208
201
209
fs.renameSync(offlinePath, sourceRoot);
202
210
});
211
212
test('creates cached thumbnail for video files using ffmpeg frame extraction', async () =>
213
{
214
const mod = loadModule();
215
const toolsDir = path.join(tempDir, 'tools');
216
const ffprobeScript = path.join(toolsDir, 'ffprobe-mock.js');
217
const ffmpegScript = path.join(toolsDir, 'ffmpeg-mock.js');
218
const ffmpegArgsLog = path.join(tempDir, 'ffmpeg-args.log');
219
const sourceFramePng = path.join(tempDir, 'ffmpeg-source-frame.png');
220
221
fs.mkdirSync(toolsDir, { recursive: true });
222
await writeTinyPng(sourceFramePng);
223
224
writeExecutable(ffprobeScript, `#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ format: { duration: '20' } }));\n`);
225
writeExecutable(ffmpegScript, `#!/usr/bin/env node\nconst fs = require('fs');\nconst args = process.argv.slice(2);\nfs.writeFileSync(${JSON.stringify(ffmpegArgsLog)}, args.join('\\n'), 'utf8');\nconst outPath = args[args.length - 1];\nfs.copyFileSync(${JSON.stringify(sourceFramePng)}, outPath);\n`);
226
227
process.env.FFPROBE_PATH = ffprobeScript;
228
process.env.FFMPEG_PATH = ffmpegScript;
229
230
const videoPath = path.join(sourceRoot, 'clips', 'sample.mp4');
231
fs.mkdirSync(path.dirname(videoPath), { recursive: true });
232
fs.writeFileSync(videoPath, 'fake-video-data');
233
234
await mod.syncThumbnailCacheOnce();
235
236
const thumbPath = mod.getThumbPathForSourceFile(videoPath);
237
expect(fs.existsSync(thumbPath)).toBe(true);
238
239
const info = await mod.getCachedThumbnailInfo(videoPath);
240
expect(info).toBeTruthy();
241
expect(info.thumbPath).toBe(thumbPath);
242
243
const ffmpegArgs = fs.readFileSync(ffmpegArgsLog, 'utf8');
244
expect(ffmpegArgs).toContain('-ss');
245
expect(ffmpegArgs).toContain('2.000');
246
});
203
247
});
@@ -37,6 +37,12 @@ function writeTinySvg (target)37
fs.writeFileSync(target, svg, 'utf8');37
fs.writeFileSync(target, svg, 'utf8');38
}38
}39
39
40
function writeExecutable (target, content)41
{42
fs.writeFileSync(target, content, { encoding: 'utf8', mode: 0o755 });43
fs.chmodSync(target, 0o755);44
}45
40
describe('thumbnail cache worker', () =>46
describe('thumbnail cache worker', () =>41
{47
{42
let tempDir;48
let tempDir;@@ -69,6 +75,8 @@ describe('thumbnail cache worker', () =>69
{75
{70
delete process.env.FILEMANAGER_PUBLIC_DIR;76
delete process.env.FILEMANAGER_PUBLIC_DIR;71
delete process.env.FILEMANAGER_THUMBS_DIR;77
delete process.env.FILEMANAGER_THUMBS_DIR;78
delete process.env.FFMPEG_PATH;79
delete process.env.FFPROBE_PATH;72
mockPaths = [];80
mockPaths = [];73
try81
try74
{82
{@@ -200,4 +208,40 @@ describe('thumbnail cache worker', () =>200
208
201
fs.renameSync(offlinePath, sourceRoot);209
fs.renameSync(offlinePath, sourceRoot);202
});210
});211
212
test('creates cached thumbnail for video files using ffmpeg frame extraction', async () =>213
{214
const mod = loadModule();215
const toolsDir = path.join(tempDir, 'tools');216
const ffprobeScript = path.join(toolsDir, 'ffprobe-mock.js');217
const ffmpegScript = path.join(toolsDir, 'ffmpeg-mock.js');218
const ffmpegArgsLog = path.join(tempDir, 'ffmpeg-args.log');219
const sourceFramePng = path.join(tempDir, 'ffmpeg-source-frame.png');220
221
fs.mkdirSync(toolsDir, { recursive: true });222
await writeTinyPng(sourceFramePng);223
224
writeExecutable(ffprobeScript, `#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ format: { duration: '20' } }));\n`);225
writeExecutable(ffmpegScript, `#!/usr/bin/env node\nconst fs = require('fs');\nconst args = process.argv.slice(2);\nfs.writeFileSync(${JSON.stringify(ffmpegArgsLog)}, args.join('\\n'), 'utf8');\nconst outPath = args[args.length - 1];\nfs.copyFileSync(${JSON.stringify(sourceFramePng)}, outPath);\n`);226
227
process.env.FFPROBE_PATH = ffprobeScript;228
process.env.FFMPEG_PATH = ffmpegScript;229
230
const videoPath = path.join(sourceRoot, 'clips', 'sample.mp4');231
fs.mkdirSync(path.dirname(videoPath), { recursive: true });232
fs.writeFileSync(videoPath, 'fake-video-data');233
234
await mod.syncThumbnailCacheOnce();235
236
const thumbPath = mod.getThumbPathForSourceFile(videoPath);237
expect(fs.existsSync(thumbPath)).toBe(true);238
239
const info = await mod.getCachedThumbnailInfo(videoPath);240
expect(info).toBeTruthy();241
expect(info.thumbPath).toBe(thumbPath);242
243
const ffmpegArgs = fs.readFileSync(ffmpegArgsLog, 'utf8');244
expect(ffmpegArgs).toContain('-ss');245
expect(ffmpegArgs).toContain('2.000');246
});203
});247
});