BlueGit

timfa filemanager

Reduce image subtitle cache memory and fix cache key pruning

timfa Tim Falken · pushed by timfa

Authored 2026-05-18 · 6 months ago

Hash
ee49686447cd7d91b7c0d04e63e75815c30c8c3c
Parent
54e35287
3 files changed +157 −55
.github/technical-reference.md +3 −3
@@ -10,10 +10,10 @@
10 10 - logError(message, error, details) - Appends error lines to logs.txt, trimming old lines so the file stays under 1 MB; use for route failures and process-level exceptions.
11 11
12 12 ## server/image-subtitles.js
13 - parseRenderOptions(options) - Normalises subtitle-frame request options and validates filePath/streamIndex/time/canvas dimensions; stream index `0` is valid and must not be treated as missing.
13 - parseRenderOptions(options) - Normalises subtitle-frame request options and validates filePath/streamIndex/time/canvas dimensions; stream index `0` is valid and must not be treated as missing; canvas is capped at 1280x720 to bound subtitle frame memory/CPU cost.
14 14 - renderImageSubtitleFrame({ filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight }) - Extracts bitmap subtitle streams to cached `.sup`, parses PGS display sets, and returns cached/composited PNG subtitle frames for a requested timestamp; returns null when no subtitle is active.
15 - clearCachesForFile(filePath) - Removes all event and frame cache entries for a given file path; called when video stream sessions end to prevent memory accumulation.
16 - pruneOldImageSubtitleCaches(filePath, slowestViewerTimeSeconds) - Drops subtitle frame caches for events that have passed their retention threshold of end time + event duration + 30 seconds (e.g., event ending at 30s with 10s duration is dropped after slowest viewer reaches 70s); used during segment pruning to keep memory usage bounded on long videos, especially on resource-limited hardware like Raspberry Pi.
15 - clearCachesForFile(filePath) - Removes all event and frame cache entries for a given file path using path-prefixed cache keys and byte-accounted eviction; called when video stream sessions end to prevent memory accumulation.
16 - pruneOldImageSubtitleCaches(filePath, slowestViewerTimeSeconds) - Drops subtitle frame caches for events that have passed their retention threshold of end time + event duration + 5 seconds (e.g., event ending at 30s with 10s duration is dropped after slowest viewer reaches 45s); used during segment pruning to keep memory usage bounded on long videos, especially on resource-limited hardware like Raspberry Pi.
17 17
18 18 ## server/thumbnail-cache.js
19 19 - syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
@@ -10,10 +10,10 @@
10- logError(message, error, details) - Appends error lines to logs.txt, trimming old lines so the file stays under 1 MB; use for route failures and process-level exceptions.
10- logError(message, error, details) - Appends error lines to logs.txt, trimming old lines so the file stays under 1 MB; use for route failures and process-level exceptions.
11
11
12## server/image-subtitles.js
12## server/image-subtitles.js
13- parseRenderOptions(options) - Normalises subtitle-frame request options and validates filePath/streamIndex/time/canvas dimensions; stream index `0` is valid and must not be treated as missing.
13- parseRenderOptions(options) - Normalises subtitle-frame request options and validates filePath/streamIndex/time/canvas dimensions; stream index `0` is valid and must not be treated as missing; canvas is capped at 1280x720 to bound subtitle frame memory/CPU cost.
14- renderImageSubtitleFrame({ filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight }) - Extracts bitmap subtitle streams to cached `.sup`, parses PGS display sets, and returns cached/composited PNG subtitle frames for a requested timestamp; returns null when no subtitle is active.
14- renderImageSubtitleFrame({ filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight }) - Extracts bitmap subtitle streams to cached `.sup`, parses PGS display sets, and returns cached/composited PNG subtitle frames for a requested timestamp; returns null when no subtitle is active.
15- clearCachesForFile(filePath) - Removes all event and frame cache entries for a given file path; called when video stream sessions end to prevent memory accumulation.
15- clearCachesForFile(filePath) - Removes all event and frame cache entries for a given file path using path-prefixed cache keys and byte-accounted eviction; called when video stream sessions end to prevent memory accumulation.
16- pruneOldImageSubtitleCaches(filePath, slowestViewerTimeSeconds) - Drops subtitle frame caches for events that have passed their retention threshold of end time + event duration + 30 seconds (e.g., event ending at 30s with 10s duration is dropped after slowest viewer reaches 70s); used during segment pruning to keep memory usage bounded on long videos, especially on resource-limited hardware like Raspberry Pi.
16- pruneOldImageSubtitleCaches(filePath, slowestViewerTimeSeconds) - Drops subtitle frame caches for events that have passed their retention threshold of end time + event duration + 5 seconds (e.g., event ending at 30s with 10s duration is dropped after slowest viewer reaches 45s); used during segment pruning to keep memory usage bounded on long videos, especially on resource-limited hardware like Raspberry Pi.
17
17
18## server/thumbnail-cache.js
18## server/thumbnail-cache.js
19- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
19- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
server/image-subtitles.js +132 −44
@@ -9,15 +9,17 @@ const { Readable } = require('stream');
9 9 const sharp = require('sharp');
10 10
11 11 const SUBTITLE_CACHE_DIR = path.join(os.tmpdir(), 'filemanager-video-subtitles');
12 const EVENT_CACHE_TTL_MS = 20 * 60 * 1000;
13 const FRAME_CACHE_TTL_MS = 10 * 60 * 1000;
14 const MAX_EVENT_CACHE_ITEMS = 6;
15 const MAX_FRAME_CACHE_ITEMS = 400;
12 const EVENT_CACHE_TTL_MS = 5 * 60 * 1000;
13 const FRAME_CACHE_TTL_MS = 2 * 60 * 1000;
14 const MAX_EVENT_CACHE_ITEMS = 3;
15 const MAX_FRAME_CACHE_ITEMS = 96;
16 const MAX_EVENT_CACHE_BYTES = 96 * 1024 * 1024;
17 const MAX_FRAME_CACHE_BYTES = 48 * 1024 * 1024;
16 18 const PGS_TIMEBASE = 90000;
17 19 const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = 5;
18 20
19 const eventCache = new Map();
20 const frameCache = new Map();
21 const eventCache = createCacheState(MAX_EVENT_CACHE_ITEMS, MAX_EVENT_CACHE_BYTES);
22 const frameCache = createCacheState(MAX_FRAME_CACHE_ITEMS, MAX_FRAME_CACHE_BYTES);
21 23
22 24 let parseDisplaySets = null;
23 25
@@ -71,36 +73,116 @@ function hashSubtitleKey (filePath, streamIndex, stat)
71 73 .digest('hex');
72 74 }
73 75
76 function createCacheState (maxItems, maxBytes)
77 {
78 return {
79 map: new Map(),
80 totalBytes: 0,
81 maxItems,
82 maxBytes,
83 };
84 }
85
86 function normaliseFilePath (filePath)
87 {
88 return path.resolve(String(filePath || '').trim());
89 }
90
91 function cachePrefixForFile (filePath)
92 {
93 const normalised = String(filePath || '').trim();
94 if (!normalised) return '';
95 return `${normaliseFilePath(normalised)}|`;
96 }
97
98 function eventIdentityKey (filePath, streamIndex, stat)
99 {
100 const normalisedPath = normaliseFilePath(filePath);
101 const hash = hashSubtitleKey(normalisedPath, streamIndex, stat);
102 return `${normalisedPath}|${streamIndex}|${hash}`;
103 }
104
105 function eventCacheFileKey (identityKey)
106 {
107 const hash = crypto.createHash('sha1').update(String(identityKey || '')).digest('hex');
108 return `event-${hash}`;
109 }
110
111 function toValidCacheSize (value)
112 {
113 const number = Number(value);
114 if (!Number.isFinite(number) || number <= 0) return 0;
115 return Math.floor(number);
116 }
117
118 function estimateEventCacheBytes (value)
119 {
120 if (!value) return 0;
121 if (typeof value.then === 'function') return 1024;
122 if (!Array.isArray(value.events)) return 1024;
123
124 let bytes = 1024;
125 for (const event of value.events)
126 {
127 if (event && Buffer.isBuffer(event.imagePng)) bytes += event.imagePng.length;
128 bytes += 160;
129 }
130 return bytes;
131 }
132
133 function estimateFrameCacheBytes (value)
134 {
135 if (!value) return 0;
136 if (Buffer.isBuffer(value)) return value.length;
137 if (value && Buffer.isBuffer(value.frame)) return value.frame.length + 96;
138 return 1024;
139 }
140
141 function deleteCachedValue (cache, key)
142 {
143 const entry = cache.map.get(key);
144 if (!entry) return false;
145
146 cache.map.delete(key);
147 cache.totalBytes = Math.max(0, cache.totalBytes - toValidCacheSize(entry.sizeBytes));
148 return true;
149 }
150
74 151 function getCachedValue (cache, key)
75 152 {
76 153 const now = Date.now();
77 const entry = cache.get(key);
154 const entry = cache.map.get(key);
78 155 if (!entry) return null;
79 156
80 157 if (entry.expiresAt <= now)
81 158 {
82 cache.delete(key);
159 deleteCachedValue(cache, key);
83 160 return null;
84 161 }
85 162
86 cache.delete(key);
87 cache.set(key, entry);
163 cache.map.delete(key);
164 cache.map.set(key, entry);
88 165 return entry.value;
89 166 }
90 167
91 function setCachedValue (cache, key, value, ttlMs, maxItems)
168 function setCachedValue (cache, key, value, ttlMs, estimateBytes)
92 169 {
93 170 const now = Date.now();
94 cache.delete(key);
95 cache.set(key, {
171 deleteCachedValue(cache, key);
172
173 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
174
175 cache.map.set(key, {
96 176 value,
97 177 expiresAt: now + ttlMs,
178 sizeBytes,
98 179 });
180 cache.totalBytes += sizeBytes;
99 181
100 while (cache.size > maxItems)
182 while (cache.map.size > cache.maxItems || cache.totalBytes > cache.maxBytes)
101 183 {
102 const oldestKey = cache.keys().next().value;
103 cache.delete(oldestKey);
184 const oldestKey = cache.map.keys().next().value;
185 deleteCachedValue(cache, oldestKey);
104 186 }
105 187 }
106 188
@@ -263,22 +345,22 @@ async function parseSupEvents (supPath)
263 345 return events;
264 346 }
265 347
266 async function buildEventCacheEntry (filePath, streamIndex)
348 async function buildEventCacheEntry (filePath, streamIndex, identityKey)
267 349 {
268 const stat = await fs.promises.stat(filePath);
269 const cacheKey = hashSubtitleKey(filePath, streamIndex, stat);
270 const supPath = await extractSupFile(filePath, streamIndex, cacheKey);
350 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
351 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
271 352 const events = await parseSupEvents(supPath);
272 353 return {
273 cacheKey,
354 cacheKey: identityKey,
274 355 events,
275 356 };
276 357 }
277 358
278 359 async function getEventCacheEntry (filePath, streamIndex)
279 360 {
280 const stat = await fs.promises.stat(filePath);
281 const identityKey = hashSubtitleKey(filePath, streamIndex, stat);
361 const normalisedPath = normaliseFilePath(filePath);
362 const stat = await fs.promises.stat(normalisedPath);
363 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
282 364
283 365 const cached = getCachedValue(eventCache, identityKey);
284 366 if (cached)
@@ -286,17 +368,17 @@ async function getEventCacheEntry (filePath, streamIndex)
286 368 return cached;
287 369 }
288 370
289 const pending = buildEventCacheEntry(filePath, streamIndex);
290 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
371 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
372 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
291 373
292 374 try
293 375 {
294 376 const resolved = await pending;
295 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
377 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
296 378 return resolved;
297 379 } catch (error)
298 380 {
299 eventCache.delete(identityKey);
381 deleteCachedValue(eventCache, identityKey);
300 382 throw error;
301 383 }
302 384 }
@@ -380,8 +462,8 @@ function parseRenderOptions (options)
380 462 const filePath = String(input.filePath ?? '').trim();
381 463 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
382 464 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
383 const canvasWidth = Math.max(1, Math.min(8192, toPositiveInt(input.canvasWidth ?? '', 1280)));
384 const canvasHeight = Math.max(1, Math.min(8192, toPositiveInt(input.canvasHeight ?? '', 720)));
465 const canvasWidth = Math.max(1, Math.min(1280, toPositiveInt(input.canvasWidth ?? '', 1280)));
466 const canvasHeight = Math.max(1, Math.min(720, toPositiveInt(input.canvasHeight ?? '', 720)));
385 467
386 468 return {
387 469 filePath,
@@ -418,21 +500,27 @@ async function renderImageSubtitleFrame (options)
418 500
419 501 const frame = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
420 502 const event = entry.events[eventIndex];
421 setCachedValue(frameCache, frameKey, { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds }, FRAME_CACHE_TTL_MS, MAX_FRAME_CACHE_ITEMS);
503 setCachedValue(
504 frameCache,
505 frameKey,
506 { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds },
507 FRAME_CACHE_TTL_MS,
508 estimateFrameCacheBytes,
509 );
422 510 return frame;
423 511 }
424 512
425 513 function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
426 514 {
427 const normalised = String(filePath || '').trim();
428 if (!normalised || !Number.isFinite(slowestViewerTimeSeconds)) return;
515 const prefix = cachePrefixForFile(filePath);
516 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
429 517
430 518 const keysToDelete = [];
431 for (const key of frameCache.keys())
519 for (const key of frameCache.map.keys())
432 520 {
433 if (!key.startsWith(normalised)) continue;
521 if (!key.startsWith(prefix)) continue;
434 522
435 const entry = frameCache.get(key);
523 const entry = frameCache.map.get(key);
436 524 if (!entry) continue;
437 525
438 526 const eventEntry = entry.value || {};
@@ -451,28 +539,28 @@ function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
451 539
452 540 for (const key of keysToDelete)
453 541 {
454 frameCache.delete(key);
542 deleteCachedValue(frameCache, key);
455 543 }
456 544 }
457 545
458 546 function clearCachesForFile (filePath)
459 547 {
460 const normalised = String(filePath || '').trim();
461 if (!normalised) return;
548 const prefix = cachePrefixForFile(filePath);
549 if (!prefix) return;
462 550
463 551 const keysToDelete = [];
464 552
465 for (const key of eventCache.keys())
553 for (const key of eventCache.map.keys())
466 554 {
467 if (key.startsWith(normalised))
555 if (key.startsWith(prefix))
468 556 {
469 557 keysToDelete.push(key);
470 558 }
471 559 }
472 560
473 for (const key of frameCache.keys())
561 for (const key of frameCache.map.keys())
474 562 {
475 if (key.startsWith(normalised))
563 if (key.startsWith(prefix))
476 564 {
477 565 keysToDelete.push(key);
478 566 }
@@ -480,8 +568,8 @@ function clearCachesForFile (filePath)
480 568
481 569 for (const key of keysToDelete)
482 570 {
483 eventCache.delete(key);
484 frameCache.delete(key);
571 deleteCachedValue(eventCache, key);
572 deleteCachedValue(frameCache, key);
485 573 }
486 574 }
487 575
@@ -9,15 +9,17 @@ const { Readable } = require('stream');
9const sharp = require('sharp');
9const sharp = require('sharp');
10
10
11const SUBTITLE_CACHE_DIR = path.join(os.tmpdir(), 'filemanager-video-subtitles');
11const SUBTITLE_CACHE_DIR = path.join(os.tmpdir(), 'filemanager-video-subtitles');
12const EVENT_CACHE_TTL_MS = 20 * 60 * 1000;
12const EVENT_CACHE_TTL_MS = 5 * 60 * 1000;
13const FRAME_CACHE_TTL_MS = 10 * 60 * 1000;
13const FRAME_CACHE_TTL_MS = 2 * 60 * 1000;
14const MAX_EVENT_CACHE_ITEMS = 6;
14const MAX_EVENT_CACHE_ITEMS = 3;
15const MAX_FRAME_CACHE_ITEMS = 400;
15const MAX_FRAME_CACHE_ITEMS = 96;
16const MAX_EVENT_CACHE_BYTES = 96 * 1024 * 1024;
17const MAX_FRAME_CACHE_BYTES = 48 * 1024 * 1024;
16const PGS_TIMEBASE = 90000;
18const PGS_TIMEBASE = 90000;
17const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = 5;
19const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = 5;
18
20
19const eventCache = new Map();
21const eventCache = createCacheState(MAX_EVENT_CACHE_ITEMS, MAX_EVENT_CACHE_BYTES);
20const frameCache = new Map();
22const frameCache = createCacheState(MAX_FRAME_CACHE_ITEMS, MAX_FRAME_CACHE_BYTES);
21
23
22let parseDisplaySets = null;
24let parseDisplaySets = null;
23
25
@@ -71,36 +73,116 @@ function hashSubtitleKey (filePath, streamIndex, stat)
71 .digest('hex');
73 .digest('hex');
72}
74}
73
75
76function createCacheState (maxItems, maxBytes)
77{
78 return {
79 map: new Map(),
80 totalBytes: 0,
81 maxItems,
82 maxBytes,
83 };
84}
85
86function normaliseFilePath (filePath)
87{
88 return path.resolve(String(filePath || '').trim());
89}
90
91function cachePrefixForFile (filePath)
92{
93 const normalised = String(filePath || '').trim();
94 if (!normalised) return '';
95 return `${normaliseFilePath(normalised)}|`;
96}
97
98function eventIdentityKey (filePath, streamIndex, stat)
99{
100 const normalisedPath = normaliseFilePath(filePath);
101 const hash = hashSubtitleKey(normalisedPath, streamIndex, stat);
102 return `${normalisedPath}|${streamIndex}|${hash}`;
103}
104
105function eventCacheFileKey (identityKey)
106{
107 const hash = crypto.createHash('sha1').update(String(identityKey || '')).digest('hex');
108 return `event-${hash}`;
109}
110
111function toValidCacheSize (value)
112{
113 const number = Number(value);
114 if (!Number.isFinite(number) || number <= 0) return 0;
115 return Math.floor(number);
116}
117
118function estimateEventCacheBytes (value)
119{
120 if (!value) return 0;
121 if (typeof value.then === 'function') return 1024;
122 if (!Array.isArray(value.events)) return 1024;
123
124 let bytes = 1024;
125 for (const event of value.events)
126 {
127 if (event && Buffer.isBuffer(event.imagePng)) bytes += event.imagePng.length;
128 bytes += 160;
129 }
130 return bytes;
131}
132
133function estimateFrameCacheBytes (value)
134{
135 if (!value) return 0;
136 if (Buffer.isBuffer(value)) return value.length;
137 if (value && Buffer.isBuffer(value.frame)) return value.frame.length + 96;
138 return 1024;
139}
140
141function deleteCachedValue (cache, key)
142{
143 const entry = cache.map.get(key);
144 if (!entry) return false;
145
146 cache.map.delete(key);
147 cache.totalBytes = Math.max(0, cache.totalBytes - toValidCacheSize(entry.sizeBytes));
148 return true;
149}
150
74function getCachedValue (cache, key)
151function getCachedValue (cache, key)
75{
152{
76 const now = Date.now();
153 const now = Date.now();
77 const entry = cache.get(key);
154 const entry = cache.map.get(key);
78 if (!entry) return null;
155 if (!entry) return null;
79
156
80 if (entry.expiresAt <= now)
157 if (entry.expiresAt <= now)
81 {
158 {
82 cache.delete(key);
159 deleteCachedValue(cache, key);
83 return null;
160 return null;
84 }
161 }
85
162
86 cache.delete(key);
163 cache.map.delete(key);
87 cache.set(key, entry);
164 cache.map.set(key, entry);
88 return entry.value;
165 return entry.value;
89}
166}
90
167
91function setCachedValue (cache, key, value, ttlMs, maxItems)
168function setCachedValue (cache, key, value, ttlMs, estimateBytes)
92{
169{
93 const now = Date.now();
170 const now = Date.now();
94 cache.delete(key);
171 deleteCachedValue(cache, key);
95 cache.set(key, {
172
173 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
174
175 cache.map.set(key, {
96 value,
176 value,
97 expiresAt: now + ttlMs,
177 expiresAt: now + ttlMs,
178 sizeBytes,
98 });
179 });
180 cache.totalBytes += sizeBytes;
99
181
100 while (cache.size > maxItems)
182 while (cache.map.size > cache.maxItems || cache.totalBytes > cache.maxBytes)
101 {
183 {
102 const oldestKey = cache.keys().next().value;
184 const oldestKey = cache.map.keys().next().value;
103 cache.delete(oldestKey);
185 deleteCachedValue(cache, oldestKey);
104 }
186 }
105}
187}
106
188
@@ -263,22 +345,22 @@ async function parseSupEvents (supPath)
263 return events;
345 return events;
264}
346}
265
347
266async function buildEventCacheEntry (filePath, streamIndex)
348async function buildEventCacheEntry (filePath, streamIndex, identityKey)
267{
349{
268 const stat = await fs.promises.stat(filePath);
350 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
269 const cacheKey = hashSubtitleKey(filePath, streamIndex, stat);
351 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
270 const supPath = await extractSupFile(filePath, streamIndex, cacheKey);
271 const events = await parseSupEvents(supPath);
352 const events = await parseSupEvents(supPath);
272 return {
353 return {
273 cacheKey,
354 cacheKey: identityKey,
274 events,
355 events,
275 };
356 };
276}
357}
277
358
278async function getEventCacheEntry (filePath, streamIndex)
359async function getEventCacheEntry (filePath, streamIndex)
279{
360{
280 const stat = await fs.promises.stat(filePath);
361 const normalisedPath = normaliseFilePath(filePath);
281 const identityKey = hashSubtitleKey(filePath, streamIndex, stat);
362 const stat = await fs.promises.stat(normalisedPath);
363 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
282
364
283 const cached = getCachedValue(eventCache, identityKey);
365 const cached = getCachedValue(eventCache, identityKey);
284 if (cached)
366 if (cached)
@@ -286,17 +368,17 @@ async function getEventCacheEntry (filePath, streamIndex)
286 return cached;
368 return cached;
287 }
369 }
288
370
289 const pending = buildEventCacheEntry(filePath, streamIndex);
371 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
290 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
372 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
291
373
292 try
374 try
293 {
375 {
294 const resolved = await pending;
376 const resolved = await pending;
295 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
377 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
296 return resolved;
378 return resolved;
297 } catch (error)
379 } catch (error)
298 {
380 {
299 eventCache.delete(identityKey);
381 deleteCachedValue(eventCache, identityKey);
300 throw error;
382 throw error;
301 }
383 }
302}
384}
@@ -380,8 +462,8 @@ function parseRenderOptions (options)
380 const filePath = String(input.filePath ?? '').trim();
462 const filePath = String(input.filePath ?? '').trim();
381 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
463 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
382 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
464 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
383 const canvasWidth = Math.max(1, Math.min(8192, toPositiveInt(input.canvasWidth ?? '', 1280)));
465 const canvasWidth = Math.max(1, Math.min(1280, toPositiveInt(input.canvasWidth ?? '', 1280)));
384 const canvasHeight = Math.max(1, Math.min(8192, toPositiveInt(input.canvasHeight ?? '', 720)));
466 const canvasHeight = Math.max(1, Math.min(720, toPositiveInt(input.canvasHeight ?? '', 720)));
385
467
386 return {
468 return {
387 filePath,
469 filePath,
@@ -418,21 +500,27 @@ async function renderImageSubtitleFrame (options)
418
500
419 const frame = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
501 const frame = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
420 const event = entry.events[eventIndex];
502 const event = entry.events[eventIndex];
421 setCachedValue(frameCache, frameKey, { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds }, FRAME_CACHE_TTL_MS, MAX_FRAME_CACHE_ITEMS);
503 setCachedValue(
504 frameCache,
505 frameKey,
506 { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds },
507 FRAME_CACHE_TTL_MS,
508 estimateFrameCacheBytes,
509 );
422 return frame;
510 return frame;
423}
511}
424
512
425function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
513function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
426{
514{
427 const normalised = String(filePath || '').trim();
515 const prefix = cachePrefixForFile(filePath);
428 if (!normalised || !Number.isFinite(slowestViewerTimeSeconds)) return;
516 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
429
517
430 const keysToDelete = [];
518 const keysToDelete = [];
431 for (const key of frameCache.keys())
519 for (const key of frameCache.map.keys())
432 {
520 {
433 if (!key.startsWith(normalised)) continue;
521 if (!key.startsWith(prefix)) continue;
434
522
435 const entry = frameCache.get(key);
523 const entry = frameCache.map.get(key);
436 if (!entry) continue;
524 if (!entry) continue;
437
525
438 const eventEntry = entry.value || {};
526 const eventEntry = entry.value || {};
@@ -451,28 +539,28 @@ function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
451
539
452 for (const key of keysToDelete)
540 for (const key of keysToDelete)
453 {
541 {
454 frameCache.delete(key);
542 deleteCachedValue(frameCache, key);
455 }
543 }
456}
544}
457
545
458function clearCachesForFile (filePath)
546function clearCachesForFile (filePath)
459{
547{
460 const normalised = String(filePath || '').trim();
548 const prefix = cachePrefixForFile(filePath);
461 if (!normalised) return;
549 if (!prefix) return;
462
550
463 const keysToDelete = [];
551 const keysToDelete = [];
464
552
465 for (const key of eventCache.keys())
553 for (const key of eventCache.map.keys())
466 {
554 {
467 if (key.startsWith(normalised))
555 if (key.startsWith(prefix))
468 {
556 {
469 keysToDelete.push(key);
557 keysToDelete.push(key);
470 }
558 }
471 }
559 }
472
560
473 for (const key of frameCache.keys())
561 for (const key of frameCache.map.keys())
474 {
562 {
475 if (key.startsWith(normalised))
563 if (key.startsWith(prefix))
476 {
564 {
477 keysToDelete.push(key);
565 keysToDelete.push(key);
478 }
566 }
@@ -480,8 +568,8 @@ function clearCachesForFile (filePath)
480
568
481 for (const key of keysToDelete)
569 for (const key of keysToDelete)
482 {
570 {
483 eventCache.delete(key);
571 deleteCachedValue(eventCache, key);
484 frameCache.delete(key);
572 deleteCachedValue(frameCache, key);
485 }
573 }
486}
574}
487
575
tests/image-subtitles.test.js +22 −8
@@ -30,27 +30,41 @@ describe('image subtitle option parsing', () =>
30 30 expect(parsed.isValid).toBe(false);
31 31 });
32 32
33 test('prune calculates drop threshold as end plus duration plus 30 seconds', () =>
33 test('caps subtitle render canvas to 1280x720', () =>
34 34 {
35 // When a subtitle event is 20-30s (10s duration), it should be dropped after 70s
36 // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 30
35 const parsed = parseRenderOptions({
36 filePath: '/tmp/example.mkv',
37 streamIndex: 0,
38 timeSeconds: 5,
39 canvasWidth: 4096,
40 canvasHeight: 2160,
41 });
42
43 expect(parsed.canvasWidth).toBe(1280);
44 expect(parsed.canvasHeight).toBe(720);
45 });
46
47 test('prune calculates drop threshold as end plus duration plus 5 seconds', () =>
48 {
49 // When a subtitle event is 20-30s (10s duration), it should be dropped after 45s
50 // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 5
37 51 const startTime = 20;
38 52 const endTime = 30;
39 53 const duration = endTime - startTime;
40 const dropThreshold = endTime + duration + 30;
54 const dropThreshold = endTime + duration + 5;
41 55
42 expect(dropThreshold).toBe(70);
56 expect(dropThreshold).toBe(45);
43 57
44 58 // Before threshold: subtitle should be kept
45 const beforeThreshold = 69.999;
59 const beforeThreshold = 44.999;
46 60 expect(beforeThreshold < dropThreshold).toBe(true);
47 61
48 62 // At or after threshold: subtitle should be dropped
49 const atThreshold = 70;
63 const atThreshold = 45;
50 64 expect(atThreshold >= dropThreshold).toBe(true);
51 65
52 66 // Later: definitely should be dropped
53 const afterThreshold = 80;
67 const afterThreshold = 55;
54 68 expect(afterThreshold >= dropThreshold).toBe(true);
55 69 });
56 70 });
@@ -30,27 +30,41 @@ describe('image subtitle option parsing', () =>
30 expect(parsed.isValid).toBe(false);
30 expect(parsed.isValid).toBe(false);
31 });
31 });
32
32
33 test('prune calculates drop threshold as end plus duration plus 30 seconds', () =>
33 test('caps subtitle render canvas to 1280x720', () =>
34 {
34 {
35 // When a subtitle event is 20-30s (10s duration), it should be dropped after 70s
35 const parsed = parseRenderOptions({
36 // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 30
36 filePath: '/tmp/example.mkv',
37 streamIndex: 0,
38 timeSeconds: 5,
39 canvasWidth: 4096,
40 canvasHeight: 2160,
41 });
42
43 expect(parsed.canvasWidth).toBe(1280);
44 expect(parsed.canvasHeight).toBe(720);
45 });
46
47 test('prune calculates drop threshold as end plus duration plus 5 seconds', () =>
48 {
49 // When a subtitle event is 20-30s (10s duration), it should be dropped after 45s
50 // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 5
37 const startTime = 20;
51 const startTime = 20;
38 const endTime = 30;
52 const endTime = 30;
39 const duration = endTime - startTime;
53 const duration = endTime - startTime;
40 const dropThreshold = endTime + duration + 30;
54 const dropThreshold = endTime + duration + 5;
41
55
42 expect(dropThreshold).toBe(70);
56 expect(dropThreshold).toBe(45);
43
57
44 // Before threshold: subtitle should be kept
58 // Before threshold: subtitle should be kept
45 const beforeThreshold = 69.999;
59 const beforeThreshold = 44.999;
46 expect(beforeThreshold < dropThreshold).toBe(true);
60 expect(beforeThreshold < dropThreshold).toBe(true);
47
61
48 // At or after threshold: subtitle should be dropped
62 // At or after threshold: subtitle should be dropped
49 const atThreshold = 70;
63 const atThreshold = 45;
50 expect(atThreshold >= dropThreshold).toBe(true);
64 expect(atThreshold >= dropThreshold).toBe(true);
51
65
52 // Later: definitely should be dropped
66 // Later: definitely should be dropped
53 const afterThreshold = 80;
67 const afterThreshold = 55;
54 expect(afterThreshold >= dropThreshold).toBe(true);
68 expect(afterThreshold >= dropThreshold).toBe(true);
55 });
69 });
56});
70});