BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

sanitize {notes} in VTT
timfa Tim Falken · pushed by timfa · 5 months ago
e3629e40 History
video-stream.js 35.5 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 { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');
9
10const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
12const STREAM_READY_TIMEOUT_MS = 20 * 1000;
13const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
14const STREAM_EXPIRY_SWEEP_MS = parseSweepInterval(process.env.VIDEO_STREAM_EXPIRY_SWEEP_MS, 60 * 1000);
15const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;
16const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;
17const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);
18const MAX_STREAM_SEGMENT_RETENTION_COUNT = 360;
19const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);
20const IMAGE_BASED_SUBTITLE_CODECS = new Set([
21 'hdmv_pgs_subtitle',
22 'pgssub',
23 'dvd_subtitle',
24 'dvdsub',
25 'dvb_subtitle',
26 'xsub',
27]);
28const sessions = new Map();
29const leases = new Map();
30const reusableSessionIdBySource = new Map();
31
32// Remove any .sup subtitle files left over from a previous server run.
33purgeSubtitleCacheDir();
34
35function purgeStreamRootDir ()
36{
37 try
38 {
39 fs.rmSync(STREAM_ROOT_DIR, { recursive: true, force: true });
40 } catch
41 {
42 // Best-effort cleanup.
43 }
44}
45
46function cleanupIdleStreamArtifacts ()
47{
48 if (sessions.size > 0) return;
49 if (leases.size > 0) return;
50 purgeStreamRootDir();
51}
52
53// Remove stale stream output folders left by previous runs.
54purgeStreamRootDir();
55
56// Periodically sweep stream artifacts when no streams are active.
57const streamRootCleanupTimer = setInterval(cleanupIdleStreamArtifacts, STREAM_IDLE_STOP_MS);
58if (streamRootCleanupTimer && typeof streamRootCleanupTimer.unref === 'function')
59{
60 streamRootCleanupTimer.unref();
61}
62
63// Ensure expired leases/sessions are reclaimed even without further stream requests.
64const streamExpirySweepTimer = setInterval(pruneExpiredSessions, STREAM_EXPIRY_SWEEP_MS);
65if (streamExpirySweepTimer && typeof streamExpirySweepTimer.unref === 'function')
66{
67 streamExpirySweepTimer.unref();
68}
69
70function parseSweepInterval (value, fallbackMs)
71{
72 const parsed = Number.parseInt(String(value || ''), 10);
73 if (Number.isInteger(parsed) && parsed >= 1000)
74 {
75 return clampNumber(parsed, 1000, 10 * 60 * 1000);
76 }
77 return fallbackMs;
78}
79
80function parseSegmentRetentionCount (value)
81{
82 const parsed = Number.parseInt(String(value || ''), 10);
83 if (Number.isInteger(parsed) && parsed >= 30)
84 {
85 return clampNumber(parsed, 30, MAX_STREAM_SEGMENT_RETENTION_COUNT);
86 }
87 return defaultSegmentRetentionCount();
88}
89
90function parsePruneDeleteBatchSize (value)
91{
92 const parsed = Number.parseInt(String(value || ''), 10);
93 if (Number.isInteger(parsed) && parsed >= 1)
94 {
95 return clampNumber(parsed, 1, 128);
96 }
97 return 16;
98}
99
100function readRaspberryPiModel ()
101{
102 try
103 {
104 return fs.readFileSync('/proc/device-tree/model', 'utf8').replace(/\u0000/g, '').trim();
105 } catch
106 {
107 return '';
108 }
109}
110
111function clampNumber (value, min, max)
112{
113 return Math.min(max, Math.max(min, value));
114}
115
116function currentCpuPressureFactor (cpuCount)
117{
118 if (typeof os.loadavg !== 'function') return 1;
119 if (!Number.isFinite(cpuCount) || cpuCount <= 0) return 1;
120
121 const [load1] = os.loadavg();
122 if (!Number.isFinite(load1) || load1 < 0) return 1;
123
124 const saturation = load1 / cpuCount;
125 if (saturation >= 1.5) return 0.72;
126 if (saturation >= 1.2) return 0.8;
127 if (saturation >= 1.0) return 0.88;
128 if (saturation >= 0.8) return 0.94;
129 return 1;
130}
131
132function defaultSegmentRetentionCount ()
133{
134 const totalMemBytes = Number(os.totalmem()) || 0;
135 const freeMemBytes = Number(os.freemem()) || 0;
136 const cpuCount = Math.max(1, ((os.cpus() || []).length || 1));
137 const arch = String(os.arch() || '').toLowerCase();
138 const piModel = readRaspberryPiModel().toLowerCase();
139 const isPi5 = piModel.includes('raspberry pi 5');
140 const isArm = arch === 'arm' || arch === 'arm64' || arch.startsWith('armv');
141 const cpuPressure = currentCpuPressureFactor(cpuCount);
142
143 const freeMemRatio = totalMemBytes > 0 ? (freeMemBytes / totalMemBytes) : 1;
144 const memoryPressure = freeMemRatio < 0.12 ? 0.72 : freeMemRatio < 0.2 ? 0.84 : freeMemRatio < 0.3 ? 0.92 : 1;
145
146 if (isPi5)
147 {
148 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.03);
149 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
150 const byCpu = 120 + ((cpuCount - 2) * 20);
151 const combined = Math.min(byMemory, byCpu);
152 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 100, 220);
153 }
154
155 if (isArm)
156 {
157 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.025);
158 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
159 const byCpu = 90 + ((cpuCount - 2) * 14);
160 const combined = Math.min(byMemory, byCpu);
161 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 80, 180);
162 }
163
164 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.05);
165 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
166 const byCpu = 140 + ((cpuCount - 4) * 16);
167 const combined = Math.min(byMemory, byCpu);
168 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 120, 320);
169}
170
171function hasInitialHlsAssets (outputDir)
172{
173 try
174 {
175 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
176 return entries.some((entry) =>
177 {
178 if (!entry.isFile()) return false;
179 if (entry.name === 'master.m3u8') return true;
180 if (/^stream_.+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
181 if (/^stream_.+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
182 return false;
183 });
184 } catch
185 {
186 return false;
187 }
188}
189
190function makeHttpError (status, message)
191{
192 const err = new Error(message);
193 err.status = status;
194 return err;
195}
196
197function ensureStreamRootDir ()
198{
199 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
200}
201
202function ffmpegPath ()
203{
204 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
205}
206
207function ffprobePath ()
208{
209 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
210}
211
212function pruneExpiredSessions ()
213{
214 const now = Date.now();
215
216 for (const [leaseId, lease] of leases)
217 {
218 if (lease.expiresAt > now) continue;
219 releaseVideoStreamSession(leaseId);
220 }
221
222 for (const [sessionId, session] of sessions)
223 {
224 if (session.expiresAt > now) continue;
225 destroySession(sessionId);
226 }
227}
228
229function refreshSession (session)
230{
231 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
232}
233
234function refreshLease (lease)
235{
236 lease.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
237}
238
239function destroySession (sessionId)
240{
241 const session = sessions.get(sessionId);
242 if (!session) return false;
243
244 sessions.delete(sessionId);
245
246 if (reusableSessionIdBySource.get(session.sourcePath) === session.id)
247 {
248 reusableSessionIdBySource.delete(session.sourcePath);
249 }
250
251 if (session.leaseIds)
252 {
253 for (const leaseId of session.leaseIds)
254 {
255 leases.delete(leaseId);
256 }
257 session.leaseIds.clear();
258 }
259
260 if (session.idleTimer)
261 {
262 clearTimeout(session.idleTimer);
263 session.idleTimer = null;
264 }
265
266 try
267 {
268 if (session.process) session.process.kill('SIGTERM');
269 } catch
270 {
271 // Ignore process cleanup failures.
272 }
273
274 fs.rmSync(session.outputDir, { recursive: true, force: true });
275
276 // Only clear file-wide subtitle caches when no other active stream session
277 // for the same source file remains.
278 if (!hasActiveSessionForSourcePath(session.sourcePath))
279 {
280 clearCachesForFile(session.sourcePath);
281 }
282
283 return true;
284}
285
286function pruneImageSubtitleCachesForSession (session)
287{
288 if (!session || !session.sourcePath) return;
289
290 const slowestViewerTimeSeconds = slowestViewerTimeSecondsForSourcePath(session.sourcePath);
291 if (!Number.isFinite(slowestViewerTimeSeconds)) return;
292 pruneOldImageSubtitleCaches(session.sourcePath, slowestViewerTimeSeconds);
293}
294
295function hasActiveSessionForSourcePath (sourcePath)
296{
297 const wantedPath = String(sourcePath || '');
298 if (!wantedPath) return false;
299
300 for (const session of sessions.values())
301 {
302 if (session && session.sourcePath === wantedPath) return true;
303 }
304
305 return false;
306}
307
308function slowestViewerTimeSecondsForSourcePath (sourcePath)
309{
310 const wantedPath = String(sourcePath || '');
311 if (!wantedPath) return null;
312
313 let slowestIndex = null;
314 let hasActiveViewers = false;
315
316 for (const session of sessions.values())
317 {
318 if (!session || session.sourcePath !== wantedPath) continue;
319 if (!session.leaseIds || session.leaseIds.size === 0) continue;
320
321 hasActiveViewers = true;
322 const sessionSlowestIndex = slowestLeaseSegmentIndex(session);
323 if (!Number.isInteger(sessionSlowestIndex)) return null;
324 if (slowestIndex === null || sessionSlowestIndex < slowestIndex)
325 {
326 slowestIndex = sessionSlowestIndex;
327 }
328 }
329
330 if (!hasActiveViewers || slowestIndex === null) return null;
331 return slowestIndex * ESTIMATED_SEGMENT_DURATION_SECONDS;
332}
333
334function getSessionForLease (leaseId)
335{
336 const lease = leases.get(leaseId);
337 if (!lease) return null;
338
339 const session = sessions.get(lease.sessionId);
340 if (!session)
341 {
342 leases.delete(leaseId);
343 return null;
344 }
345
346 return { lease, session };
347}
348
349function createLeaseForSession (session)
350{
351 const leaseId = crypto.randomBytes(18).toString('hex');
352 const lease = {
353 id: leaseId,
354 sessionId: session.id,
355 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
356 lastActivityAt: Date.now(),
357 lastSegmentIndex: null,
358 };
359 leases.set(leaseId, lease);
360 session.leaseIds.add(leaseId);
361 return lease;
362}
363
364function touchSessionActivity (session, lease)
365{
366 refreshSession(session);
367 session.lastActivityAt = Date.now();
368
369 if (lease)
370 {
371 refreshLease(lease);
372 lease.lastActivityAt = Date.now();
373 }
374}
375
376function clearIdleStopTimer (session)
377{
378 if (!session.idleTimer) return;
379 clearTimeout(session.idleTimer);
380 session.idleTimer = null;
381}
382
383function scheduleIdleStop (session)
384{
385 clearIdleStopTimer(session);
386 if (session.activeRequests > 0 || (session.leaseIds && session.leaseIds.size > 0)) return;
387
388 session.idleTimer = setTimeout(() =>
389 {
390 const activeSession = sessions.get(session.id);
391 if (!activeSession) return;
392 if (activeSession.activeRequests > 0 || (activeSession.leaseIds && activeSession.leaseIds.size > 0)) return;
393 if (Date.now() - activeSession.lastActivityAt < STREAM_IDLE_STOP_MS)
394 {
395 scheduleIdleStop(activeSession);
396 return;
397 }
398 destroySession(activeSession.id);
399 }, STREAM_IDLE_STOP_MS);
400}
401
402function startAssetRequest (session, lease)
403{
404 session.activeRequests += 1;
405 touchSessionActivity(session, lease);
406 clearIdleStopTimer(session);
407}
408
409function finishAssetRequest (session, lease)
410{
411 if (session.activeRequests > 0) session.activeRequests -= 1;
412
413 const activeSession = sessions.get(session.id);
414 if (!activeSession) return;
415
416 touchSessionActivity(activeSession, lease);
417 if (activeSession.activeRequests === 0 && activeSession.leaseIds.size === 0)
418 {
419 destroySession(activeSession.id);
420 }
421}
422
423function parseSegmentIndexFromAssetPath (assetPath)
424{
425 const baseName = path.basename(String(assetPath || ''));
426 const match = baseName.match(/^stream_.+_segment_(\d+)\.ts$/i);
427 if (!match) return null;
428
429 const segmentIndex = Number.parseInt(match[1], 10);
430 return Number.isInteger(segmentIndex) ? segmentIndex : null;
431}
432
433function runSegmentPrune (session)
434{
435 if (session.segmentPruneRunning) return;
436 if (session.segmentPrunePendingCutoff <= session.segmentPrunedUntil) return;
437
438 const cutoff = session.segmentPrunePendingCutoff;
439 const pruneStartedAt = Date.now();
440 session.segmentPruneRunning = true;
441
442 fs.promises.readdir(session.outputDir)
443 .then((entries) =>
444 {
445 const removals = [];
446 for (const entryName of entries)
447 {
448 const match = String(entryName).match(/^stream_.+_segment_(\d+)\.ts$/i);
449 if (!match) continue;
450 const segmentIndex = Number.parseInt(match[1], 10);
451 if (!Number.isInteger(segmentIndex)) continue;
452 if (segmentIndex > cutoff) continue;
453
454 removals.push(path.join(session.outputDir, entryName));
455 }
456 return deleteFilesInBatches(removals, STREAM_PRUNE_DELETE_BATCH_SIZE);
457 })
458 .then((deletedCount) =>
459 {
460 const completedAt = Date.now();
461 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
462 session.lastSegmentPrune = {
463 cutoff,
464 deletedSegments: deletedCount,
465 durationMs: Math.max(0, completedAt - pruneStartedAt),
466 completedAt,
467 };
468 pruneImageSubtitleCachesForSession(session);
469 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
470 {
471 reusableSessionIdBySource.delete(session.sourcePath);
472 }
473 })
474 .catch(() =>
475 {
476 // Best effort cleanup; keep streaming even if pruning fails.
477 })
478 .finally(() =>
479 {
480 session.segmentPruneRunning = false;
481 if (session.segmentPrunePendingCutoff > session.segmentPrunedUntil)
482 {
483 runSegmentPrune(session);
484 }
485 });
486}
487
488async function deleteFilesInBatches (pathsToDelete, batchSize)
489{
490 const paths = Array.isArray(pathsToDelete) ? pathsToDelete : [];
491 if (!paths.length) return 0;
492
493 let deletedCount = 0;
494 for (let index = 0; index < paths.length; index += batchSize)
495 {
496 const batch = paths.slice(index, index + batchSize);
497 await Promise.all(batch.map((targetPath) =>
498 fs.promises.unlink(targetPath)
499 .then(() =>
500 {
501 deletedCount += 1;
502 })
503 .catch(() =>
504 {
505 // Ignore races while ffmpeg rotates temp files and request handlers read segments.
506 })
507 ));
508 }
509
510 return deletedCount;
511}
512
513function slowestLeaseSegmentIndex (session)
514{
515 if (!session.leaseIds.size) return null;
516
517 let minIndex = null;
518 for (const leaseId of session.leaseIds)
519 {
520 const lease = leases.get(leaseId);
521 if (!lease) continue;
522 if (!Number.isInteger(lease.lastSegmentIndex)) return null;
523 if (minIndex === null || lease.lastSegmentIndex < minIndex) minIndex = lease.lastSegmentIndex;
524 }
525
526 return minIndex;
527}
528
529function scheduleSegmentPrune (session, lease, segmentIndex)
530{
531 if (!Number.isInteger(segmentIndex) || segmentIndex < 0) return;
532
533 lease.lastSegmentIndex = segmentIndex;
534 const slowestIndex = slowestLeaseSegmentIndex(session);
535 if (!Number.isInteger(slowestIndex)) return;
536
537 const pruneCutoff = slowestIndex - STREAM_SEGMENT_RETENTION_COUNT;
538 if (pruneCutoff <= session.segmentPrunedUntil) return;
539
540 session.segmentPrunePendingCutoff = Math.max(session.segmentPrunePendingCutoff, pruneCutoff);
541 runSegmentPrune(session);
542}
543
544function runCommand (command, args)
545{
546 return new Promise((resolve, reject) =>
547 {
548 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
549 let stdout = '';
550 let stderr = '';
551
552 child.stdout.on('data', (chunk) =>
553 {
554 stdout += chunk.toString();
555 });
556
557 child.stderr.on('data', (chunk) =>
558 {
559 stderr += chunk.toString();
560 });
561
562 child.on('error', (err) =>
563 {
564 if (err && err.code === 'ENOENT')
565 {
566 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
567 return;
568 }
569 reject(err);
570 });
571
572 child.on('close', (code) =>
573 {
574 if (code === 0)
575 {
576 resolve({ stdout, stderr });
577 return;
578 }
579 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
580 });
581 });
582}
583
584async function probeInput (sourcePath)
585{
586 const { stdout } = await runCommand(ffprobePath(), [
587 '-v', 'error',
588 '-print_format', 'json',
589 '-show_streams',
590 '-show_format',
591 sourcePath,
592 ]);
593
594 let payload;
595 try
596 {
597 payload = JSON.parse(stdout || '{}');
598 } catch
599 {
600 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
601 }
602
603 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
604 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
605 {
606 const codecType = String(stream.codec_type || '').toLowerCase();
607 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
608 ? typedIndexes[codecType]++
609 : null;
610 return {
611 ...stream,
612 codecType,
613 relativeIndex,
614 tags: stream.tags || {},
615 disposition: stream.disposition || {},
616 };
617 }) : [];
618
619 return {
620 raw: payload,
621 durationSeconds: inputDurationSeconds(payload, streams),
622 video: streams.filter((stream) => stream.codecType === 'video'),
623 audio: streams.filter((stream) => stream.codecType === 'audio'),
624 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
625 };
626}
627
628function parsePositiveNumber (value)
629{
630 const number = Number(value);
631 return Number.isFinite(number) && number > 0 ? number : null;
632}
633
634function parseDurationTagSeconds (value)
635{
636 const raw = String(value || '').trim();
637 if (!raw) return null;
638
639 const numeric = parsePositiveNumber(raw);
640 if (numeric) return numeric;
641
642 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
643 if (!match) return null;
644
645 const hours = Number(match[1]);
646 const minutes = Number(match[2]);
647 const seconds = Number(match[3]);
648 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
649 return hours * 3600 + minutes * 60 + seconds;
650}
651
652function inputDurationSeconds (payload, streams)
653{
654 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
655 if (formatDuration) return formatDuration;
656
657 for (const stream of streams)
658 {
659 const duration = parsePositiveNumber(stream.duration);
660 if (duration) return duration;
661 }
662
663 const formatTags = payload && payload.format && payload.format.tags;
664 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
665 if (formatTagDuration) return formatTagDuration;
666
667 for (const stream of streams)
668 {
669 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
670 if (tagDuration) return tagDuration;
671 }
672
673 return null;
674}
675
676function sanitiseTrackValue (value, fallback)
677{
678 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
679 return cleaned || fallback;
680}
681
682function trackLanguage (stream)
683{
684 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
685}
686
687function trackLabel (stream, fallback)
688{
689 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
690 if (title) return title;
691 const language = trackLanguage(stream);
692 if (language && language !== 'und') return `${fallback} (${language})`;
693 return fallback;
694}
695
696function findMatchingSidecarSrtPath (sourcePath)
697{
698 const absoluteSourcePath = path.resolve(String(sourcePath || ''));
699 const dirPath = path.dirname(absoluteSourcePath);
700 const baseName = path.parse(absoluteSourcePath).name;
701 const exactPath = path.join(dirPath, `${baseName}.srt`);
702
703 try
704 {
705 const exactStat = fs.statSync(exactPath);
706 if (exactStat.isFile()) return exactPath;
707 } catch
708 {
709 // Fall back to case-insensitive directory scan.
710 }
711
712 const wantedName = `${baseName}.srt`.toLowerCase();
713
714 try
715 {
716 const entries = fs.readdirSync(dirPath, { withFileTypes: true });
717 for (const entry of entries)
718 {
719 if (!entry.isFile()) continue;
720 if (entry.name.toLowerCase() !== wantedName) continue;
721 return path.join(dirPath, entry.name);
722 }
723 } catch
724 {
725 return null;
726 }
727
728 return null;
729}
730
731function stripSubtitleEditorNotes (text)
732{
733 return String(text || '')
734 .replace(/\{[^{}]*\}/g, '')
735 .replace(/[ \t]{2,}/g, ' ')
736 .trim();
737}
738
739function isWebVttStructuralLine (line)
740{
741 const trimmed = String(line || '').trim();
742 if (!trimmed) return true;
743 if (/^WEBVTT\b/i.test(trimmed)) return true;
744 if (trimmed.includes('-->')) return true;
745 if (/^(NOTE|STYLE|REGION)\b/i.test(trimmed)) return true;
746 if (/^\d+$/.test(trimmed)) return true;
747 return false;
748}
749
750function sanitizeWebVttContent (vttContent)
751{
752 return String(vttContent || '').split(/\r?\n/).map((line) =>
753 {
754 if (isWebVttStructuralLine(line)) return line;
755 return stripSubtitleEditorNotes(line);
756 }).join('\n');
757}
758
759async function sanitizeWebVttFile (filePath)
760{
761 const raw = await fs.promises.readFile(filePath, 'utf8');
762 const sanitized = sanitizeWebVttContent(raw);
763 if (sanitized !== raw)
764 {
765 await fs.promises.writeFile(filePath, sanitized, 'utf8');
766 }
767}
768
769async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
770{
771 const extractedTracks = [];
772 const imageBasedTracks = [];
773 let skippedCount = 0;
774 let trackNumber = 0;
775
776 for (const stream of subtitleStreams)
777 {
778 const codecName = String(stream.codec_name || '').toLowerCase();
779
780 // Image-based subtitle formats will be shown as DOM overlays via frame extraction.
781 if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))
782 {
783 imageBasedTracks.push({
784 label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),
785 language: trackLanguage(stream),
786 isDefault: !!stream.disposition.default,
787 streamIndex: stream.relativeIndex,
788 });
789 continue;
790 }
791
792 const fileName = `subtitle-${trackNumber}.vtt`;
793 const targetPath = path.join(outputDir, fileName);
794
795 try
796 {
797 await runCommand(ffmpegPath(), [
798 '-hide_banner',
799 '-loglevel', 'error',
800 '-nostdin',
801 '-y',
802 '-i', sourcePath,
803 '-map', `0:s:${stream.relativeIndex}`,
804 '-c:s', 'webvtt',
805 targetPath,
806 ]);
807 } catch
808 {
809 skippedCount += 1;
810 continue;
811 }
812
813 try
814 {
815 await sanitizeWebVttFile(targetPath);
816 } catch
817 {
818 skippedCount += 1;
819 continue;
820 }
821
822 extractedTracks.push({
823 fileName,
824 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
825 language: trackLanguage(stream),
826 isDefault: !!stream.disposition.default,
827 });
828 trackNumber += 1;
829 }
830
831 const sidecarSrtPath = findMatchingSidecarSrtPath(sourcePath);
832 const hasSidecarSrt = !!sidecarSrtPath;
833 if (sidecarSrtPath)
834 {
835 const fileName = `subtitle-${trackNumber}.vtt`;
836 const targetPath = path.join(outputDir, fileName);
837 try
838 {
839 await runCommand(ffmpegPath(), [
840 '-hide_banner',
841 '-loglevel', 'error',
842 '-nostdin',
843 '-y',
844 '-i', sidecarSrtPath,
845 '-c:s', 'webvtt',
846 targetPath,
847 ]);
848 await sanitizeWebVttFile(targetPath);
849
850 extractedTracks.push({
851 fileName,
852 label: `Subtitle ${trackNumber + 1} (external)`,
853 language: 'und',
854 isDefault: extractedTracks.length === 0 && imageBasedTracks.length === 0,
855 });
856 trackNumber += 1;
857 } catch
858 {
859 skippedCount += 1;
860 }
861 }
862
863 return {
864 tracks: extractedTracks,
865 imageBasedTracks,
866 hasSidecarSrt,
867 skippedCount,
868 };
869}
870
871function buildHlsArgs (sourcePath, outputDir, streamInfo)
872{
873 const args = [
874 '-hide_banner',
875 '-loglevel', 'error',
876 '-nostdin',
877 '-y',
878 '-i', sourcePath,
879 '-map', '0:v:0',
880 ];
881
882 for (const audioStream of streamInfo.audio)
883 {
884 args.push('-map', `0:a:${audioStream.relativeIndex}`);
885 }
886
887 args.push(
888 '-c:v', 'libx264',
889 '-preset', 'veryfast',
890 '-crf', '23',
891 '-pix_fmt', 'yuv420p',
892 '-g', '48',
893 '-sc_threshold', '0',
894 );
895
896 if (streamInfo.audio.length)
897 {
898 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
899 } else
900 {
901 args.push('-an');
902 }
903
904 const variantMap = [];
905 if (streamInfo.audio.length)
906 {
907 variantMap.push('v:0,agroup:audio,name:video');
908 streamInfo.audio.forEach((audioStream, index) =>
909 {
910 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
911 if (audioStream.disposition.default) entry += ',default:yes';
912 variantMap.push(entry);
913 });
914 } else
915 {
916 variantMap.push('v:0,name:video');
917 }
918
919 args.push(
920 '-f', 'hls',
921 '-hls_time', '4',
922 '-hls_list_size', '0',
923 '-hls_flags', 'independent_segments+temp_file',
924 '-master_pl_name', 'master.m3u8',
925 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
926 '-var_stream_map', variantMap.join(' '),
927 path.join(outputDir, 'stream_%v.m3u8'),
928 );
929
930 return args;
931}
932
933function startHlsTranscode (session, streamInfo)
934{
935 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
936 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
937 let stderr = '';
938
939 child.stderr.on('data', (chunk) =>
940 {
941 stderr += chunk.toString();
942 if (stderr.length > 16000) stderr = stderr.slice(-16000);
943 });
944
945 child.on('error', (err) =>
946 {
947 if (err && err.code === 'ENOENT')
948 {
949 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
950 return;
951 }
952 session.error = err;
953 });
954
955 child.on('close', (code) =>
956 {
957 session.process = null;
958 if (code === 0 || session.error) return;
959 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
960 });
961
962 session.process = child;
963}
964
965function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
966{
967 const startedAt = Date.now();
968
969 return new Promise((resolve, reject) =>
970 {
971 const check = () =>
972 {
973 if (fs.existsSync(targetPath))
974 {
975 resolve();
976 return;
977 }
978
979 if (session.error)
980 {
981 reject(session.error);
982 return;
983 }
984
985 if (Date.now() - startedAt >= timeoutMs)
986 {
987 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
988 return;
989 }
990
991 setTimeout(check, 120);
992 };
993
994 check();
995 });
996}
997
998function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
999{
1000 const startedAt = Date.now();
1001
1002 return new Promise((resolve, reject) =>
1003 {
1004 const check = () =>
1005 {
1006 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
1007 {
1008 resolve();
1009 return;
1010 }
1011
1012 if (session.error)
1013 {
1014 reject(session.error);
1015 return;
1016 }
1017
1018 if (!session.process)
1019 {
1020 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
1021 return;
1022 }
1023
1024 if (Date.now() - startedAt >= timeoutMs)
1025 {
1026 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
1027 return;
1028 }
1029
1030 setTimeout(check, 120);
1031 };
1032
1033 check();
1034 });
1035}
1036
1037async function initialiseSession (session)
1038{
1039 const streamInfo = await probeInput(session.sourcePath);
1040 if (!streamInfo.video.length)
1041 {
1042 throw makeHttpError(400, 'File does not contain a video stream.');
1043 }
1044
1045 const primaryVideoStream = streamInfo.video[0] || {};
1046 session.videoWidth = parsePositiveNumber(primaryVideoStream.width);
1047 session.videoHeight = parsePositiveNumber(primaryVideoStream.height);
1048
1049 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
1050 session.subtitleTracks = subtitleResult.tracks;
1051 session.imageSubtitleTracks = subtitleResult.imageBasedTracks;
1052 session.subtitleSummary = {
1053 total: streamInfo.subtitles.length + (subtitleResult.hasSidecarSrt ? 1 : 0),
1054 converted: subtitleResult.tracks.length,
1055 imageBased: subtitleResult.imageBasedTracks.length,
1056 skipped: subtitleResult.skippedCount,
1057 };
1058 startHlsTranscode(session, streamInfo);
1059 session.durationSeconds = streamInfo.durationSeconds;
1060 await waitForStreamStart(session);
1061}
1062
1063async function createVideoStreamSession (sourcePath)
1064{
1065 pruneExpiredSessions();
1066 ensureStreamRootDir();
1067
1068 const sourceKey = String(sourcePath || '');
1069 let session = null;
1070
1071 const reusableSessionId = reusableSessionIdBySource.get(sourceKey);
1072 if (reusableSessionId)
1073 {
1074 const reusable = sessions.get(reusableSessionId);
1075 if (reusable && !reusable.error && reusable.segmentPrunedUntil < 0)
1076 {
1077 session = reusable;
1078 } else
1079 {
1080 reusableSessionIdBySource.delete(sourceKey);
1081 }
1082 }
1083
1084 if (!session)
1085 {
1086 const sessionId = crypto.randomBytes(18).toString('hex');
1087 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
1088 fs.mkdirSync(outputDir, { recursive: true });
1089
1090 session = {
1091 id: sessionId,
1092 sourcePath: String(sourcePath || ''),
1093 sourceKey: sourceKey,
1094 outputDir,
1095 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
1096 process: null,
1097 error: null,
1098 subtitleTracks: [],
1099 imageSubtitleTracks: [],
1100 subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },
1101 readyPromise: null,
1102 activeRequests: 0,
1103 lastActivityAt: Date.now(),
1104 idleTimer: null,
1105 segmentPruneRunning: false,
1106 segmentPrunePendingCutoff: -1,
1107 segmentPrunedUntil: -1,
1108 lastSegmentPrune: null,
1109 leaseIds: new Set(),
1110 };
1111
1112 session.readyPromise = initialiseSession(session).catch((err) =>
1113 {
1114 session.error = err;
1115 throw err;
1116 });
1117
1118 sessions.set(session.id, session);
1119 reusableSessionIdBySource.set(sourceKey, session.id);
1120 }
1121
1122 if (!session.leaseIds) session.leaseIds = new Set();
1123 const lease = createLeaseForSession(session);
1124 touchSessionActivity(session, lease);
1125
1126 try
1127 {
1128 await session.readyPromise;
1129 } catch (err)
1130 {
1131 releaseVideoStreamSession(lease.id);
1132 throw err;
1133 }
1134
1135 return {
1136 sessionId: lease.id,
1137 durationSeconds: session.durationSeconds,
1138 videoWidth: session.videoWidth || null,
1139 videoHeight: session.videoHeight || null,
1140 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
1141 imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),
1142 subtitleSummary: {
1143 total: Number(session.subtitleSummary.total || 0),
1144 converted: Number(session.subtitleSummary.converted || 0),
1145 imageBased: Number(session.subtitleSummary.imageBased || 0),
1146 skipped: Number(session.subtitleSummary.skipped || 0),
1147 },
1148 };
1149}
1150
1151function contentTypeForAsset (assetPath)
1152{
1153 const ext = path.extname(assetPath).toLowerCase();
1154 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
1155 if (ext === '.ts') return 'video/mp2t';
1156 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
1157 if (ext === '.aac') return 'audio/aac';
1158 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
1159 return 'application/octet-stream';
1160}
1161
1162async function sendVideoStreamAsset (sessionId, assetPath, req, res)
1163{
1164 pruneExpiredSessions();
1165 const active = getSessionForLease(sessionId);
1166 if (!active) throw makeHttpError(404, 'Video stream session not found.');
1167
1168 const { session, lease } = active;
1169
1170 touchSessionActivity(session, lease);
1171 await session.readyPromise;
1172
1173 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
1174 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
1175 const resolvedRoot = path.resolve(session.outputDir);
1176 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
1177 {
1178 throw makeHttpError(404, 'Stream asset not found.');
1179 }
1180
1181 const segmentIndex = parseSegmentIndexFromAssetPath(relativeAssetPath);
1182 if (segmentIndex !== null) scheduleSegmentPrune(session, lease, segmentIndex);
1183
1184 await waitForFile(resolvedPath, session);
1185 res.set('Cache-Control', 'private, no-store');
1186 res.type(contentTypeForAsset(resolvedPath));
1187
1188 startAssetRequest(session, lease);
1189
1190 let settled = false;
1191 const finish = () =>
1192 {
1193 if (settled) return;
1194 settled = true;
1195 finishAssetRequest(session, lease);
1196 };
1197
1198 req.on('aborted', finish);
1199 res.on('close', finish);
1200 res.on('finish', finish);
1201 res.on('error', finish);
1202
1203 await new Promise((resolve, reject) =>
1204 {
1205 res.sendFile(resolvedPath, (err) =>
1206 {
1207 finish();
1208 if (!err)
1209 {
1210 resolve();
1211 return;
1212 }
1213
1214 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
1215 {
1216 resolve();
1217 return;
1218 }
1219
1220 reject(err);
1221 });
1222 });
1223}
1224
1225function releaseVideoStreamSession (sessionId)
1226{
1227 const leaseId = String(sessionId || '');
1228 const active = getSessionForLease(leaseId);
1229 if (!active)
1230 {
1231 leases.delete(leaseId);
1232 return false;
1233 }
1234
1235 const { session } = active;
1236 leases.delete(leaseId);
1237 session.leaseIds.delete(leaseId);
1238
1239 if (session.leaseIds.size === 0)
1240 {
1241 return destroySession(session.id);
1242 }
1243
1244 return true;
1245}
1246
1247function collectOutputDirStats (outputDir)
1248{
1249 const stats = {
1250 exists: false,
1251 totalBytes: 0,
1252 totalFiles: 0,
1253 segmentFiles: 0,
1254 segmentBytes: 0,
1255 playlistFiles: 0,
1256 subtitleFiles: 0,
1257 };
1258
1259 if (!outputDir || !fs.existsSync(outputDir)) return stats;
1260 stats.exists = true;
1261
1262 let entries = [];
1263 try
1264 {
1265 entries = fs.readdirSync(outputDir, { withFileTypes: true });
1266 } catch
1267 {
1268 return stats;
1269 }
1270
1271 for (const entry of entries)
1272 {
1273 if (!entry.isFile()) continue;
1274 const name = entry.name;
1275 const fullPath = path.join(outputDir, name);
1276 let size = 0;
1277 try
1278 {
1279 const fileStat = fs.statSync(fullPath);
1280 size = Number(fileStat.size) || 0;
1281 } catch
1282 {
1283 size = 0;
1284 }
1285
1286 stats.totalFiles += 1;
1287 stats.totalBytes += size;
1288
1289 if (/\.ts$/i.test(name))
1290 {
1291 stats.segmentFiles += 1;
1292 stats.segmentBytes += size;
1293 continue;
1294 }
1295
1296 if (/\.m3u8$/i.test(name))
1297 {
1298 stats.playlistFiles += 1;
1299 continue;
1300 }
1301
1302 if (/\.(sup|vtt)$/i.test(name))
1303 {
1304 stats.subtitleFiles += 1;
1305 }
1306 }
1307
1308 return stats;
1309}
1310
1311function getVideoStreamDebugSnapshot ()
1312{
1313 const activeStreams = [];
1314
1315 for (const [sessionId, session] of sessions.entries())
1316 {
1317 activeStreams.push({
1318 sessionId,
1319 sourcePath: session.sourcePath,
1320 outputDir: session.outputDir,
1321 viewers: session.leaseIds ? session.leaseIds.size : 0,
1322 activeRequests: Number(session.activeRequests || 0),
1323 expiresAt: Number(session.expiresAt || 0),
1324 segmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1325 segmentPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1326 segmentPrunePendingCutoff: Number(session.segmentPrunePendingCutoff || 0),
1327 segmentPrunedUntil: Number(session.segmentPrunedUntil || 0),
1328 slowestViewerSegmentIndex: slowestLeaseSegmentIndex(session) ?? -1,
1329 lastSegmentPrune: session.lastSegmentPrune || null,
1330 output: collectOutputDirStats(session.outputDir),
1331 });
1332 }
1333
1334 const knownSessionDirs = new Set(activeStreams.map((stream) => path.resolve(stream.outputDir || '')));
1335 const orphanedOutputDirs = [];
1336 try
1337 {
1338 const entries = fs.readdirSync(STREAM_ROOT_DIR, { withFileTypes: true });
1339 for (const entry of entries)
1340 {
1341 if (!entry.isDirectory()) continue;
1342 const dirPath = path.resolve(path.join(STREAM_ROOT_DIR, entry.name));
1343 if (knownSessionDirs.has(dirPath)) continue;
1344 orphanedOutputDirs.push({
1345 outputDir: dirPath,
1346 output: collectOutputDirStats(dirPath),
1347 });
1348 }
1349 } catch
1350 {
1351 // Stream root may not exist yet.
1352 }
1353
1354 return {
1355 streamRootDir: STREAM_ROOT_DIR,
1356 streamSegmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1357 streamSegmentRetentionMax: MAX_STREAM_SEGMENT_RETENTION_COUNT,
1358 streamPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1359 activeSessionCount: sessions.size,
1360 activeLeaseCount: leases.size,
1361 activeStreams,
1362 orphanedOutputDirs,
1363 };
1364}
1365
1366function getActiveStreamCount ()
1367{
1368 return sessions.size;
1369}
1370
1371module.exports = {
1372 STREAM_ROOT_DIR,
1373 STREAM_SESSION_TTL_MS,
1374 STREAM_IDLE_STOP_MS,
1375 STREAM_SEGMENT_RETENTION_COUNT,
1376 createVideoStreamSession,
1377 sendVideoStreamAsset,
1378 releaseVideoStreamSession,
1379 getActiveStreamCount,
1380 getVideoStreamDebugSnapshot,
1381 cleanupIdleStreamArtifacts,
1382 stripSubtitleEditorNotes,
1383 sanitizeWebVttContent,
1384};