BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

d5a02468 History
video-stream.js 34.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 { 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 touchSessionActivity(session, lease);
413 if (session.activeRequests === 0 && session.leaseIds.size === 0) scheduleIdleStop(session);
414}
415
416function parseSegmentIndexFromAssetPath (assetPath)
417{
418 const baseName = path.basename(String(assetPath || ''));
419 const match = baseName.match(/^stream_.+_segment_(\d+)\.ts$/i);
420 if (!match) return null;
421
422 const segmentIndex = Number.parseInt(match[1], 10);
423 return Number.isInteger(segmentIndex) ? segmentIndex : null;
424}
425
426function runSegmentPrune (session)
427{
428 if (session.segmentPruneRunning) return;
429 if (session.segmentPrunePendingCutoff <= session.segmentPrunedUntil) return;
430
431 const cutoff = session.segmentPrunePendingCutoff;
432 const pruneStartedAt = Date.now();
433 session.segmentPruneRunning = true;
434
435 fs.promises.readdir(session.outputDir)
436 .then((entries) =>
437 {
438 const removals = [];
439 for (const entryName of entries)
440 {
441 const match = String(entryName).match(/^stream_.+_segment_(\d+)\.ts$/i);
442 if (!match) continue;
443 const segmentIndex = Number.parseInt(match[1], 10);
444 if (!Number.isInteger(segmentIndex)) continue;
445 if (segmentIndex > cutoff) continue;
446
447 removals.push(path.join(session.outputDir, entryName));
448 }
449 return deleteFilesInBatches(removals, STREAM_PRUNE_DELETE_BATCH_SIZE);
450 })
451 .then((deletedCount) =>
452 {
453 const completedAt = Date.now();
454 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
455 session.lastSegmentPrune = {
456 cutoff,
457 deletedSegments: deletedCount,
458 durationMs: Math.max(0, completedAt - pruneStartedAt),
459 completedAt,
460 };
461 pruneImageSubtitleCachesForSession(session);
462 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
463 {
464 reusableSessionIdBySource.delete(session.sourcePath);
465 }
466 })
467 .catch(() =>
468 {
469 // Best effort cleanup; keep streaming even if pruning fails.
470 })
471 .finally(() =>
472 {
473 session.segmentPruneRunning = false;
474 if (session.segmentPrunePendingCutoff > session.segmentPrunedUntil)
475 {
476 runSegmentPrune(session);
477 }
478 });
479}
480
481async function deleteFilesInBatches (pathsToDelete, batchSize)
482{
483 const paths = Array.isArray(pathsToDelete) ? pathsToDelete : [];
484 if (!paths.length) return 0;
485
486 let deletedCount = 0;
487 for (let index = 0; index < paths.length; index += batchSize)
488 {
489 const batch = paths.slice(index, index + batchSize);
490 await Promise.all(batch.map((targetPath) =>
491 fs.promises.unlink(targetPath)
492 .then(() =>
493 {
494 deletedCount += 1;
495 })
496 .catch(() =>
497 {
498 // Ignore races while ffmpeg rotates temp files and request handlers read segments.
499 })
500 ));
501 }
502
503 return deletedCount;
504}
505
506function slowestLeaseSegmentIndex (session)
507{
508 if (!session.leaseIds.size) return null;
509
510 let minIndex = null;
511 for (const leaseId of session.leaseIds)
512 {
513 const lease = leases.get(leaseId);
514 if (!lease) continue;
515 if (!Number.isInteger(lease.lastSegmentIndex)) return null;
516 if (minIndex === null || lease.lastSegmentIndex < minIndex) minIndex = lease.lastSegmentIndex;
517 }
518
519 return minIndex;
520}
521
522function scheduleSegmentPrune (session, lease, segmentIndex)
523{
524 if (!Number.isInteger(segmentIndex) || segmentIndex < 0) return;
525
526 lease.lastSegmentIndex = segmentIndex;
527 const slowestIndex = slowestLeaseSegmentIndex(session);
528 if (!Number.isInteger(slowestIndex)) return;
529
530 const pruneCutoff = slowestIndex - STREAM_SEGMENT_RETENTION_COUNT;
531 if (pruneCutoff <= session.segmentPrunedUntil) return;
532
533 session.segmentPrunePendingCutoff = Math.max(session.segmentPrunePendingCutoff, pruneCutoff);
534 runSegmentPrune(session);
535}
536
537function runCommand (command, args)
538{
539 return new Promise((resolve, reject) =>
540 {
541 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
542 let stdout = '';
543 let stderr = '';
544
545 child.stdout.on('data', (chunk) =>
546 {
547 stdout += chunk.toString();
548 });
549
550 child.stderr.on('data', (chunk) =>
551 {
552 stderr += chunk.toString();
553 });
554
555 child.on('error', (err) =>
556 {
557 if (err && err.code === 'ENOENT')
558 {
559 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
560 return;
561 }
562 reject(err);
563 });
564
565 child.on('close', (code) =>
566 {
567 if (code === 0)
568 {
569 resolve({ stdout, stderr });
570 return;
571 }
572 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
573 });
574 });
575}
576
577async function probeInput (sourcePath)
578{
579 const { stdout } = await runCommand(ffprobePath(), [
580 '-v', 'error',
581 '-print_format', 'json',
582 '-show_streams',
583 '-show_format',
584 sourcePath,
585 ]);
586
587 let payload;
588 try
589 {
590 payload = JSON.parse(stdout || '{}');
591 } catch
592 {
593 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
594 }
595
596 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
597 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
598 {
599 const codecType = String(stream.codec_type || '').toLowerCase();
600 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
601 ? typedIndexes[codecType]++
602 : null;
603 return {
604 ...stream,
605 codecType,
606 relativeIndex,
607 tags: stream.tags || {},
608 disposition: stream.disposition || {},
609 };
610 }) : [];
611
612 return {
613 raw: payload,
614 durationSeconds: inputDurationSeconds(payload, streams),
615 video: streams.filter((stream) => stream.codecType === 'video'),
616 audio: streams.filter((stream) => stream.codecType === 'audio'),
617 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
618 };
619}
620
621function parsePositiveNumber (value)
622{
623 const number = Number(value);
624 return Number.isFinite(number) && number > 0 ? number : null;
625}
626
627function parseDurationTagSeconds (value)
628{
629 const raw = String(value || '').trim();
630 if (!raw) return null;
631
632 const numeric = parsePositiveNumber(raw);
633 if (numeric) return numeric;
634
635 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
636 if (!match) return null;
637
638 const hours = Number(match[1]);
639 const minutes = Number(match[2]);
640 const seconds = Number(match[3]);
641 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
642 return hours * 3600 + minutes * 60 + seconds;
643}
644
645function inputDurationSeconds (payload, streams)
646{
647 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
648 if (formatDuration) return formatDuration;
649
650 for (const stream of streams)
651 {
652 const duration = parsePositiveNumber(stream.duration);
653 if (duration) return duration;
654 }
655
656 const formatTags = payload && payload.format && payload.format.tags;
657 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
658 if (formatTagDuration) return formatTagDuration;
659
660 for (const stream of streams)
661 {
662 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
663 if (tagDuration) return tagDuration;
664 }
665
666 return null;
667}
668
669function sanitiseTrackValue (value, fallback)
670{
671 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
672 return cleaned || fallback;
673}
674
675function trackLanguage (stream)
676{
677 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
678}
679
680function trackLabel (stream, fallback)
681{
682 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
683 if (title) return title;
684 const language = trackLanguage(stream);
685 if (language && language !== 'und') return `${fallback} (${language})`;
686 return fallback;
687}
688
689function findMatchingSidecarSrtPath (sourcePath)
690{
691 const absoluteSourcePath = path.resolve(String(sourcePath || ''));
692 const dirPath = path.dirname(absoluteSourcePath);
693 const baseName = path.parse(absoluteSourcePath).name;
694 const exactPath = path.join(dirPath, `${baseName}.srt`);
695
696 try
697 {
698 const exactStat = fs.statSync(exactPath);
699 if (exactStat.isFile()) return exactPath;
700 } catch
701 {
702 // Fall back to case-insensitive directory scan.
703 }
704
705 const wantedName = `${baseName}.srt`.toLowerCase();
706
707 try
708 {
709 const entries = fs.readdirSync(dirPath, { withFileTypes: true });
710 for (const entry of entries)
711 {
712 if (!entry.isFile()) continue;
713 if (entry.name.toLowerCase() !== wantedName) continue;
714 return path.join(dirPath, entry.name);
715 }
716 } catch
717 {
718 return null;
719 }
720
721 return null;
722}
723
724async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
725{
726 const extractedTracks = [];
727 const imageBasedTracks = [];
728 let skippedCount = 0;
729 let trackNumber = 0;
730
731 for (const stream of subtitleStreams)
732 {
733 const codecName = String(stream.codec_name || '').toLowerCase();
734
735 // Image-based subtitle formats will be shown as DOM overlays via frame extraction.
736 if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))
737 {
738 imageBasedTracks.push({
739 label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),
740 language: trackLanguage(stream),
741 isDefault: !!stream.disposition.default,
742 streamIndex: stream.relativeIndex,
743 });
744 continue;
745 }
746
747 const fileName = `subtitle-${trackNumber}.vtt`;
748 const targetPath = path.join(outputDir, fileName);
749
750 try
751 {
752 await runCommand(ffmpegPath(), [
753 '-hide_banner',
754 '-loglevel', 'error',
755 '-nostdin',
756 '-y',
757 '-i', sourcePath,
758 '-map', `0:s:${stream.relativeIndex}`,
759 '-c:s', 'webvtt',
760 targetPath,
761 ]);
762 } catch
763 {
764 skippedCount += 1;
765 continue;
766 }
767
768 extractedTracks.push({
769 fileName,
770 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
771 language: trackLanguage(stream),
772 isDefault: !!stream.disposition.default,
773 });
774 trackNumber += 1;
775 }
776
777 const sidecarSrtPath = findMatchingSidecarSrtPath(sourcePath);
778 const hasSidecarSrt = !!sidecarSrtPath;
779 if (sidecarSrtPath)
780 {
781 const fileName = `subtitle-${trackNumber}.vtt`;
782 const targetPath = path.join(outputDir, fileName);
783 try
784 {
785 await runCommand(ffmpegPath(), [
786 '-hide_banner',
787 '-loglevel', 'error',
788 '-nostdin',
789 '-y',
790 '-i', sidecarSrtPath,
791 '-c:s', 'webvtt',
792 targetPath,
793 ]);
794
795 extractedTracks.push({
796 fileName,
797 label: `Subtitle ${trackNumber + 1} (external)`,
798 language: 'und',
799 isDefault: extractedTracks.length === 0 && imageBasedTracks.length === 0,
800 });
801 trackNumber += 1;
802 } catch
803 {
804 skippedCount += 1;
805 }
806 }
807
808 return {
809 tracks: extractedTracks,
810 imageBasedTracks,
811 hasSidecarSrt,
812 skippedCount,
813 };
814}
815
816function buildHlsArgs (sourcePath, outputDir, streamInfo)
817{
818 const args = [
819 '-hide_banner',
820 '-loglevel', 'error',
821 '-nostdin',
822 '-y',
823 '-i', sourcePath,
824 '-map', '0:v:0',
825 ];
826
827 for (const audioStream of streamInfo.audio)
828 {
829 args.push('-map', `0:a:${audioStream.relativeIndex}`);
830 }
831
832 args.push(
833 '-c:v', 'libx264',
834 '-preset', 'veryfast',
835 '-crf', '23',
836 '-pix_fmt', 'yuv420p',
837 '-g', '48',
838 '-sc_threshold', '0',
839 );
840
841 if (streamInfo.audio.length)
842 {
843 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
844 } else
845 {
846 args.push('-an');
847 }
848
849 const variantMap = [];
850 if (streamInfo.audio.length)
851 {
852 variantMap.push('v:0,agroup:audio,name:video');
853 streamInfo.audio.forEach((audioStream, index) =>
854 {
855 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
856 if (audioStream.disposition.default) entry += ',default:yes';
857 variantMap.push(entry);
858 });
859 } else
860 {
861 variantMap.push('v:0,name:video');
862 }
863
864 args.push(
865 '-f', 'hls',
866 '-hls_time', '4',
867 '-hls_list_size', '0',
868 '-hls_flags', 'independent_segments+temp_file',
869 '-master_pl_name', 'master.m3u8',
870 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
871 '-var_stream_map', variantMap.join(' '),
872 path.join(outputDir, 'stream_%v.m3u8'),
873 );
874
875 return args;
876}
877
878function startHlsTranscode (session, streamInfo)
879{
880 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
881 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
882 let stderr = '';
883
884 child.stderr.on('data', (chunk) =>
885 {
886 stderr += chunk.toString();
887 if (stderr.length > 16000) stderr = stderr.slice(-16000);
888 });
889
890 child.on('error', (err) =>
891 {
892 if (err && err.code === 'ENOENT')
893 {
894 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
895 return;
896 }
897 session.error = err;
898 });
899
900 child.on('close', (code) =>
901 {
902 session.process = null;
903 if (code === 0 || session.error) return;
904 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
905 });
906
907 session.process = child;
908}
909
910function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
911{
912 const startedAt = Date.now();
913
914 return new Promise((resolve, reject) =>
915 {
916 const check = () =>
917 {
918 if (fs.existsSync(targetPath))
919 {
920 resolve();
921 return;
922 }
923
924 if (session.error)
925 {
926 reject(session.error);
927 return;
928 }
929
930 if (Date.now() - startedAt >= timeoutMs)
931 {
932 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
933 return;
934 }
935
936 setTimeout(check, 120);
937 };
938
939 check();
940 });
941}
942
943function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
944{
945 const startedAt = Date.now();
946
947 return new Promise((resolve, reject) =>
948 {
949 const check = () =>
950 {
951 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
952 {
953 resolve();
954 return;
955 }
956
957 if (session.error)
958 {
959 reject(session.error);
960 return;
961 }
962
963 if (!session.process)
964 {
965 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
966 return;
967 }
968
969 if (Date.now() - startedAt >= timeoutMs)
970 {
971 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
972 return;
973 }
974
975 setTimeout(check, 120);
976 };
977
978 check();
979 });
980}
981
982async function initialiseSession (session)
983{
984 const streamInfo = await probeInput(session.sourcePath);
985 if (!streamInfo.video.length)
986 {
987 throw makeHttpError(400, 'File does not contain a video stream.');
988 }
989
990 const primaryVideoStream = streamInfo.video[0] || {};
991 session.videoWidth = parsePositiveNumber(primaryVideoStream.width);
992 session.videoHeight = parsePositiveNumber(primaryVideoStream.height);
993
994 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
995 session.subtitleTracks = subtitleResult.tracks;
996 session.imageSubtitleTracks = subtitleResult.imageBasedTracks;
997 session.subtitleSummary = {
998 total: streamInfo.subtitles.length + (subtitleResult.hasSidecarSrt ? 1 : 0),
999 converted: subtitleResult.tracks.length,
1000 imageBased: subtitleResult.imageBasedTracks.length,
1001 skipped: subtitleResult.skippedCount,
1002 };
1003 startHlsTranscode(session, streamInfo);
1004 session.durationSeconds = streamInfo.durationSeconds;
1005 await waitForStreamStart(session);
1006}
1007
1008async function createVideoStreamSession (sourcePath)
1009{
1010 pruneExpiredSessions();
1011 ensureStreamRootDir();
1012
1013 const sourceKey = String(sourcePath || '');
1014 let session = null;
1015
1016 const reusableSessionId = reusableSessionIdBySource.get(sourceKey);
1017 if (reusableSessionId)
1018 {
1019 const reusable = sessions.get(reusableSessionId);
1020 if (reusable && !reusable.error && reusable.segmentPrunedUntil < 0)
1021 {
1022 session = reusable;
1023 } else
1024 {
1025 reusableSessionIdBySource.delete(sourceKey);
1026 }
1027 }
1028
1029 if (!session)
1030 {
1031 const sessionId = crypto.randomBytes(18).toString('hex');
1032 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
1033 fs.mkdirSync(outputDir, { recursive: true });
1034
1035 session = {
1036 id: sessionId,
1037 sourcePath: String(sourcePath || ''),
1038 sourceKey: sourceKey,
1039 outputDir,
1040 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
1041 process: null,
1042 error: null,
1043 subtitleTracks: [],
1044 imageSubtitleTracks: [],
1045 subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },
1046 readyPromise: null,
1047 activeRequests: 0,
1048 lastActivityAt: Date.now(),
1049 idleTimer: null,
1050 segmentPruneRunning: false,
1051 segmentPrunePendingCutoff: -1,
1052 segmentPrunedUntil: -1,
1053 lastSegmentPrune: null,
1054 leaseIds: new Set(),
1055 };
1056
1057 session.readyPromise = initialiseSession(session).catch((err) =>
1058 {
1059 session.error = err;
1060 throw err;
1061 });
1062
1063 sessions.set(session.id, session);
1064 reusableSessionIdBySource.set(sourceKey, session.id);
1065 }
1066
1067 if (!session.leaseIds) session.leaseIds = new Set();
1068 const lease = createLeaseForSession(session);
1069 touchSessionActivity(session, lease);
1070
1071 try
1072 {
1073 await session.readyPromise;
1074 } catch (err)
1075 {
1076 releaseVideoStreamSession(lease.id);
1077 throw err;
1078 }
1079
1080 return {
1081 sessionId: lease.id,
1082 durationSeconds: session.durationSeconds,
1083 videoWidth: session.videoWidth || null,
1084 videoHeight: session.videoHeight || null,
1085 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
1086 imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),
1087 subtitleSummary: {
1088 total: Number(session.subtitleSummary.total || 0),
1089 converted: Number(session.subtitleSummary.converted || 0),
1090 imageBased: Number(session.subtitleSummary.imageBased || 0),
1091 skipped: Number(session.subtitleSummary.skipped || 0),
1092 },
1093 };
1094}
1095
1096function contentTypeForAsset (assetPath)
1097{
1098 const ext = path.extname(assetPath).toLowerCase();
1099 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
1100 if (ext === '.ts') return 'video/mp2t';
1101 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
1102 if (ext === '.aac') return 'audio/aac';
1103 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
1104 return 'application/octet-stream';
1105}
1106
1107async function sendVideoStreamAsset (sessionId, assetPath, req, res)
1108{
1109 pruneExpiredSessions();
1110 const active = getSessionForLease(sessionId);
1111 if (!active) throw makeHttpError(404, 'Video stream session not found.');
1112
1113 const { session, lease } = active;
1114
1115 touchSessionActivity(session, lease);
1116 await session.readyPromise;
1117
1118 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
1119 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
1120 const resolvedRoot = path.resolve(session.outputDir);
1121 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
1122 {
1123 throw makeHttpError(404, 'Stream asset not found.');
1124 }
1125
1126 const segmentIndex = parseSegmentIndexFromAssetPath(relativeAssetPath);
1127 if (segmentIndex !== null) scheduleSegmentPrune(session, lease, segmentIndex);
1128
1129 await waitForFile(resolvedPath, session);
1130 res.set('Cache-Control', 'private, no-store');
1131 res.type(contentTypeForAsset(resolvedPath));
1132
1133 startAssetRequest(session, lease);
1134
1135 let settled = false;
1136 const finish = () =>
1137 {
1138 if (settled) return;
1139 settled = true;
1140 finishAssetRequest(session, lease);
1141 };
1142
1143 req.on('aborted', finish);
1144 res.on('close', finish);
1145 res.on('finish', finish);
1146 res.on('error', finish);
1147
1148 await new Promise((resolve, reject) =>
1149 {
1150 res.sendFile(resolvedPath, (err) =>
1151 {
1152 finish();
1153 if (!err)
1154 {
1155 resolve();
1156 return;
1157 }
1158
1159 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
1160 {
1161 resolve();
1162 return;
1163 }
1164
1165 reject(err);
1166 });
1167 });
1168}
1169
1170function releaseVideoStreamSession (sessionId)
1171{
1172 const leaseId = String(sessionId || '');
1173 const active = getSessionForLease(leaseId);
1174 if (!active)
1175 {
1176 leases.delete(leaseId);
1177 return false;
1178 }
1179
1180 const { session } = active;
1181 leases.delete(leaseId);
1182 session.leaseIds.delete(leaseId);
1183
1184 if (session.leaseIds.size === 0 && session.activeRequests === 0)
1185 {
1186 return destroySession(session.id);
1187 }
1188
1189 return true;
1190}
1191
1192function collectOutputDirStats (outputDir)
1193{
1194 const stats = {
1195 exists: false,
1196 totalBytes: 0,
1197 totalFiles: 0,
1198 segmentFiles: 0,
1199 segmentBytes: 0,
1200 playlistFiles: 0,
1201 subtitleFiles: 0,
1202 };
1203
1204 if (!outputDir || !fs.existsSync(outputDir)) return stats;
1205 stats.exists = true;
1206
1207 let entries = [];
1208 try
1209 {
1210 entries = fs.readdirSync(outputDir, { withFileTypes: true });
1211 } catch
1212 {
1213 return stats;
1214 }
1215
1216 for (const entry of entries)
1217 {
1218 if (!entry.isFile()) continue;
1219 const name = entry.name;
1220 const fullPath = path.join(outputDir, name);
1221 let size = 0;
1222 try
1223 {
1224 const fileStat = fs.statSync(fullPath);
1225 size = Number(fileStat.size) || 0;
1226 } catch
1227 {
1228 size = 0;
1229 }
1230
1231 stats.totalFiles += 1;
1232 stats.totalBytes += size;
1233
1234 if (/\.ts$/i.test(name))
1235 {
1236 stats.segmentFiles += 1;
1237 stats.segmentBytes += size;
1238 continue;
1239 }
1240
1241 if (/\.m3u8$/i.test(name))
1242 {
1243 stats.playlistFiles += 1;
1244 continue;
1245 }
1246
1247 if (/\.(sup|vtt)$/i.test(name))
1248 {
1249 stats.subtitleFiles += 1;
1250 }
1251 }
1252
1253 return stats;
1254}
1255
1256function getVideoStreamDebugSnapshot ()
1257{
1258 const activeStreams = [];
1259
1260 for (const [sessionId, session] of sessions.entries())
1261 {
1262 activeStreams.push({
1263 sessionId,
1264 sourcePath: session.sourcePath,
1265 outputDir: session.outputDir,
1266 viewers: session.leaseIds ? session.leaseIds.size : 0,
1267 activeRequests: Number(session.activeRequests || 0),
1268 expiresAt: Number(session.expiresAt || 0),
1269 segmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1270 segmentPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1271 segmentPrunePendingCutoff: Number(session.segmentPrunePendingCutoff || 0),
1272 segmentPrunedUntil: Number(session.segmentPrunedUntil || 0),
1273 slowestViewerSegmentIndex: slowestLeaseSegmentIndex(session) ?? -1,
1274 lastSegmentPrune: session.lastSegmentPrune || null,
1275 output: collectOutputDirStats(session.outputDir),
1276 });
1277 }
1278
1279 const knownSessionDirs = new Set(activeStreams.map((stream) => path.resolve(stream.outputDir || '')));
1280 const orphanedOutputDirs = [];
1281 try
1282 {
1283 const entries = fs.readdirSync(STREAM_ROOT_DIR, { withFileTypes: true });
1284 for (const entry of entries)
1285 {
1286 if (!entry.isDirectory()) continue;
1287 const dirPath = path.resolve(path.join(STREAM_ROOT_DIR, entry.name));
1288 if (knownSessionDirs.has(dirPath)) continue;
1289 orphanedOutputDirs.push({
1290 outputDir: dirPath,
1291 output: collectOutputDirStats(dirPath),
1292 });
1293 }
1294 } catch
1295 {
1296 // Stream root may not exist yet.
1297 }
1298
1299 return {
1300 streamRootDir: STREAM_ROOT_DIR,
1301 streamSegmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1302 streamSegmentRetentionMax: MAX_STREAM_SEGMENT_RETENTION_COUNT,
1303 streamPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1304 activeSessionCount: sessions.size,
1305 activeLeaseCount: leases.size,
1306 activeStreams,
1307 orphanedOutputDirs,
1308 };
1309}
1310
1311function getActiveStreamCount ()
1312{
1313 return sessions.size;
1314}
1315
1316module.exports = {
1317 STREAM_ROOT_DIR,
1318 STREAM_SESSION_TTL_MS,
1319 STREAM_IDLE_STOP_MS,
1320 STREAM_SEGMENT_RETENTION_COUNT,
1321 createVideoStreamSession,
1322 sendVideoStreamAsset,
1323 releaseVideoStreamSession,
1324 getActiveStreamCount,
1325 getVideoStreamDebugSnapshot,
1326 cleanupIdleStreamArtifacts,
1327};