BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

dd321017 History
image-subtitles.js 24.9 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 = toCacheItemLimit(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, Infinity);
15const MAX_FRAME_CACHE_ITEMS = toCacheItemLimit(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, Infinity);
16// Removed byte size cap for event cache to prevent thrashing
17const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 50 * 1024 * 1024);
18const MIN_EVENT_WINDOW_SECONDS = 30; // Always keep at least this much for seeking
19const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
20const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
21const PGS_TIMEBASE = 90000;
22const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = toPositiveInt(process.env.IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS, 5);
23
24const eventCache = createCacheState(MAX_EVENT_CACHE_ITEMS, MAX_EVENT_CACHE_BYTES);
25const frameCache = createCacheState(MAX_FRAME_CACHE_ITEMS, MAX_FRAME_CACHE_BYTES);
26const subtitleDebugStats = createSubtitleDebugStats();
27
28let parseDisplaySets = null;
29
30function createDurationStats ()
31{
32 return {
33 count: 0,
34 totalMs: 0,
35 maxMs: 0,
36 lastMs: 0,
37 };
38}
39
40function createSubtitleDebugStats ()
41{
42 return {
43 eventCacheHits: 0,
44 eventCacheMisses: 0,
45 eventCachePendingHits: 0,
46 eventCacheBuilds: 0,
47 eventCacheBuildErrors: 0,
48 frameCacheHits: 0,
49 frameCacheMisses: 0,
50 frameRenders: 0,
51 eventBuildDurationMs: createDurationStats(),
52 frameRenderDurationMs: createDurationStats(),
53 frameRequestDurationMs: createDurationStats(),
54 };
55}
56
57function recordDuration (stats, startedAt)
58{
59 if (!stats || !Number.isFinite(startedAt)) return 0;
60
61 const elapsedMs = Math.max(0, Date.now() - startedAt);
62 stats.count += 1;
63 stats.totalMs += elapsedMs;
64 stats.lastMs = elapsedMs;
65 if (elapsedMs > stats.maxMs) stats.maxMs = elapsedMs;
66 return elapsedMs;
67}
68
69function averageDurationMs (stats)
70{
71 if (!stats || !stats.count) return 0;
72 return Number((stats.totalMs / stats.count).toFixed(1));
73}
74
75function debugLimitValue (value)
76{
77 return Number.isFinite(value) ? value : null;
78}
79
80function ensureImageDataGlobal ()
81{
82 if (typeof globalThis.ImageData === 'function') return;
83
84 class ImageDataPolyfill
85 {
86 constructor(data, width, height)
87 {
88 this.data = data;
89 this.width = width;
90 this.height = height;
91 }
92 }
93
94 globalThis.ImageData = ImageDataPolyfill;
95}
96
97function loadPgsParser ()
98{
99 if (parseDisplaySets) return parseDisplaySets;
100 ensureImageDataGlobal();
101 ({ parseDisplaySets } = require('pgs-parser'));
102 return parseDisplaySets;
103}
104
105function ffmpegPath ()
106{
107 return process.env.FFMPEG_PATH || 'ffmpeg';
108}
109
110function ensureSubtitleCacheDir ()
111{
112 fs.mkdirSync(SUBTITLE_CACHE_DIR, { recursive: true });
113}
114
115function toPositiveInt (value, fallback)
116{
117 const number = Number.parseInt(String(value || ''), 10);
118 if (Number.isInteger(number) && number > 0) return number;
119 return fallback;
120}
121
122function toCacheItemLimit (value, fallback)
123{
124 const raw = String(value || '').trim().toLowerCase();
125 if (!raw) return fallback;
126 if (raw === '0' || raw === 'none' || raw === 'infinity' || raw === 'inf' || raw === 'unbounded' || raw === 'unlimited')
127 {
128 return Infinity;
129 }
130
131 const number = Number.parseInt(raw, 10);
132 if (Number.isInteger(number) && number > 0) return number;
133 return fallback;
134}
135
136function toBoundedNumber (value, fallback, min, max)
137{
138 const numeric = Number.parseFloat(String(value || ''));
139 if (!Number.isFinite(numeric)) return fallback;
140 if (numeric < min || numeric > max) return fallback;
141 return numeric;
142}
143
144function hashSubtitleKey (filePath, streamIndex, stat)
145{
146 return crypto
147 .createHash('sha1')
148 .update(`${filePath}|${streamIndex}|${stat.size}|${Math.floor(stat.mtimeMs)}`)
149 .digest('hex');
150}
151
152function createCacheState (maxItems, maxBytes)
153{
154 return {
155 map: new Map(),
156 totalBytes: 0,
157 maxItems,
158 maxBytes,
159 };
160}
161
162function normaliseFilePath (filePath)
163{
164 return path.resolve(String(filePath || '').trim());
165}
166
167function cachePrefixForFile (filePath)
168{
169 const normalised = String(filePath || '').trim();
170 if (!normalised) return '';
171 return `${normaliseFilePath(normalised)}|`;
172}
173
174function eventIdentityKey (filePath, streamIndex, stat)
175{
176 const normalisedPath = normaliseFilePath(filePath);
177 const hash = hashSubtitleKey(normalisedPath, streamIndex, stat);
178 return `${normalisedPath}|${streamIndex}|${hash}`;
179}
180
181function eventCacheFileKey (identityKey)
182{
183 const hash = crypto.createHash('sha1').update(String(identityKey || '')).digest('hex');
184 return `event-${hash}`;
185}
186
187function toValidCacheSize (value)
188{
189 const number = Number(value);
190 if (!Number.isFinite(number) || number <= 0) return 0;
191 return Math.floor(number);
192}
193
194function estimateEventCacheBytes (value)
195{
196 if (!value) return 0;
197 if (typeof value.then === 'function') return 1024;
198 if (!Array.isArray(value.events)) return 1024;
199
200 let bytes = 1024;
201 for (const event of value.events)
202 {
203 if (event && Buffer.isBuffer(event.imagePng)) bytes += event.imagePng.length;
204 bytes += 160;
205 }
206 return bytes;
207}
208
209function estimateFrameCacheBytes (value)
210{
211 if (!value) return 0;
212 if (Buffer.isBuffer(value)) return value.length;
213 if (value && Buffer.isBuffer(value.frame)) return value.frame.length + 96;
214 return 1024;
215}
216
217function deleteCachedValue (cache, key)
218{
219 const entry = cache.map.get(key);
220 if (!entry) return false;
221
222 cache.map.delete(key);
223 cache.totalBytes = Math.max(0, cache.totalBytes - toValidCacheSize(entry.sizeBytes));
224 return true;
225}
226
227function touchCachedEntry (cache, key, ttlMs)
228{
229 const entry = cache.map.get(key);
230 if (!entry) return;
231 entry.expiresAt = Date.now() + ttlMs;
232 cache.map.delete(key);
233 cache.map.set(key, entry);
234}
235
236function getCachedValue (cache, key)
237{
238 const now = Date.now();
239 const entry = cache.map.get(key);
240 if (!entry) return null;
241
242 if (entry.expiresAt <= now)
243 {
244 deleteCachedValue(cache, key);
245 return null;
246 }
247
248 cache.map.delete(key);
249 cache.map.set(key, entry);
250 return entry.value;
251}
252
253function setCachedValue (cache, key, value, ttlMs, estimateBytes)
254{
255 const now = Date.now();
256 deleteCachedValue(cache, key);
257
258 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
259
260 cache.map.set(key, {
261 value,
262 expiresAt: now + ttlMs,
263 sizeBytes,
264 });
265 cache.totalBytes += sizeBytes;
266
267 while (cache.map.size > cache.maxItems || cache.maxBytes !== Infinity && cache.totalBytes > cache.maxBytes)
268 {
269 const oldestKey = cache.map.keys().next().value;
270 deleteCachedValue(cache, oldestKey);
271 }
272}
273
274function runCommand (command, args)
275{
276 return new Promise((resolve, reject) =>
277 {
278 const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
279 let stderr = '';
280
281 child.stderr.on('data', (chunk) =>
282 {
283 stderr += chunk.toString();
284 if (stderr.length > 16000) stderr = stderr.slice(-16000);
285 });
286
287 child.on('error', reject);
288 child.on('close', (code) =>
289 {
290 if (code === 0)
291 {
292 resolve();
293 return;
294 }
295 reject(new Error(stderr.trim() || `ffmpeg exited with code ${code}`));
296 });
297 });
298}
299
300async function extractSupFile (sourcePath, streamIndex, cacheKey)
301{
302 ensureSubtitleCacheDir();
303 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
304
305 if (fs.existsSync(supPath) && fs.statSync(supPath).size > 0)
306 {
307 return supPath;
308 }
309
310 await runCommand(ffmpegPath(), [
311 '-hide_banner',
312 '-loglevel', 'error',
313 '-nostdin',
314 '-y',
315 '-i', sourcePath,
316 '-map', `0:s:${streamIndex}`,
317 '-c:s', 'copy',
318 '-f', 'sup',
319 supPath,
320 ]);
321
322 return supPath;
323}
324
325function toSeconds (pgsTimestamp)
326{
327 const numeric = Number(pgsTimestamp);
328 if (!Number.isFinite(numeric) || numeric < 0) return 0;
329 return numeric / PGS_TIMEBASE;
330}
331
332function parseDisplaySetTimestamp (displaySet)
333{
334 if (displaySet && displaySet.presentationCompositionSegment && displaySet.presentationCompositionSegment.header)
335 {
336 return toSeconds(displaySet.presentationCompositionSegment.header.presentationTimestamp);
337 }
338 if (displaySet && displaySet.endDefinitionSegment && displaySet.endDefinitionSegment.header)
339 {
340 return toSeconds(displaySet.endDefinitionSegment.header.presentationTimestamp);
341 }
342 return 0;
343}
344
345async function parseSupEvents (supPath)
346{
347 const parserFactory = loadPgsParser();
348 if (typeof Readable.toWeb !== 'function')
349 {
350 throw new Error('Node.js stream bridge is unavailable for subtitle parsing.');
351 }
352
353 const subtitleStream = Readable.toWeb(fs.createReadStream(supPath));
354 const parserStream = subtitleStream.pipeThrough(parserFactory());
355 const reader = parserStream.getReader();
356
357 const events = [];
358 let currentEvent = null;
359 let scratchBuffer = null;
360
361 while (true)
362 {
363 const { done, value: displaySet } = await reader.read();
364 if (done) break;
365
366 const timestampSeconds = parseDisplaySetTimestamp(displaySet);
367
368 if (currentEvent && timestampSeconds > currentEvent.startTimeSeconds)
369 {
370 currentEvent.endTimeSeconds = timestampSeconds;
371 events.push(currentEvent);
372 currentEvent = null;
373 }
374
375 if (!displaySet || !Array.isArray(displaySet.objectDefinitionSegments) || displaySet.objectDefinitionSegments.length === 0)
376 {
377 continue;
378 }
379
380 const pcs = displaySet.presentationCompositionSegment;
381 if (!pcs) continue;
382
383 const neededPixels = Number(pcs.width || 0) * Number(pcs.height || 0) * 4;
384 if (!scratchBuffer || !Number.isInteger(neededPixels) || neededPixels <= 0 || scratchBuffer.length < neededPixels)
385 {
386 scratchBuffer = neededPixels > 0 ? new Uint8ClampedArray(neededPixels) : null;
387 }
388
389 let imageData;
390 try
391 {
392 imageData = displaySet.imageData(scratchBuffer || undefined);
393 } catch
394 {
395 continue;
396 }
397
398 if (!imageData || !imageData.data || !Number.isInteger(imageData.width) || !Number.isInteger(imageData.height))
399 {
400 continue;
401 }
402
403 const rawPixels = Buffer.from(imageData.data);
404 const imagePng = await sharp(rawPixels, {
405 raw: {
406 width: imageData.width,
407 height: imageData.height,
408 channels: 4,
409 },
410 }).png().toBuffer();
411
412 currentEvent = {
413 startTimeSeconds: timestampSeconds,
414 endTimeSeconds: Number.POSITIVE_INFINITY,
415 screenWidth: toPositiveInt(pcs.width, imageData.width),
416 screenHeight: toPositiveInt(pcs.height, imageData.height),
417 left: toPositiveInt(pcs.objectHorizontalPosition, 0),
418 top: toPositiveInt(pcs.objectVerticalPosition, 0),
419 imageWidth: imageData.width,
420 imageHeight: imageData.height,
421 imagePng,
422 };
423 }
424
425 if (currentEvent)
426 {
427 events.push(currentEvent);
428 }
429
430 return events;
431}
432
433async function buildEventCacheEntry (filePath, streamIndex, identityKey)
434{
435 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
436 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
437 const events = await parseSupEvents(supPath);
438 return {
439 cacheKey: identityKey,
440 events,
441 };
442}
443
444function beginEventCacheBuild (normalisedPath, streamIndex, identityKey)
445{
446 subtitleDebugStats.eventCacheBuilds += 1;
447 const buildStartedAt = Date.now();
448 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
449 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
450
451 pending
452 .then((resolved) =>
453 {
454 recordDuration(subtitleDebugStats.eventBuildDurationMs, buildStartedAt);
455 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
456 return resolved;
457 })
458 .catch(() =>
459 {
460 subtitleDebugStats.eventCacheBuildErrors += 1;
461 deleteCachedValue(eventCache, identityKey);
462 });
463
464 return pending;
465}
466
467async function getEventCacheEntry (filePath, streamIndex, options = {})
468{
469 const waitForBuild = options.waitForBuild !== false;
470 const normalisedPath = normaliseFilePath(filePath);
471 const stat = await fs.promises.stat(normalisedPath);
472 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
473
474 const cached = getCachedValue(eventCache, identityKey);
475 if (cached)
476 {
477 if (typeof cached.then === 'function') subtitleDebugStats.eventCachePendingHits += 1;
478 else subtitleDebugStats.eventCacheHits += 1;
479 touchCachedEntry(eventCache, identityKey, EVENT_CACHE_TTL_MS);
480
481 if (typeof cached.then === 'function')
482 {
483 return waitForBuild ? cached : null;
484 }
485
486 return cached;
487 }
488
489 subtitleDebugStats.eventCacheMisses += 1;
490 const pending = beginEventCacheBuild(normalisedPath, streamIndex, identityKey);
491 return waitForBuild ? pending : null;
492}
493
494function findActiveEventIndex (events, timeSeconds)
495{
496 let left = 0;
497 let right = events.length - 1;
498 let candidate = -1;
499
500 while (left <= right)
501 {
502 const middle = Math.floor((left + right) / 2);
503 const event = events[middle];
504
505 if (event.startTimeSeconds <= timeSeconds)
506 {
507 candidate = middle;
508 left = middle + 1;
509 } else
510 {
511 right = middle - 1;
512 }
513 }
514
515 if (candidate < 0) return -1;
516
517 const active = events[candidate];
518 if (timeSeconds >= active.endTimeSeconds) return -1;
519 return candidate;
520}
521
522async function renderEventFrame (event, canvasWidth, canvasHeight)
523{
524 const sourceWidth = Math.max(1, toPositiveInt(event.screenWidth, canvasWidth));
525 const sourceHeight = Math.max(1, toPositiveInt(event.screenHeight, canvasHeight));
526
527 const scaleX = canvasWidth / sourceWidth;
528 const scaleY = canvasHeight / sourceHeight;
529
530 const targetWidth = Math.max(1, Math.round(event.imageWidth * scaleX));
531 const targetHeight = Math.max(1, Math.round(event.imageHeight * scaleY));
532 const left = Math.round(event.left * scaleX);
533 const top = Math.round(event.top * scaleY);
534 const right = left + targetWidth;
535 const bottom = top + targetHeight;
536
537 const visibleLeft = Math.max(0, left);
538 const visibleTop = Math.max(0, top);
539 const visibleRight = Math.min(canvasWidth, right);
540 const visibleBottom = Math.min(canvasHeight, bottom);
541 const visibleWidth = Math.max(0, visibleRight - visibleLeft);
542 const visibleHeight = Math.max(0, visibleBottom - visibleTop);
543
544 if (visibleWidth <= 0 || visibleHeight <= 0)
545 {
546 return null;
547 }
548
549 let imageBuffer = (targetWidth === event.imageWidth && targetHeight === event.imageHeight)
550 ? event.imagePng
551 : await sharp(event.imagePng).resize({ width: targetWidth, height: targetHeight, fit: 'fill' }).png().toBuffer();
552
553 if (visibleWidth < targetWidth || visibleHeight < targetHeight)
554 {
555 imageBuffer = await sharp(imageBuffer)
556 .extract({
557 left: Math.max(0, visibleLeft - left),
558 top: Math.max(0, visibleTop - top),
559 width: visibleWidth,
560 height: visibleHeight,
561 })
562 .png()
563 .toBuffer();
564 }
565
566 return {
567 frame: imageBuffer,
568 left: visibleLeft,
569 top: visibleTop,
570 width: visibleWidth,
571 height: visibleHeight,
572 canvasWidth,
573 canvasHeight,
574 };
575}
576
577function parseRenderOptions (options)
578{
579 const input = options || {};
580 const filePath = String(input.filePath ?? '').trim();
581 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
582 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
583 const requestedWidth = Math.max(1, toPositiveInt(input.canvasWidth ?? '', 1280));
584 const requestedHeight = Math.max(1, toPositiveInt(input.canvasHeight ?? '', 720));
585 const scale = Math.min(1, 1280 / requestedWidth, 720 / requestedHeight) * IMAGE_SUBTITLE_RENDER_SCALE;
586 const canvasWidth = Math.max(1, Math.round(requestedWidth * scale));
587 const canvasHeight = Math.max(1, Math.round(requestedHeight * scale));
588
589 return {
590 filePath,
591 streamIndex,
592 timeSeconds,
593 canvasWidth,
594 canvasHeight,
595 isValid: !!(filePath && Number.isInteger(streamIndex) && streamIndex >= 0 && Number.isFinite(timeSeconds) && timeSeconds >= 0),
596 };
597}
598
599async function renderImageSubtitleFrame (options)
600{
601 const requestStartedAt = Date.now();
602 const parsed = parseRenderOptions(options);
603 const { filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight } = parsed;
604
605 if (!parsed.isValid)
606 {
607 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
608 return null;
609 }
610
611 const entry = await getEventCacheEntry(filePath, streamIndex, { waitForBuild: false });
612 if (!entry || !Array.isArray(entry.events) || entry.events.length === 0)
613 {
614 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
615 return null;
616 }
617
618 const eventIndex = findActiveEventIndex(entry.events, timeSeconds);
619 if (eventIndex < 0)
620 {
621 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
622 return null;
623 }
624
625 const frameKey = `${entry.cacheKey}|${eventIndex}|${canvasWidth}x${canvasHeight}`;
626 const cachedFrame = getCachedValue(frameCache, frameKey);
627 if (cachedFrame)
628 {
629 subtitleDebugStats.frameCacheHits += 1;
630 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
631 return cachedFrame;
632 }
633
634 subtitleDebugStats.frameCacheMisses += 1;
635 subtitleDebugStats.frameRenders += 1;
636 const renderStartedAt = Date.now();
637 const frameData = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
638 if (!frameData)
639 {
640 recordDuration(subtitleDebugStats.frameRenderDurationMs, renderStartedAt);
641 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
642 return null;
643 }
644 recordDuration(subtitleDebugStats.frameRenderDurationMs, renderStartedAt);
645
646 const event = entry.events[eventIndex];
647 const cacheValue = {
648 frame: frameData.frame,
649 left: frameData.left,
650 top: frameData.top,
651 width: frameData.width,
652 height: frameData.height,
653 canvasWidth: frameData.canvasWidth,
654 canvasHeight: frameData.canvasHeight,
655 startTimeSeconds: event.startTimeSeconds,
656 endTimeSeconds: event.endTimeSeconds,
657 };
658 setCachedValue(
659 frameCache,
660 frameKey,
661 cacheValue,
662 FRAME_CACHE_TTL_MS,
663 estimateFrameCacheBytes,
664 );
665 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
666 return cacheValue;
667}
668
669function deleteSupFileForKey (identityKey)
670{
671 const cacheKey = eventCacheFileKey(identityKey);
672 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
673 fs.unlink(supPath, () =>
674 {
675 // Best-effort; file may not exist if extraction never completed.
676 });
677}
678
679function purgeSubtitleCacheDir ()
680{
681 try
682 {
683 fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });
684 } catch
685 {
686 // Best-effort; directory may not exist.
687 }
688}
689
690function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
691{
692 const prefix = cachePrefixForFile(filePath);
693 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
694
695 // Prune frameCache as before
696 const keysToDelete = [];
697 for (const key of frameCache.map.keys())
698 {
699 if (!key.startsWith(prefix)) continue;
700 const entry = frameCache.map.get(key);
701 if (!entry) continue;
702 const eventEntry = entry.value || {};
703 const { startTimeSeconds, endTimeSeconds } = eventEntry;
704 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
705 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
706 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
707 if (slowestViewerTimeSeconds >= dropThreshold)
708 {
709 keysToDelete.push(key);
710 }
711 }
712 for (const key of keysToDelete)
713 {
714 deleteCachedValue(frameCache, key);
715 }
716
717 // Smart eventCache pruning: only prune if over cap
718 let totalBytes = eventCache.totalBytes;
719 if (totalBytes <= MAX_EVENT_CACHE_BYTES) return;
720 // For each entry, prune events that end before (slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS)
721 for (const [key, entry] of eventCache.map.entries())
722 {
723 if (!key.startsWith(prefix)) continue;
724 const value = entry.value;
725 if (!value || !Array.isArray(value.events)) continue;
726 let events = value.events;
727 // Find first event that ends after the retention threshold
728 const minKeepTime = Math.max(0, slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS);
729 let pruneIdx = 0;
730 while (pruneIdx < events.length && events[pruneIdx].endTimeSeconds < minKeepTime)
731 {
732 pruneIdx++;
733 }
734 if (pruneIdx > 0)
735 {
736 // Remove pruned events and update memory usage
737 const removed = events.splice(0, pruneIdx);
738 for (const ev of removed)
739 {
740 if (ev && Buffer.isBuffer(ev.imagePng))
741 {
742 entry.sizeBytes -= ev.imagePng.length + 160;
743 eventCache.totalBytes -= ev.imagePng.length + 160;
744 }
745 }
746 }
747 // If all events are pruned, remove the entry
748 if (events.length === 0)
749 {
750 deleteSupFileForKey(key);
751 deleteCachedValue(eventCache, key);
752 }
753 // Stop pruning if under cap
754 if (eventCache.totalBytes <= MAX_EVENT_CACHE_BYTES) break;
755 }
756}
757
758function clearCachesForFile (filePath)
759{
760 const prefix = cachePrefixForFile(filePath);
761 if (!prefix) return;
762
763 const eventKeysToDelete = [];
764 const frameKeysToDelete = [];
765
766 for (const key of eventCache.map.keys())
767 {
768 if (key.startsWith(prefix)) eventKeysToDelete.push(key);
769 }
770
771 for (const key of frameCache.map.keys())
772 {
773 if (key.startsWith(prefix)) frameKeysToDelete.push(key);
774 }
775
776 for (const key of eventKeysToDelete)
777 {
778 deleteSupFileForKey(key);
779 deleteCachedValue(eventCache, key);
780 }
781
782 for (const key of frameKeysToDelete)
783 {
784 deleteCachedValue(frameCache, key);
785 }
786}
787
788function filePathFromCacheKey (key)
789{
790 const raw = String(key || '');
791 const divider = raw.indexOf('|');
792 if (divider < 0) return '';
793 return raw.slice(0, divider);
794}
795
796function getImageSubtitleDebugSnapshot ()
797{
798 const byFile = {};
799
800 for (const [key, entry] of eventCache.map.entries())
801 {
802 const filePath = filePathFromCacheKey(key);
803 if (!filePath) continue;
804
805 const row = byFile[filePath] || {
806 filePath,
807 eventEntries: 0,
808 eventBytes: 0,
809 eventCount: 0,
810 eventImageBytes: 0,
811 frameEntries: 0,
812 frameBytes: 0,
813 };
814
815 row.eventEntries += 1;
816 row.eventBytes += Number(entry && entry.sizeBytes) || 0;
817
818 const value = entry && entry.value;
819 if (value && Array.isArray(value.events))
820 {
821 row.eventCount += value.events.length;
822 for (const event of value.events)
823 {
824 if (event && Buffer.isBuffer(event.imagePng))
825 {
826 row.eventImageBytes += event.imagePng.length;
827 }
828 }
829 }
830
831 byFile[filePath] = row;
832 }
833
834 for (const [key, entry] of frameCache.map.entries())
835 {
836 const filePath = filePathFromCacheKey(key);
837 if (!filePath) continue;
838
839 const row = byFile[filePath] || {
840 filePath,
841 eventEntries: 0,
842 eventBytes: 0,
843 eventCount: 0,
844 eventImageBytes: 0,
845 frameEntries: 0,
846 frameBytes: 0,
847 };
848
849 row.frameEntries += 1;
850 row.frameBytes += Number(entry && entry.sizeBytes) || 0;
851
852 byFile[filePath] = row;
853 }
854
855 return {
856 eventCache: {
857 entries: eventCache.map.size,
858 bytes: eventCache.totalBytes,
859 maxItems: debugLimitValue(eventCache.maxItems),
860 maxBytes: eventCache.maxBytes,
861 },
862 frameCache: {
863 entries: frameCache.map.size,
864 bytes: frameCache.totalBytes,
865 maxItems: debugLimitValue(frameCache.maxItems),
866 maxBytes: frameCache.maxBytes,
867 },
868 metrics: {
869 eventCacheHits: subtitleDebugStats.eventCacheHits,
870 eventCacheMisses: subtitleDebugStats.eventCacheMisses,
871 eventCachePendingHits: subtitleDebugStats.eventCachePendingHits,
872 eventCacheBuilds: subtitleDebugStats.eventCacheBuilds,
873 eventCacheBuildErrors: subtitleDebugStats.eventCacheBuildErrors,
874 frameCacheHits: subtitleDebugStats.frameCacheHits,
875 frameCacheMisses: subtitleDebugStats.frameCacheMisses,
876 frameRenders: subtitleDebugStats.frameRenders,
877 eventBuildDurationMs: {
878 last: subtitleDebugStats.eventBuildDurationMs.lastMs,
879 max: subtitleDebugStats.eventBuildDurationMs.maxMs,
880 average: averageDurationMs(subtitleDebugStats.eventBuildDurationMs),
881 },
882 frameRenderDurationMs: {
883 last: subtitleDebugStats.frameRenderDurationMs.lastMs,
884 max: subtitleDebugStats.frameRenderDurationMs.maxMs,
885 average: averageDurationMs(subtitleDebugStats.frameRenderDurationMs),
886 },
887 frameRequestDurationMs: {
888 last: subtitleDebugStats.frameRequestDurationMs.lastMs,
889 max: subtitleDebugStats.frameRequestDurationMs.maxMs,
890 average: averageDurationMs(subtitleDebugStats.frameRequestDurationMs),
891 },
892 },
893 byFile,
894 };
895}
896
897module.exports = {
898 parseRenderOptions,
899 renderImageSubtitleFrame,
900 clearCachesForFile,
901 pruneOldImageSubtitleCaches,
902 purgeSubtitleCacheDir,
903 getImageSubtitleDebugSnapshot,
904};