BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

50e24563 History
image-subtitles.js 17.1 KiB Download Raw
1'use strict';
2
3const { spawn } = require('child_process');
4const crypto = require('crypto');
5const fs = require('fs');
6const os = require('os');
7const path = require('path');
8const { Readable } = require('stream');
9const sharp = require('sharp');
10
11const SUBTITLE_CACHE_DIR = path.join(os.tmpdir(), 'filemanager-video-subtitles');
12const EVENT_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_TTL_MS, 5 * 60 * 1000);
13const FRAME_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_TTL_MS, 2 * 60 * 1000);
14const MAX_EVENT_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, 3);
15const MAX_FRAME_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, 50);
16const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 50 * 1024 * 1024);
17const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
18const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
19const PGS_TIMEBASE = 90000;
20const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = toPositiveInt(process.env.IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS, 5);
21
22const eventCache = createCacheState(MAX_EVENT_CACHE_ITEMS, MAX_EVENT_CACHE_BYTES);
23const frameCache = createCacheState(MAX_FRAME_CACHE_ITEMS, MAX_FRAME_CACHE_BYTES);
24
25let parseDisplaySets = null;
26
27function ensureImageDataGlobal ()
28{
29 if (typeof globalThis.ImageData === 'function') return;
30
31 class ImageDataPolyfill
32 {
33 constructor(data, width, height)
34 {
35 this.data = data;
36 this.width = width;
37 this.height = height;
38 }
39 }
40
41 globalThis.ImageData = ImageDataPolyfill;
42}
43
44function loadPgsParser ()
45{
46 if (parseDisplaySets) return parseDisplaySets;
47 ensureImageDataGlobal();
48 ({ parseDisplaySets } = require('pgs-parser'));
49 return parseDisplaySets;
50}
51
52function ffmpegPath ()
53{
54 return process.env.FFMPEG_PATH || 'ffmpeg';
55}
56
57function ensureSubtitleCacheDir ()
58{
59 fs.mkdirSync(SUBTITLE_CACHE_DIR, { recursive: true });
60}
61
62function toPositiveInt (value, fallback)
63{
64 const number = Number.parseInt(String(value || ''), 10);
65 if (Number.isInteger(number) && number > 0) return number;
66 return fallback;
67}
68
69function toBoundedNumber (value, fallback, min, max)
70{
71 const numeric = Number.parseFloat(String(value || ''));
72 if (!Number.isFinite(numeric)) return fallback;
73 if (numeric < min || numeric > max) return fallback;
74 return numeric;
75}
76
77function hashSubtitleKey (filePath, streamIndex, stat)
78{
79 return crypto
80 .createHash('sha1')
81 .update(`${filePath}|${streamIndex}|${stat.size}|${Math.floor(stat.mtimeMs)}`)
82 .digest('hex');
83}
84
85function createCacheState (maxItems, maxBytes)
86{
87 return {
88 map: new Map(),
89 totalBytes: 0,
90 maxItems,
91 maxBytes,
92 };
93}
94
95function normaliseFilePath (filePath)
96{
97 return path.resolve(String(filePath || '').trim());
98}
99
100function cachePrefixForFile (filePath)
101{
102 const normalised = String(filePath || '').trim();
103 if (!normalised) return '';
104 return `${normaliseFilePath(normalised)}|`;
105}
106
107function eventIdentityKey (filePath, streamIndex, stat)
108{
109 const normalisedPath = normaliseFilePath(filePath);
110 const hash = hashSubtitleKey(normalisedPath, streamIndex, stat);
111 return `${normalisedPath}|${streamIndex}|${hash}`;
112}
113
114function eventCacheFileKey (identityKey)
115{
116 const hash = crypto.createHash('sha1').update(String(identityKey || '')).digest('hex');
117 return `event-${hash}`;
118}
119
120function toValidCacheSize (value)
121{
122 const number = Number(value);
123 if (!Number.isFinite(number) || number <= 0) return 0;
124 return Math.floor(number);
125}
126
127function estimateEventCacheBytes (value)
128{
129 if (!value) return 0;
130 if (typeof value.then === 'function') return 1024;
131 if (!Array.isArray(value.events)) return 1024;
132
133 let bytes = 1024;
134 for (const event of value.events)
135 {
136 if (event && Buffer.isBuffer(event.imagePng)) bytes += event.imagePng.length;
137 bytes += 160;
138 }
139 return bytes;
140}
141
142function estimateFrameCacheBytes (value)
143{
144 if (!value) return 0;
145 if (Buffer.isBuffer(value)) return value.length;
146 if (value && Buffer.isBuffer(value.frame)) return value.frame.length + 96;
147 return 1024;
148}
149
150function deleteCachedValue (cache, key)
151{
152 const entry = cache.map.get(key);
153 if (!entry) return false;
154
155 cache.map.delete(key);
156 cache.totalBytes = Math.max(0, cache.totalBytes - toValidCacheSize(entry.sizeBytes));
157 return true;
158}
159
160function touchCachedEntry (cache, key, ttlMs)
161{
162 const entry = cache.map.get(key);
163 if (!entry) return;
164 entry.expiresAt = Date.now() + ttlMs;
165 cache.map.delete(key);
166 cache.map.set(key, entry);
167}
168
169function getCachedValue (cache, key)
170{
171 const now = Date.now();
172 const entry = cache.map.get(key);
173 if (!entry) return null;
174
175 if (entry.expiresAt <= now)
176 {
177 deleteCachedValue(cache, key);
178 return null;
179 }
180
181 cache.map.delete(key);
182 cache.map.set(key, entry);
183 return entry.value;
184}
185
186function setCachedValue (cache, key, value, ttlMs, estimateBytes)
187{
188 const now = Date.now();
189 deleteCachedValue(cache, key);
190
191 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
192
193 cache.map.set(key, {
194 value,
195 expiresAt: now + ttlMs,
196 sizeBytes,
197 });
198 cache.totalBytes += sizeBytes;
199
200 while (cache.map.size > cache.maxItems || cache.totalBytes > cache.maxBytes)
201 {
202 const oldestKey = cache.map.keys().next().value;
203 deleteCachedValue(cache, oldestKey);
204 }
205}
206
207function runCommand (command, args)
208{
209 return new Promise((resolve, reject) =>
210 {
211 const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
212 let stderr = '';
213
214 child.stderr.on('data', (chunk) =>
215 {
216 stderr += chunk.toString();
217 if (stderr.length > 16000) stderr = stderr.slice(-16000);
218 });
219
220 child.on('error', reject);
221 child.on('close', (code) =>
222 {
223 if (code === 0)
224 {
225 resolve();
226 return;
227 }
228 reject(new Error(stderr.trim() || `ffmpeg exited with code ${code}`));
229 });
230 });
231}
232
233async function extractSupFile (sourcePath, streamIndex, cacheKey)
234{
235 ensureSubtitleCacheDir();
236 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
237
238 if (fs.existsSync(supPath) && fs.statSync(supPath).size > 0)
239 {
240 return supPath;
241 }
242
243 await runCommand(ffmpegPath(), [
244 '-hide_banner',
245 '-loglevel', 'error',
246 '-nostdin',
247 '-y',
248 '-i', sourcePath,
249 '-map', `0:s:${streamIndex}`,
250 '-c:s', 'copy',
251 '-f', 'sup',
252 supPath,
253 ]);
254
255 return supPath;
256}
257
258function toSeconds (pgsTimestamp)
259{
260 const numeric = Number(pgsTimestamp);
261 if (!Number.isFinite(numeric) || numeric < 0) return 0;
262 return numeric / PGS_TIMEBASE;
263}
264
265function parseDisplaySetTimestamp (displaySet)
266{
267 if (displaySet && displaySet.presentationCompositionSegment && displaySet.presentationCompositionSegment.header)
268 {
269 return toSeconds(displaySet.presentationCompositionSegment.header.presentationTimestamp);
270 }
271 if (displaySet && displaySet.endDefinitionSegment && displaySet.endDefinitionSegment.header)
272 {
273 return toSeconds(displaySet.endDefinitionSegment.header.presentationTimestamp);
274 }
275 return 0;
276}
277
278async function parseSupEvents (supPath)
279{
280 const parserFactory = loadPgsParser();
281 if (typeof Readable.toWeb !== 'function')
282 {
283 throw new Error('Node.js stream bridge is unavailable for subtitle parsing.');
284 }
285
286 const subtitleStream = Readable.toWeb(fs.createReadStream(supPath));
287 const parserStream = subtitleStream.pipeThrough(parserFactory());
288 const reader = parserStream.getReader();
289
290 const events = [];
291 let currentEvent = null;
292 let scratchBuffer = null;
293
294 while (true)
295 {
296 const { done, value: displaySet } = await reader.read();
297 if (done) break;
298
299 const timestampSeconds = parseDisplaySetTimestamp(displaySet);
300
301 if (currentEvent && timestampSeconds > currentEvent.startTimeSeconds)
302 {
303 currentEvent.endTimeSeconds = timestampSeconds;
304 events.push(currentEvent);
305 currentEvent = null;
306 }
307
308 if (!displaySet || !Array.isArray(displaySet.objectDefinitionSegments) || displaySet.objectDefinitionSegments.length === 0)
309 {
310 continue;
311 }
312
313 const pcs = displaySet.presentationCompositionSegment;
314 if (!pcs) continue;
315
316 const neededPixels = Number(pcs.width || 0) * Number(pcs.height || 0) * 4;
317 if (!scratchBuffer || !Number.isInteger(neededPixels) || neededPixels <= 0 || scratchBuffer.length < neededPixels)
318 {
319 scratchBuffer = neededPixels > 0 ? new Uint8ClampedArray(neededPixels) : null;
320 }
321
322 let imageData;
323 try
324 {
325 imageData = displaySet.imageData(scratchBuffer || undefined);
326 } catch
327 {
328 continue;
329 }
330
331 if (!imageData || !imageData.data || !Number.isInteger(imageData.width) || !Number.isInteger(imageData.height))
332 {
333 continue;
334 }
335
336 const rawPixels = Buffer.from(imageData.data);
337 const imagePng = await sharp(rawPixels, {
338 raw: {
339 width: imageData.width,
340 height: imageData.height,
341 channels: 4,
342 },
343 }).png().toBuffer();
344
345 currentEvent = {
346 startTimeSeconds: timestampSeconds,
347 endTimeSeconds: Number.POSITIVE_INFINITY,
348 screenWidth: toPositiveInt(pcs.width, imageData.width),
349 screenHeight: toPositiveInt(pcs.height, imageData.height),
350 left: toPositiveInt(pcs.objectHorizontalPosition, 0),
351 top: toPositiveInt(pcs.objectVerticalPosition, 0),
352 imageWidth: imageData.width,
353 imageHeight: imageData.height,
354 imagePng,
355 };
356 }
357
358 if (currentEvent)
359 {
360 events.push(currentEvent);
361 }
362
363 return events;
364}
365
366async function buildEventCacheEntry (filePath, streamIndex, identityKey)
367{
368 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
369 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
370 const events = await parseSupEvents(supPath);
371 return {
372 cacheKey: identityKey,
373 events,
374 };
375}
376
377async function getEventCacheEntry (filePath, streamIndex)
378{
379 const normalisedPath = normaliseFilePath(filePath);
380 const stat = await fs.promises.stat(normalisedPath);
381 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
382
383 const cached = getCachedValue(eventCache, identityKey);
384 if (cached)
385 {
386 touchCachedEntry(eventCache, identityKey, EVENT_CACHE_TTL_MS);
387 return cached;
388 }
389
390 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
391 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
392
393 try
394 {
395 const resolved = await pending;
396 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
397 return resolved;
398 } catch (error)
399 {
400 deleteCachedValue(eventCache, identityKey);
401 throw error;
402 }
403}
404
405function findActiveEventIndex (events, timeSeconds)
406{
407 let left = 0;
408 let right = events.length - 1;
409 let candidate = -1;
410
411 while (left <= right)
412 {
413 const middle = Math.floor((left + right) / 2);
414 const event = events[middle];
415
416 if (event.startTimeSeconds <= timeSeconds)
417 {
418 candidate = middle;
419 left = middle + 1;
420 } else
421 {
422 right = middle - 1;
423 }
424 }
425
426 if (candidate < 0) return -1;
427
428 const active = events[candidate];
429 if (timeSeconds >= active.endTimeSeconds) return -1;
430 return candidate;
431}
432
433async function renderEventFrame (event, canvasWidth, canvasHeight)
434{
435 const sourceWidth = Math.max(1, toPositiveInt(event.screenWidth, canvasWidth));
436 const sourceHeight = Math.max(1, toPositiveInt(event.screenHeight, canvasHeight));
437
438 const scaleX = canvasWidth / sourceWidth;
439 const scaleY = canvasHeight / sourceHeight;
440
441 const targetWidth = Math.max(1, Math.round(event.imageWidth * scaleX));
442 const targetHeight = Math.max(1, Math.round(event.imageHeight * scaleY));
443 const left = Math.round(event.left * scaleX);
444 const top = Math.round(event.top * scaleY);
445 const right = left + targetWidth;
446 const bottom = top + targetHeight;
447
448 const visibleLeft = Math.max(0, left);
449 const visibleTop = Math.max(0, top);
450 const visibleRight = Math.min(canvasWidth, right);
451 const visibleBottom = Math.min(canvasHeight, bottom);
452 const visibleWidth = Math.max(0, visibleRight - visibleLeft);
453 const visibleHeight = Math.max(0, visibleBottom - visibleTop);
454
455 if (visibleWidth <= 0 || visibleHeight <= 0)
456 {
457 return null;
458 }
459
460 let imageBuffer = (targetWidth === event.imageWidth && targetHeight === event.imageHeight)
461 ? event.imagePng
462 : await sharp(event.imagePng).resize({ width: targetWidth, height: targetHeight, fit: 'fill' }).png().toBuffer();
463
464 if (visibleWidth < targetWidth || visibleHeight < targetHeight)
465 {
466 imageBuffer = await sharp(imageBuffer)
467 .extract({
468 left: Math.max(0, visibleLeft - left),
469 top: Math.max(0, visibleTop - top),
470 width: visibleWidth,
471 height: visibleHeight,
472 })
473 .png()
474 .toBuffer();
475 }
476
477 return {
478 frame: imageBuffer,
479 left: visibleLeft,
480 top: visibleTop,
481 width: visibleWidth,
482 height: visibleHeight,
483 canvasWidth,
484 canvasHeight,
485 };
486}
487
488function parseRenderOptions (options)
489{
490 const input = options || {};
491 const filePath = String(input.filePath ?? '').trim();
492 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
493 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
494 const requestedWidth = Math.max(1, toPositiveInt(input.canvasWidth ?? '', 1280));
495 const requestedHeight = Math.max(1, toPositiveInt(input.canvasHeight ?? '', 720));
496 const scale = Math.min(1, 1280 / requestedWidth, 720 / requestedHeight) * IMAGE_SUBTITLE_RENDER_SCALE;
497 const canvasWidth = Math.max(1, Math.round(requestedWidth * scale));
498 const canvasHeight = Math.max(1, Math.round(requestedHeight * scale));
499
500 return {
501 filePath,
502 streamIndex,
503 timeSeconds,
504 canvasWidth,
505 canvasHeight,
506 isValid: !!(filePath && Number.isInteger(streamIndex) && streamIndex >= 0 && Number.isFinite(timeSeconds) && timeSeconds >= 0),
507 };
508}
509
510async function renderImageSubtitleFrame (options)
511{
512 const parsed = parseRenderOptions(options);
513 const { filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight } = parsed;
514
515 if (!parsed.isValid)
516 {
517 return null;
518 }
519
520 const entry = await getEventCacheEntry(filePath, streamIndex);
521 if (!entry || !Array.isArray(entry.events) || entry.events.length === 0) return null;
522
523 const eventIndex = findActiveEventIndex(entry.events, timeSeconds);
524 if (eventIndex < 0) return null;
525
526 const frameKey = `${entry.cacheKey}|${eventIndex}|${canvasWidth}x${canvasHeight}`;
527 const cachedFrame = getCachedValue(frameCache, frameKey);
528 if (cachedFrame)
529 {
530 return cachedFrame;
531 }
532
533 const frameData = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
534 if (!frameData)
535 {
536 return null;
537 }
538
539 const event = entry.events[eventIndex];
540 const cacheValue = {
541 frame: frameData.frame,
542 left: frameData.left,
543 top: frameData.top,
544 width: frameData.width,
545 height: frameData.height,
546 canvasWidth: frameData.canvasWidth,
547 canvasHeight: frameData.canvasHeight,
548 startTimeSeconds: event.startTimeSeconds,
549 endTimeSeconds: event.endTimeSeconds,
550 };
551 setCachedValue(
552 frameCache,
553 frameKey,
554 cacheValue,
555 FRAME_CACHE_TTL_MS,
556 estimateFrameCacheBytes,
557 );
558 return cacheValue;
559}
560
561function deleteSupFileForKey (identityKey)
562{
563 const cacheKey = eventCacheFileKey(identityKey);
564 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
565 fs.unlink(supPath, () =>
566 {
567 // Best-effort; file may not exist if extraction never completed.
568 });
569}
570
571function purgeSubtitleCacheDir ()
572{
573 try
574 {
575 fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });
576 } catch
577 {
578 // Best-effort; directory may not exist.
579 }
580}
581
582function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
583{
584 const prefix = cachePrefixForFile(filePath);
585 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
586
587 const keysToDelete = [];
588 for (const key of frameCache.map.keys())
589 {
590 if (!key.startsWith(prefix)) continue;
591
592 const entry = frameCache.map.get(key);
593 if (!entry) continue;
594
595 const eventEntry = entry.value || {};
596 const { startTimeSeconds, endTimeSeconds } = eventEntry;
597
598 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
599
600 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
601 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
602
603 if (slowestViewerTimeSeconds >= dropThreshold)
604 {
605 keysToDelete.push(key);
606 }
607 }
608
609 for (const key of keysToDelete)
610 {
611 deleteCachedValue(frameCache, key);
612 }
613}
614
615function clearCachesForFile (filePath)
616{
617 const prefix = cachePrefixForFile(filePath);
618 if (!prefix) return;
619
620 const eventKeysToDelete = [];
621 const frameKeysToDelete = [];
622
623 for (const key of eventCache.map.keys())
624 {
625 if (key.startsWith(prefix)) eventKeysToDelete.push(key);
626 }
627
628 for (const key of frameCache.map.keys())
629 {
630 if (key.startsWith(prefix)) frameKeysToDelete.push(key);
631 }
632
633 for (const key of eventKeysToDelete)
634 {
635 deleteSupFileForKey(key);
636 deleteCachedValue(eventCache, key);
637 }
638
639 for (const key of frameKeysToDelete)
640 {
641 deleteCachedValue(frameCache, key);
642 }
643}
644
645module.exports = {
646 parseRenderOptions,
647 renderImageSubtitleFrame,
648 clearCachesForFile,
649 pruneOldImageSubtitleCaches,
650 purgeSubtitleCacheDir,
651};