BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

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