BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

image-subtitles.js 12.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 = 20 * 60 * 1000;
13const FRAME_CACHE_TTL_MS = 10 * 60 * 1000;
14const MAX_EVENT_CACHE_ITEMS = 6;
15const MAX_FRAME_CACHE_ITEMS = 400;
16const PGS_TIMEBASE = 90000;
17
18const eventCache = new Map();
19const frameCache = new Map();
20
21let parseDisplaySets = null;
22
23function ensureImageDataGlobal ()
24{
25 if (typeof globalThis.ImageData === 'function') return;
26
27 class ImageDataPolyfill
28 {
29 constructor (data, width, height)
30 {
31 this.data = data;
32 this.width = width;
33 this.height = height;
34 }
35 }
36
37 globalThis.ImageData = ImageDataPolyfill;
38}
39
40function loadPgsParser ()
41{
42 if (parseDisplaySets) return parseDisplaySets;
43 ensureImageDataGlobal();
44 ({ parseDisplaySets } = require('pgs-parser'));
45 return parseDisplaySets;
46}
47
48function ffmpegPath ()
49{
50 return process.env.FFMPEG_PATH || 'ffmpeg';
51}
52
53function ensureSubtitleCacheDir ()
54{
55 fs.mkdirSync(SUBTITLE_CACHE_DIR, { recursive: true });
56}
57
58function toPositiveInt (value, fallback)
59{
60 const number = Number.parseInt(String(value || ''), 10);
61 if (Number.isInteger(number) && number > 0) return number;
62 return fallback;
63}
64
65function hashSubtitleKey (filePath, streamIndex, stat)
66{
67 return crypto
68 .createHash('sha1')
69 .update(`${filePath}|${streamIndex}|${stat.size}|${Math.floor(stat.mtimeMs)}`)
70 .digest('hex');
71}
72
73function getCachedValue (cache, key)
74{
75 const now = Date.now();
76 const entry = cache.get(key);
77 if (!entry) return null;
78
79 if (entry.expiresAt <= now)
80 {
81 cache.delete(key);
82 return null;
83 }
84
85 cache.delete(key);
86 cache.set(key, entry);
87 return entry.value;
88}
89
90function setCachedValue (cache, key, value, ttlMs, maxItems)
91{
92 const now = Date.now();
93 cache.delete(key);
94 cache.set(key, {
95 value,
96 expiresAt: now + ttlMs,
97 });
98
99 while (cache.size > maxItems)
100 {
101 const oldestKey = cache.keys().next().value;
102 cache.delete(oldestKey);
103 }
104}
105
106function runCommand (command, args)
107{
108 return new Promise((resolve, reject) =>
109 {
110 const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
111 let stderr = '';
112
113 child.stderr.on('data', (chunk) =>
114 {
115 stderr += chunk.toString();
116 if (stderr.length > 16000) stderr = stderr.slice(-16000);
117 });
118
119 child.on('error', reject);
120 child.on('close', (code) =>
121 {
122 if (code === 0)
123 {
124 resolve();
125 return;
126 }
127 reject(new Error(stderr.trim() || `ffmpeg exited with code ${code}`));
128 });
129 });
130}
131
132async function extractSupFile (sourcePath, streamIndex, cacheKey)
133{
134 ensureSubtitleCacheDir();
135 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
136
137 if (fs.existsSync(supPath) && fs.statSync(supPath).size > 0)
138 {
139 return supPath;
140 }
141
142 await runCommand(ffmpegPath(), [
143 '-hide_banner',
144 '-loglevel', 'error',
145 '-nostdin',
146 '-y',
147 '-i', sourcePath,
148 '-map', `0:s:${streamIndex}`,
149 '-c:s', 'copy',
150 '-f', 'sup',
151 supPath,
152 ]);
153
154 return supPath;
155}
156
157function toSeconds (pgsTimestamp)
158{
159 const numeric = Number(pgsTimestamp);
160 if (!Number.isFinite(numeric) || numeric < 0) return 0;
161 return numeric / PGS_TIMEBASE;
162}
163
164function parseDisplaySetTimestamp (displaySet)
165{
166 if (displaySet && displaySet.presentationCompositionSegment && displaySet.presentationCompositionSegment.header)
167 {
168 return toSeconds(displaySet.presentationCompositionSegment.header.presentationTimestamp);
169 }
170 if (displaySet && displaySet.endDefinitionSegment && displaySet.endDefinitionSegment.header)
171 {
172 return toSeconds(displaySet.endDefinitionSegment.header.presentationTimestamp);
173 }
174 return 0;
175}
176
177async function parseSupEvents (supPath)
178{
179 const parserFactory = loadPgsParser();
180 if (typeof Readable.toWeb !== 'function')
181 {
182 throw new Error('Node.js stream bridge is unavailable for subtitle parsing.');
183 }
184
185 const subtitleStream = Readable.toWeb(fs.createReadStream(supPath));
186 const parserStream = subtitleStream.pipeThrough(parserFactory());
187 const reader = parserStream.getReader();
188
189 const events = [];
190 let currentEvent = null;
191 let scratchBuffer = null;
192
193 while (true)
194 {
195 const { done, value: displaySet } = await reader.read();
196 if (done) break;
197
198 const timestampSeconds = parseDisplaySetTimestamp(displaySet);
199
200 if (currentEvent && timestampSeconds > currentEvent.startTimeSeconds)
201 {
202 currentEvent.endTimeSeconds = timestampSeconds;
203 events.push(currentEvent);
204 currentEvent = null;
205 }
206
207 if (!displaySet || !Array.isArray(displaySet.objectDefinitionSegments) || displaySet.objectDefinitionSegments.length === 0)
208 {
209 continue;
210 }
211
212 const pcs = displaySet.presentationCompositionSegment;
213 if (!pcs) continue;
214
215 const neededPixels = Number(pcs.width || 0) * Number(pcs.height || 0) * 4;
216 if (!scratchBuffer || !Number.isInteger(neededPixels) || neededPixels <= 0 || scratchBuffer.length < neededPixels)
217 {
218 scratchBuffer = neededPixels > 0 ? new Uint8ClampedArray(neededPixels) : null;
219 }
220
221 let imageData;
222 try
223 {
224 imageData = displaySet.imageData(scratchBuffer || undefined);
225 } catch
226 {
227 continue;
228 }
229
230 if (!imageData || !imageData.data || !Number.isInteger(imageData.width) || !Number.isInteger(imageData.height))
231 {
232 continue;
233 }
234
235 const rawPixels = Buffer.from(imageData.data);
236 const imagePng = await sharp(rawPixels, {
237 raw: {
238 width: imageData.width,
239 height: imageData.height,
240 channels: 4,
241 },
242 }).png().toBuffer();
243
244 currentEvent = {
245 startTimeSeconds: timestampSeconds,
246 endTimeSeconds: Number.POSITIVE_INFINITY,
247 screenWidth: toPositiveInt(pcs.width, imageData.width),
248 screenHeight: toPositiveInt(pcs.height, imageData.height),
249 left: toPositiveInt(pcs.objectHorizontalPosition, 0),
250 top: toPositiveInt(pcs.objectVerticalPosition, 0),
251 imageWidth: imageData.width,
252 imageHeight: imageData.height,
253 imagePng,
254 };
255 }
256
257 if (currentEvent)
258 {
259 events.push(currentEvent);
260 }
261
262 return events;
263}
264
265async function buildEventCacheEntry (filePath, streamIndex)
266{
267 const stat = await fs.promises.stat(filePath);
268 const cacheKey = hashSubtitleKey(filePath, streamIndex, stat);
269 const supPath = await extractSupFile(filePath, streamIndex, cacheKey);
270 const events = await parseSupEvents(supPath);
271 return {
272 cacheKey,
273 events,
274 };
275}
276
277async function getEventCacheEntry (filePath, streamIndex)
278{
279 const stat = await fs.promises.stat(filePath);
280 const identityKey = hashSubtitleKey(filePath, streamIndex, stat);
281
282 const cached = getCachedValue(eventCache, identityKey);
283 if (cached)
284 {
285 return cached;
286 }
287
288 const pending = buildEventCacheEntry(filePath, streamIndex);
289 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
290
291 try
292 {
293 const resolved = await pending;
294 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, MAX_EVENT_CACHE_ITEMS);
295 return resolved;
296 } catch (error)
297 {
298 eventCache.delete(identityKey);
299 throw error;
300 }
301}
302
303function findActiveEventIndex (events, timeSeconds)
304{
305 let left = 0;
306 let right = events.length - 1;
307 let candidate = -1;
308
309 while (left <= right)
310 {
311 const middle = Math.floor((left + right) / 2);
312 const event = events[middle];
313
314 if (event.startTimeSeconds <= timeSeconds)
315 {
316 candidate = middle;
317 left = middle + 1;
318 } else
319 {
320 right = middle - 1;
321 }
322 }
323
324 if (candidate < 0) return -1;
325
326 const active = events[candidate];
327 if (timeSeconds >= active.endTimeSeconds) return -1;
328 return candidate;
329}
330
331async function renderEventFrame (event, canvasWidth, canvasHeight)
332{
333 const sourceWidth = Math.max(1, toPositiveInt(event.screenWidth, canvasWidth));
334 const sourceHeight = Math.max(1, toPositiveInt(event.screenHeight, canvasHeight));
335
336 const scaleX = canvasWidth / sourceWidth;
337 const scaleY = canvasHeight / sourceHeight;
338
339 const targetWidth = Math.max(1, Math.round(event.imageWidth * scaleX));
340 const targetHeight = Math.max(1, Math.round(event.imageHeight * scaleY));
341 const left = Math.max(0, Math.round(event.left * scaleX));
342 const top = Math.max(0, Math.round(event.top * scaleY));
343
344 if (left >= canvasWidth || top >= canvasHeight)
345 {
346 return sharp({
347 create: {
348 width: canvasWidth,
349 height: canvasHeight,
350 channels: 4,
351 background: { r: 0, g: 0, b: 0, alpha: 0 },
352 },
353 }).png().toBuffer();
354 }
355
356 const imageBuffer = (targetWidth === event.imageWidth && targetHeight === event.imageHeight)
357 ? event.imagePng
358 : await sharp(event.imagePng).resize({ width: targetWidth, height: targetHeight, fit: 'fill' }).png().toBuffer();
359
360 return sharp({
361 create: {
362 width: canvasWidth,
363 height: canvasHeight,
364 channels: 4,
365 background: { r: 0, g: 0, b: 0, alpha: 0 },
366 },
367 }).composite([
368 {
369 input: imageBuffer,
370 left,
371 top,
372 },
373 ]).png().toBuffer();
374}
375
376function parseRenderOptions (options)
377{
378 const input = options || {};
379 const filePath = String(input.filePath ?? '').trim();
380 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
381 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
382 const canvasWidth = Math.max(1, Math.min(8192, toPositiveInt(input.canvasWidth ?? '', 1280)));
383 const canvasHeight = Math.max(1, Math.min(8192, toPositiveInt(input.canvasHeight ?? '', 720)));
384
385 return {
386 filePath,
387 streamIndex,
388 timeSeconds,
389 canvasWidth,
390 canvasHeight,
391 isValid: !!(filePath && Number.isInteger(streamIndex) && streamIndex >= 0 && Number.isFinite(timeSeconds) && timeSeconds >= 0),
392 };
393}
394
395async function renderImageSubtitleFrame (options)
396{
397 const parsed = parseRenderOptions(options);
398 const { filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight } = parsed;
399
400 if (!parsed.isValid)
401 {
402 return null;
403 }
404
405 const entry = await getEventCacheEntry(filePath, streamIndex);
406 if (!entry || !Array.isArray(entry.events) || entry.events.length === 0) return null;
407
408 const eventIndex = findActiveEventIndex(entry.events, timeSeconds);
409 if (eventIndex < 0) return null;
410
411 const frameKey = `${entry.cacheKey}|${eventIndex}|${canvasWidth}x${canvasHeight}`;
412 const cachedFrame = getCachedValue(frameCache, frameKey);
413 if (cachedFrame)
414 {
415 return cachedFrame;
416 }
417
418 const frame = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
419 const event = entry.events[eventIndex];
420 setCachedValue(frameCache, frameKey, { frame, startTimeSeconds: event.startTimeSeconds, endTimeSeconds: event.endTimeSeconds }, FRAME_CACHE_TTL_MS, MAX_FRAME_CACHE_ITEMS);
421 return frame;
422}
423
424function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
425{
426 const normalised = String(filePath || '').trim();
427 if (!normalised || !Number.isFinite(slowestViewerTimeSeconds)) return;
428
429 const keysToDelete = [];
430 for (const key of frameCache.keys())
431 {
432 if (!key.startsWith(normalised)) continue;
433
434 const entry = frameCache.get(key);
435 if (!entry) continue;
436
437 const eventEntry = entry.value || {};
438 const { startTimeSeconds, endTimeSeconds } = eventEntry;
439
440 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
441
442 const duration = endTimeSeconds - startTimeSeconds;
443 const dropThreshold = endTimeSeconds + duration;
444
445 if (slowestViewerTimeSeconds >= dropThreshold)
446 {
447 keysToDelete.push(key);
448 }
449 }
450
451 for (const key of keysToDelete)
452 {
453 frameCache.delete(key);
454 }
455}
456
457function clearCachesForFile (filePath)
458{
459 const normalised = String(filePath || '').trim();
460 if (!normalised) return;
461
462 const keysToDelete = [];
463
464 for (const key of eventCache.keys())
465 {
466 if (key.startsWith(normalised))
467 {
468 keysToDelete.push(key);
469 }
470 }
471
472 for (const key of frameCache.keys())
473 {
474 if (key.startsWith(normalised))
475 {
476 keysToDelete.push(key);
477 }
478 }
479
480 for (const key of keysToDelete)
481 {
482 eventCache.delete(key);
483 frameCache.delete(key);
484 }
485}
486
487module.exports = {
488 parseRenderOptions,
489 renderImageSubtitleFrame,
490 clearCachesForFile,
491 pruneOldImageSubtitleCaches,
492};