BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

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