BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

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