BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

image-subtitles.js 15.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 = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, 3);
15const MAX_FRAME_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, 96);
16const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 96 * 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.5, 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 getCachedValue (cache, key)
161{
162 const now = Date.now();
163 const entry = cache.map.get(key);
164 if (!entry) return null;
165
166 if (entry.expiresAt <= now)
167 {
168 deleteCachedValue(cache, key);
169 return null;
170 }
171
172 cache.map.delete(key);
173 cache.map.set(key, entry);
174 return entry.value;
175}
176
177function setCachedValue (cache, key, value, ttlMs, estimateBytes)
178{
179 const now = Date.now();
180 deleteCachedValue(cache, key);
181
182 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
183
184 cache.map.set(key, {
185 value,
186 expiresAt: now + ttlMs,
187 sizeBytes,
188 });
189 cache.totalBytes += sizeBytes;
190
191 while (cache.map.size > cache.maxItems || cache.totalBytes > cache.maxBytes)
192 {
193 const oldestKey = cache.map.keys().next().value;
194 deleteCachedValue(cache, oldestKey);
195 }
196}
197
198function runCommand (command, args)
199{
200 return new Promise((resolve, reject) =>
201 {
202 const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
203 let stderr = '';
204
205 child.stderr.on('data', (chunk) =>
206 {
207 stderr += chunk.toString();
208 if (stderr.length > 16000) stderr = stderr.slice(-16000);
209 });
210
211 child.on('error', reject);
212 child.on('close', (code) =>
213 {
214 if (code === 0)
215 {
216 resolve();
217 return;
218 }
219 reject(new Error(stderr.trim() || `ffmpeg exited with code ${code}`));
220 });
221 });
222}
223
224async function extractSupFile (sourcePath, streamIndex, cacheKey)
225{
226 ensureSubtitleCacheDir();
227 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
228
229 if (fs.existsSync(supPath) && fs.statSync(supPath).size > 0)
230 {
231 return supPath;
232 }
233
234 await runCommand(ffmpegPath(), [
235 '-hide_banner',
236 '-loglevel', 'error',
237 '-nostdin',
238 '-y',
239 '-i', sourcePath,
240 '-map', `0:s:${streamIndex}`,
241 '-c:s', 'copy',
242 '-f', 'sup',
243 supPath,
244 ]);
245
246 return supPath;
247}
248
249function toSeconds (pgsTimestamp)
250{
251 const numeric = Number(pgsTimestamp);
252 if (!Number.isFinite(numeric) || numeric < 0) return 0;
253 return numeric / PGS_TIMEBASE;
254}
255
256function parseDisplaySetTimestamp (displaySet)
257{
258 if (displaySet && displaySet.presentationCompositionSegment && displaySet.presentationCompositionSegment.header)
259 {
260 return toSeconds(displaySet.presentationCompositionSegment.header.presentationTimestamp);
261 }
262 if (displaySet && displaySet.endDefinitionSegment && displaySet.endDefinitionSegment.header)
263 {
264 return toSeconds(displaySet.endDefinitionSegment.header.presentationTimestamp);
265 }
266 return 0;
267}
268
269async function parseSupEvents (supPath)
270{
271 const parserFactory = loadPgsParser();
272 if (typeof Readable.toWeb !== 'function')
273 {
274 throw new Error('Node.js stream bridge is unavailable for subtitle parsing.');
275 }
276
277 const subtitleStream = Readable.toWeb(fs.createReadStream(supPath));
278 const parserStream = subtitleStream.pipeThrough(parserFactory());
279 const reader = parserStream.getReader();
280
281 const events = [];
282 let currentEvent = null;
283 let scratchBuffer = null;
284
285 while (true)
286 {
287 const { done, value: displaySet } = await reader.read();
288 if (done) break;
289
290 const timestampSeconds = parseDisplaySetTimestamp(displaySet);
291
292 if (currentEvent && timestampSeconds > currentEvent.startTimeSeconds)
293 {
294 currentEvent.endTimeSeconds = timestampSeconds;
295 events.push(currentEvent);
296 currentEvent = null;
297 }
298
299 if (!displaySet || !Array.isArray(displaySet.objectDefinitionSegments) || displaySet.objectDefinitionSegments.length === 0)
300 {
301 continue;
302 }
303
304 const pcs = displaySet.presentationCompositionSegment;
305 if (!pcs) continue;
306
307 const neededPixels = Number(pcs.width || 0) * Number(pcs.height || 0) * 4;
308 if (!scratchBuffer || !Number.isInteger(neededPixels) || neededPixels <= 0 || scratchBuffer.length < neededPixels)
309 {
310 scratchBuffer = neededPixels > 0 ? new Uint8ClampedArray(neededPixels) : null;
311 }
312
313 let imageData;
314 try
315 {
316 imageData = displaySet.imageData(scratchBuffer || undefined);
317 } catch
318 {
319 continue;
320 }
321
322 if (!imageData || !imageData.data || !Number.isInteger(imageData.width) || !Number.isInteger(imageData.height))
323 {
324 continue;
325 }
326
327 const rawPixels = Buffer.from(imageData.data);
328 const imagePng = await sharp(rawPixels, {
329 raw: {
330 width: imageData.width,
331 height: imageData.height,
332 channels: 4,
333 },
334 }).png().toBuffer();
335
336 currentEvent = {
337 startTimeSeconds: timestampSeconds,
338 endTimeSeconds: Number.POSITIVE_INFINITY,
339 screenWidth: toPositiveInt(pcs.width, imageData.width),
340 screenHeight: toPositiveInt(pcs.height, imageData.height),
341 left: toPositiveInt(pcs.objectHorizontalPosition, 0),
342 top: toPositiveInt(pcs.objectVerticalPosition, 0),
343 imageWidth: imageData.width,
344 imageHeight: imageData.height,
345 imagePng,
346 };
347 }
348
349 if (currentEvent)
350 {
351 events.push(currentEvent);
352 }
353
354 return events;
355}
356
357async function buildEventCacheEntry (filePath, streamIndex, identityKey)
358{
359 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
360 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
361 const events = await parseSupEvents(supPath);
362 return {
363 cacheKey: identityKey,
364 events,
365 };
366}
367
368async function getEventCacheEntry (filePath, streamIndex)
369{
370 const normalisedPath = normaliseFilePath(filePath);
371 const stat = await fs.promises.stat(normalisedPath);
372 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
373
374 const cached = getCachedValue(eventCache, identityKey);
375 if (cached)
376 {
377 return cached;
378 }
379
380 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
381 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
382
383 try
384 {
385 const resolved = await pending;
386 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
387 return resolved;
388 } catch (error)
389 {
390 deleteCachedValue(eventCache, identityKey);
391 throw error;
392 }
393}
394
395function findActiveEventIndex (events, timeSeconds)
396{
397 let left = 0;
398 let right = events.length - 1;
399 let candidate = -1;
400
401 while (left <= right)
402 {
403 const middle = Math.floor((left + right) / 2);
404 const event = events[middle];
405
406 if (event.startTimeSeconds <= timeSeconds)
407 {
408 candidate = middle;
409 left = middle + 1;
410 } else
411 {
412 right = middle - 1;
413 }
414 }
415
416 if (candidate < 0) return -1;
417
418 const active = events[candidate];
419 if (timeSeconds >= active.endTimeSeconds) return -1;
420 return candidate;
421}
422
423async function renderEventFrame (event, canvasWidth, canvasHeight)
424{
425 const sourceWidth = Math.max(1, toPositiveInt(event.screenWidth, canvasWidth));
426 const sourceHeight = Math.max(1, toPositiveInt(event.screenHeight, canvasHeight));
427
428 const scaleX = canvasWidth / sourceWidth;
429 const scaleY = canvasHeight / sourceHeight;
430
431 const targetWidth = Math.max(1, Math.round(event.imageWidth * scaleX));
432 const targetHeight = Math.max(1, Math.round(event.imageHeight * scaleY));
433 const left = Math.round(event.left * scaleX);
434 const top = Math.round(event.top * scaleY);
435 const right = left + targetWidth;
436 const bottom = top + targetHeight;
437
438 const visibleLeft = Math.max(0, left);
439 const visibleTop = Math.max(0, top);
440 const visibleRight = Math.min(canvasWidth, right);
441 const visibleBottom = Math.min(canvasHeight, bottom);
442 const visibleWidth = Math.max(0, visibleRight - visibleLeft);
443 const visibleHeight = Math.max(0, visibleBottom - visibleTop);
444
445 if (visibleWidth <= 0 || visibleHeight <= 0)
446 {
447 return null;
448 }
449
450 let imageBuffer = (targetWidth === event.imageWidth && targetHeight === event.imageHeight)
451 ? event.imagePng
452 : await sharp(event.imagePng).resize({ width: targetWidth, height: targetHeight, fit: 'fill' }).png().toBuffer();
453
454 if (visibleWidth < targetWidth || visibleHeight < targetHeight)
455 {
456 imageBuffer = await sharp(imageBuffer)
457 .extract({
458 left: Math.max(0, visibleLeft - left),
459 top: Math.max(0, visibleTop - top),
460 width: visibleWidth,
461 height: visibleHeight,
462 })
463 .png()
464 .toBuffer();
465 }
466
467 return imageBuffer;
468}
469
470function parseRenderOptions (options)
471{
472 const input = options || {};
473 const filePath = String(input.filePath ?? '').trim();
474 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
475 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
476 const requestedWidth = Math.max(1, toPositiveInt(input.canvasWidth ?? '', 1280));
477 const requestedHeight = Math.max(1, toPositiveInt(input.canvasHeight ?? '', 720));
478 const scale = Math.min(1, 1280 / requestedWidth, 720 / requestedHeight) * IMAGE_SUBTITLE_RENDER_SCALE;
479 const canvasWidth = Math.max(1, Math.round(requestedWidth * scale));
480 const canvasHeight = Math.max(1, Math.round(requestedHeight * scale));
481
482 return {
483 filePath,
484 streamIndex,
485 timeSeconds,
486 canvasWidth,
487 canvasHeight,
488 isValid: !!(filePath && Number.isInteger(streamIndex) && streamIndex >= 0 && Number.isFinite(timeSeconds) && timeSeconds >= 0),
489 };
490}
491
492async function renderImageSubtitleFrame (options)
493{
494 const parsed = parseRenderOptions(options);
495 const { filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight } = parsed;
496
497 if (!parsed.isValid)
498 {
499 return null;
500 }
501
502 const entry = await getEventCacheEntry(filePath, streamIndex);
503 if (!entry || !Array.isArray(entry.events) || entry.events.length === 0) return null;
504
505 const eventIndex = findActiveEventIndex(entry.events, timeSeconds);
506 if (eventIndex < 0) return null;
507
508 const frameKey = `${entry.cacheKey}|${eventIndex}|${canvasWidth}x${canvasHeight}`;
509 const cachedFrame = getCachedValue(frameCache, frameKey);
510 if (cachedFrame)
511 {
512 return cachedFrame.frame || cachedFrame;
513 }
514
515 const frame = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
516 const event = entry.events[eventIndex];
517 setCachedValue(
518 frameCache,
519 frameKey,
520 { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds },
521 FRAME_CACHE_TTL_MS,
522 estimateFrameCacheBytes,
523 );
524 return frame;
525}
526
527function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
528{
529 const prefix = cachePrefixForFile(filePath);
530 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
531
532 const keysToDelete = [];
533 for (const key of frameCache.map.keys())
534 {
535 if (!key.startsWith(prefix)) continue;
536
537 const entry = frameCache.map.get(key);
538 if (!entry) continue;
539
540 const eventEntry = entry.value || {};
541 const { startTimeSeconds, endTimeSeconds } = eventEntry;
542
543 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
544
545 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
546 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
547
548 if (slowestViewerTimeSeconds >= dropThreshold)
549 {
550 keysToDelete.push(key);
551 }
552 }
553
554 for (const key of keysToDelete)
555 {
556 deleteCachedValue(frameCache, key);
557 }
558}
559
560function clearCachesForFile (filePath)
561{
562 const prefix = cachePrefixForFile(filePath);
563 if (!prefix) return;
564
565 const keysToDelete = [];
566
567 for (const key of eventCache.map.keys())
568 {
569 if (key.startsWith(prefix))
570 {
571 keysToDelete.push(key);
572 }
573 }
574
575 for (const key of frameCache.map.keys())
576 {
577 if (key.startsWith(prefix))
578 {
579 keysToDelete.push(key);
580 }
581 }
582
583 for (const key of keysToDelete)
584 {
585 deleteCachedValue(eventCache, key);
586 deleteCachedValue(frameCache, key);
587 }
588}
589
590module.exports = {
591 parseRenderOptions,
592 renderImageSubtitleFrame,
593 clearCachesForFile,
594 pruneOldImageSubtitleCaches,
595};