BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

b49a534c History
video-stream.js 18.0 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');
8
9const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
10const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
11const STREAM_READY_TIMEOUT_MS = 20 * 1000;
12const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
13const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);
14const sessions = new Map();
15
16function parseSegmentRetentionCount (value)
17{
18 const parsed = Number.parseInt(String(value || ''), 10);
19 if (Number.isInteger(parsed) && parsed >= 30) return parsed;
20 return 180;
21}
22
23function hasInitialHlsAssets (outputDir)
24{
25 try
26 {
27 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
28 return entries.some((entry) =>
29 {
30 if (!entry.isFile()) return false;
31 if (entry.name === 'master.m3u8') return true;
32 if (/^stream_\d+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
33 if (/^stream_\d+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
34 return false;
35 });
36 } catch
37 {
38 return false;
39 }
40}
41
42function makeHttpError (status, message)
43{
44 const err = new Error(message);
45 err.status = status;
46 return err;
47}
48
49function ensureStreamRootDir ()
50{
51 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
52}
53
54function ffmpegPath ()
55{
56 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
57}
58
59function ffprobePath ()
60{
61 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
62}
63
64function pruneExpiredSessions ()
65{
66 const now = Date.now();
67 for (const [sessionId, session] of sessions)
68 {
69 if (session.expiresAt > now) continue;
70 destroySession(sessionId);
71 }
72}
73
74function refreshSession (session)
75{
76 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
77}
78
79function destroySession (sessionId)
80{
81 const session = sessions.get(sessionId);
82 if (!session) return false;
83
84 sessions.delete(sessionId);
85
86 if (session.idleTimer)
87 {
88 clearTimeout(session.idleTimer);
89 session.idleTimer = null;
90 }
91
92 try
93 {
94 if (session.process) session.process.kill('SIGTERM');
95 } catch
96 {
97 // Ignore process cleanup failures.
98 }
99
100 fs.rmSync(session.outputDir, { recursive: true, force: true });
101 return true;
102}
103
104function touchSessionActivity (session)
105{
106 refreshSession(session);
107 session.lastActivityAt = Date.now();
108}
109
110function clearIdleStopTimer (session)
111{
112 if (!session.idleTimer) return;
113 clearTimeout(session.idleTimer);
114 session.idleTimer = null;
115}
116
117function scheduleIdleStop (session)
118{
119 clearIdleStopTimer(session);
120 if (session.activeRequests > 0) return;
121
122 session.idleTimer = setTimeout(() =>
123 {
124 const activeSession = sessions.get(session.id);
125 if (!activeSession) return;
126 if (activeSession.activeRequests > 0) return;
127 if (Date.now() - activeSession.lastActivityAt < STREAM_IDLE_STOP_MS)
128 {
129 scheduleIdleStop(activeSession);
130 return;
131 }
132 destroySession(activeSession.id);
133 }, STREAM_IDLE_STOP_MS);
134}
135
136function startAssetRequest (session)
137{
138 session.activeRequests += 1;
139 touchSessionActivity(session);
140 clearIdleStopTimer(session);
141}
142
143function finishAssetRequest (session)
144{
145 if (session.activeRequests > 0) session.activeRequests -= 1;
146 touchSessionActivity(session);
147 if (session.activeRequests === 0) scheduleIdleStop(session);
148}
149
150function parseSegmentIndexFromAssetPath (assetPath)
151{
152 const baseName = path.basename(String(assetPath || ''));
153 const match = baseName.match(/^stream_\d+_segment_(\d+)\.ts$/i);
154 if (!match) return null;
155
156 const segmentIndex = Number.parseInt(match[1], 10);
157 return Number.isInteger(segmentIndex) ? segmentIndex : null;
158}
159
160function runSegmentPrune (session)
161{
162 if (session.segmentPruneRunning) return;
163 if (session.segmentPrunePendingCutoff <= session.segmentPrunedUntil) return;
164
165 const cutoff = session.segmentPrunePendingCutoff;
166 session.segmentPruneRunning = true;
167
168 fs.promises.readdir(session.outputDir)
169 .then((entries) =>
170 {
171 const removals = [];
172 for (const entryName of entries)
173 {
174 const match = String(entryName).match(/^stream_\d+_segment_(\d+)\.ts$/i);
175 if (!match) continue;
176 const segmentIndex = Number.parseInt(match[1], 10);
177 if (!Number.isInteger(segmentIndex)) continue;
178 if (segmentIndex > cutoff) continue;
179
180 removals.push(
181 fs.promises.unlink(path.join(session.outputDir, entryName)).catch(() =>
182 {
183 // Ignore races while ffmpeg rotates temp files and request handlers read segments.
184 })
185 );
186 }
187 return Promise.all(removals);
188 })
189 .then(() =>
190 {
191 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
192 })
193 .catch(() =>
194 {
195 // Best effort cleanup; keep streaming even if pruning fails.
196 })
197 .finally(() =>
198 {
199 session.segmentPruneRunning = false;
200 if (session.segmentPrunePendingCutoff > session.segmentPrunedUntil)
201 {
202 runSegmentPrune(session);
203 }
204 });
205}
206
207function scheduleSegmentPrune (session, segmentIndex)
208{
209 if (!Number.isInteger(segmentIndex) || segmentIndex < 0) return;
210
211 const pruneCutoff = segmentIndex - STREAM_SEGMENT_RETENTION_COUNT;
212 if (pruneCutoff <= session.segmentPrunedUntil) return;
213
214 session.segmentPrunePendingCutoff = Math.max(session.segmentPrunePendingCutoff, pruneCutoff);
215 runSegmentPrune(session);
216}
217
218function runCommand (command, args)
219{
220 return new Promise((resolve, reject) =>
221 {
222 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
223 let stdout = '';
224 let stderr = '';
225
226 child.stdout.on('data', (chunk) =>
227 {
228 stdout += chunk.toString();
229 });
230
231 child.stderr.on('data', (chunk) =>
232 {
233 stderr += chunk.toString();
234 });
235
236 child.on('error', (err) =>
237 {
238 if (err && err.code === 'ENOENT')
239 {
240 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
241 return;
242 }
243 reject(err);
244 });
245
246 child.on('close', (code) =>
247 {
248 if (code === 0)
249 {
250 resolve({ stdout, stderr });
251 return;
252 }
253 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
254 });
255 });
256}
257
258async function probeInput (sourcePath)
259{
260 const { stdout } = await runCommand(ffprobePath(), [
261 '-v', 'error',
262 '-print_format', 'json',
263 '-show_streams',
264 '-show_format',
265 sourcePath,
266 ]);
267
268 let payload;
269 try
270 {
271 payload = JSON.parse(stdout || '{}');
272 } catch
273 {
274 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
275 }
276
277 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
278 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
279 {
280 const codecType = String(stream.codec_type || '').toLowerCase();
281 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
282 ? typedIndexes[codecType]++
283 : null;
284 return {
285 ...stream,
286 codecType,
287 relativeIndex,
288 tags: stream.tags || {},
289 disposition: stream.disposition || {},
290 };
291 }) : [];
292
293 return {
294 raw: payload,
295 durationSeconds: inputDurationSeconds(payload, streams),
296 video: streams.filter((stream) => stream.codecType === 'video'),
297 audio: streams.filter((stream) => stream.codecType === 'audio'),
298 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
299 };
300}
301
302function parsePositiveNumber (value)
303{
304 const number = Number(value);
305 return Number.isFinite(number) && number > 0 ? number : null;
306}
307
308function parseDurationTagSeconds (value)
309{
310 const raw = String(value || '').trim();
311 if (!raw) return null;
312
313 const numeric = parsePositiveNumber(raw);
314 if (numeric) return numeric;
315
316 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
317 if (!match) return null;
318
319 const hours = Number(match[1]);
320 const minutes = Number(match[2]);
321 const seconds = Number(match[3]);
322 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
323 return hours * 3600 + minutes * 60 + seconds;
324}
325
326function inputDurationSeconds (payload, streams)
327{
328 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
329 if (formatDuration) return formatDuration;
330
331 for (const stream of streams)
332 {
333 const duration = parsePositiveNumber(stream.duration);
334 if (duration) return duration;
335 }
336
337 const formatTags = payload && payload.format && payload.format.tags;
338 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
339 if (formatTagDuration) return formatTagDuration;
340
341 for (const stream of streams)
342 {
343 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
344 if (tagDuration) return tagDuration;
345 }
346
347 return null;
348}
349
350function sanitiseTrackValue (value, fallback)
351{
352 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
353 return cleaned || fallback;
354}
355
356function trackLanguage (stream)
357{
358 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
359}
360
361function trackLabel (stream, fallback)
362{
363 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
364 if (title) return title;
365 const language = trackLanguage(stream);
366 if (language && language !== 'und') return `${fallback} (${language})`;
367 return fallback;
368}
369
370async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
371{
372 const extractedTracks = [];
373 let skippedCount = 0;
374 let trackNumber = 0;
375
376 for (const stream of subtitleStreams)
377 {
378 const fileName = `subtitle-${trackNumber}.vtt`;
379 const targetPath = path.join(outputDir, fileName);
380
381 try
382 {
383 await runCommand(ffmpegPath(), [
384 '-hide_banner',
385 '-loglevel', 'error',
386 '-nostdin',
387 '-y',
388 '-i', sourcePath,
389 '-map', `0:s:${stream.relativeIndex}`,
390 '-c:s', 'webvtt',
391 targetPath,
392 ]);
393 } catch
394 {
395 skippedCount += 1;
396 continue;
397 }
398
399 extractedTracks.push({
400 fileName,
401 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
402 language: trackLanguage(stream),
403 isDefault: !!stream.disposition.default,
404 });
405 trackNumber += 1;
406 }
407
408 return {
409 tracks: extractedTracks,
410 skippedCount,
411 };
412}
413
414function buildHlsArgs (sourcePath, outputDir, streamInfo)
415{
416 const args = [
417 '-hide_banner',
418 '-loglevel', 'error',
419 '-nostdin',
420 '-y',
421 '-i', sourcePath,
422 '-map', '0:v:0',
423 ];
424
425 for (const audioStream of streamInfo.audio)
426 {
427 args.push('-map', `0:a:${audioStream.relativeIndex}`);
428 }
429
430 args.push(
431 '-c:v', 'libx264',
432 '-preset', 'veryfast',
433 '-crf', '23',
434 '-pix_fmt', 'yuv420p',
435 '-g', '48',
436 '-sc_threshold', '0',
437 );
438
439 if (streamInfo.audio.length)
440 {
441 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
442 } else
443 {
444 args.push('-an');
445 }
446
447 const variantMap = [];
448 if (streamInfo.audio.length)
449 {
450 variantMap.push('v:0,agroup:audio,name:video');
451 streamInfo.audio.forEach((audioStream, index) =>
452 {
453 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
454 if (audioStream.disposition.default) entry += ',default:yes';
455 variantMap.push(entry);
456 });
457 } else
458 {
459 variantMap.push('v:0,name:video');
460 }
461
462 args.push(
463 '-f', 'hls',
464 '-hls_time', '4',
465 '-hls_list_size', '0',
466 '-hls_flags', 'independent_segments+temp_file',
467 '-master_pl_name', 'master.m3u8',
468 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
469 '-var_stream_map', variantMap.join(' '),
470 path.join(outputDir, 'stream_%v.m3u8'),
471 );
472
473 return args;
474}
475
476function startHlsTranscode (session, streamInfo)
477{
478 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
479 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
480 let stderr = '';
481
482 child.stderr.on('data', (chunk) =>
483 {
484 stderr += chunk.toString();
485 if (stderr.length > 16000) stderr = stderr.slice(-16000);
486 });
487
488 child.on('error', (err) =>
489 {
490 if (err && err.code === 'ENOENT')
491 {
492 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
493 return;
494 }
495 session.error = err;
496 });
497
498 child.on('close', (code) =>
499 {
500 session.process = null;
501 if (code === 0 || session.error) return;
502 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
503 });
504
505 session.process = child;
506}
507
508function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
509{
510 const startedAt = Date.now();
511
512 return new Promise((resolve, reject) =>
513 {
514 const check = () =>
515 {
516 if (fs.existsSync(targetPath))
517 {
518 resolve();
519 return;
520 }
521
522 if (session.error)
523 {
524 reject(session.error);
525 return;
526 }
527
528 if (Date.now() - startedAt >= timeoutMs)
529 {
530 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
531 return;
532 }
533
534 setTimeout(check, 120);
535 };
536
537 check();
538 });
539}
540
541function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
542{
543 const startedAt = Date.now();
544
545 return new Promise((resolve, reject) =>
546 {
547 const check = () =>
548 {
549 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
550 {
551 resolve();
552 return;
553 }
554
555 if (session.error)
556 {
557 reject(session.error);
558 return;
559 }
560
561 if (!session.process)
562 {
563 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
564 return;
565 }
566
567 if (Date.now() - startedAt >= timeoutMs)
568 {
569 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
570 return;
571 }
572
573 setTimeout(check, 120);
574 };
575
576 check();
577 });
578}
579
580async function initialiseSession (session)
581{
582 const streamInfo = await probeInput(session.sourcePath);
583 if (!streamInfo.video.length)
584 {
585 throw makeHttpError(400, 'File does not contain a video stream.');
586 }
587
588 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
589 session.subtitleTracks = subtitleResult.tracks;
590 session.subtitleSummary = {
591 total: streamInfo.subtitles.length,
592 converted: subtitleResult.tracks.length,
593 skipped: subtitleResult.skippedCount,
594 };
595 startHlsTranscode(session, streamInfo);
596 session.durationSeconds = streamInfo.durationSeconds;
597 await waitForStreamStart(session);
598}
599
600async function createVideoStreamSession (sourcePath)
601{
602 pruneExpiredSessions();
603 ensureStreamRootDir();
604
605 const sessionId = crypto.randomBytes(18).toString('hex');
606 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
607 fs.mkdirSync(outputDir, { recursive: true });
608
609 const session = {
610 id: sessionId,
611 sourcePath,
612 outputDir,
613 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
614 process: null,
615 error: null,
616 subtitleTracks: [],
617 subtitleSummary: { total: 0, converted: 0, skipped: 0 },
618 readyPromise: null,
619 activeRequests: 0,
620 lastActivityAt: Date.now(),
621 idleTimer: null,
622 segmentPruneRunning: false,
623 segmentPrunePendingCutoff: -1,
624 segmentPrunedUntil: -1,
625 };
626
627 session.readyPromise = initialiseSession(session).catch((err) =>
628 {
629 session.error = err;
630 throw err;
631 });
632
633 sessions.set(sessionId, session);
634 await session.readyPromise;
635
636 return {
637 sessionId,
638 durationSeconds: session.durationSeconds,
639 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
640 subtitleSummary: {
641 total: Number(session.subtitleSummary.total || 0),
642 converted: Number(session.subtitleSummary.converted || 0),
643 skipped: Number(session.subtitleSummary.skipped || 0),
644 },
645 };
646}
647
648function contentTypeForAsset (assetPath)
649{
650 const ext = path.extname(assetPath).toLowerCase();
651 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
652 if (ext === '.ts') return 'video/mp2t';
653 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
654 if (ext === '.aac') return 'audio/aac';
655 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
656 return 'application/octet-stream';
657}
658
659async function sendVideoStreamAsset (sessionId, assetPath, req, res)
660{
661 pruneExpiredSessions();
662 const session = sessions.get(sessionId);
663 if (!session) throw makeHttpError(404, 'Video stream session not found.');
664
665 touchSessionActivity(session);
666 await session.readyPromise;
667
668 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
669 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
670 const resolvedRoot = path.resolve(session.outputDir);
671 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
672 {
673 throw makeHttpError(404, 'Stream asset not found.');
674 }
675
676 const segmentIndex = parseSegmentIndexFromAssetPath(relativeAssetPath);
677 if (segmentIndex !== null) scheduleSegmentPrune(session, segmentIndex);
678
679 await waitForFile(resolvedPath, session);
680 res.set('Cache-Control', 'private, no-store');
681 res.type(contentTypeForAsset(resolvedPath));
682
683 startAssetRequest(session);
684
685 let settled = false;
686 const finish = () =>
687 {
688 if (settled) return;
689 settled = true;
690 finishAssetRequest(session);
691 };
692
693 req.on('aborted', finish);
694 res.on('close', finish);
695 res.on('finish', finish);
696 res.on('error', finish);
697
698 await new Promise((resolve, reject) =>
699 {
700 res.sendFile(resolvedPath, (err) =>
701 {
702 finish();
703 if (!err)
704 {
705 resolve();
706 return;
707 }
708
709 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
710 {
711 resolve();
712 return;
713 }
714
715 reject(err);
716 });
717 });
718}
719
720function releaseVideoStreamSession (sessionId)
721{
722 pruneExpiredSessions();
723 return destroySession(String(sessionId || ''));
724}
725
726module.exports = {
727 STREAM_ROOT_DIR,
728 STREAM_SESSION_TTL_MS,
729 STREAM_IDLE_STOP_MS,
730 createVideoStreamSession,
731 sendVideoStreamAsset,
732 releaseVideoStreamSession,
733};