BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

fix subs maybe
timfa Tim Falken · pushed by timfa · 6 months ago
1a987d8b History
video-stream.js 11.7 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 sessions = new Map();
13
14function hasInitialHlsAssets (outputDir)
15{
16 try
17 {
18 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
19 return entries.some((entry) =>
20 {
21 if (!entry.isFile()) return false;
22 if (entry.name === 'master.m3u8') return true;
23 if (/^stream_\d+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
24 if (/^stream_\d+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
25 return false;
26 });
27 } catch
28 {
29 return false;
30 }
31}
32
33function makeHttpError (status, message)
34{
35 const err = new Error(message);
36 err.status = status;
37 return err;
38}
39
40function ensureStreamRootDir ()
41{
42 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
43}
44
45function ffmpegPath ()
46{
47 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
48}
49
50function ffprobePath ()
51{
52 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
53}
54
55function pruneExpiredSessions ()
56{
57 const now = Date.now();
58 for (const [sessionId, session] of sessions)
59 {
60 if (session.expiresAt > now) continue;
61 try
62 {
63 if (session.process) session.process.kill('SIGTERM');
64 } catch
65 {
66 // Ignore process cleanup failures.
67 }
68 sessions.delete(sessionId);
69 fs.rmSync(session.outputDir, { recursive: true, force: true });
70 }
71}
72
73function refreshSession (session)
74{
75 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
76}
77
78function runCommand (command, args)
79{
80 return new Promise((resolve, reject) =>
81 {
82 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
83 let stdout = '';
84 let stderr = '';
85
86 child.stdout.on('data', (chunk) =>
87 {
88 stdout += chunk.toString();
89 });
90
91 child.stderr.on('data', (chunk) =>
92 {
93 stderr += chunk.toString();
94 });
95
96 child.on('error', (err) =>
97 {
98 if (err && err.code === 'ENOENT')
99 {
100 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
101 return;
102 }
103 reject(err);
104 });
105
106 child.on('close', (code) =>
107 {
108 if (code === 0)
109 {
110 resolve({ stdout, stderr });
111 return;
112 }
113 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
114 });
115 });
116}
117
118async function probeInput (sourcePath)
119{
120 const { stdout } = await runCommand(ffprobePath(), [
121 '-v', 'error',
122 '-print_format', 'json',
123 '-show_streams',
124 '-show_format',
125 sourcePath,
126 ]);
127
128 let payload;
129 try
130 {
131 payload = JSON.parse(stdout || '{}');
132 } catch
133 {
134 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
135 }
136
137 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
138 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
139 {
140 const codecType = String(stream.codec_type || '').toLowerCase();
141 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
142 ? typedIndexes[codecType]++
143 : null;
144 return {
145 ...stream,
146 codecType,
147 relativeIndex,
148 tags: stream.tags || {},
149 disposition: stream.disposition || {},
150 };
151 }) : [];
152
153 return {
154 raw: payload,
155 video: streams.filter((stream) => stream.codecType === 'video'),
156 audio: streams.filter((stream) => stream.codecType === 'audio'),
157 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
158 };
159}
160
161function sanitiseTrackValue (value, fallback)
162{
163 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
164 return cleaned || fallback;
165}
166
167function trackLanguage (stream)
168{
169 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
170}
171
172function trackLabel (stream, fallback)
173{
174 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
175 if (title) return title;
176 const language = trackLanguage(stream);
177 if (language && language !== 'und') return `${fallback} (${language})`;
178 return fallback;
179}
180
181async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
182{
183 const extractedTracks = [];
184 let skippedCount = 0;
185 let trackNumber = 0;
186
187 for (const stream of subtitleStreams)
188 {
189 const fileName = `subtitle-${trackNumber}.vtt`;
190 const targetPath = path.join(outputDir, fileName);
191
192 try
193 {
194 await runCommand(ffmpegPath(), [
195 '-hide_banner',
196 '-loglevel', 'error',
197 '-nostdin',
198 '-y',
199 '-i', sourcePath,
200 '-map', `0:s:${stream.relativeIndex}`,
201 '-c:s', 'webvtt',
202 targetPath,
203 ]);
204 } catch
205 {
206 skippedCount += 1;
207 continue;
208 }
209
210 extractedTracks.push({
211 fileName,
212 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
213 language: trackLanguage(stream),
214 isDefault: !!stream.disposition.default,
215 });
216 trackNumber += 1;
217 }
218
219 return {
220 tracks: extractedTracks,
221 skippedCount,
222 };
223}
224
225function buildHlsArgs (sourcePath, outputDir, streamInfo)
226{
227 const args = [
228 '-hide_banner',
229 '-loglevel', 'error',
230 '-nostdin',
231 '-y',
232 '-i', sourcePath,
233 '-map', '0:v:0',
234 ];
235
236 for (const audioStream of streamInfo.audio)
237 {
238 args.push('-map', `0:a:${audioStream.relativeIndex}`);
239 }
240
241 args.push(
242 '-c:v', 'libx264',
243 '-preset', 'veryfast',
244 '-crf', '23',
245 '-pix_fmt', 'yuv420p',
246 '-g', '48',
247 '-sc_threshold', '0',
248 );
249
250 if (streamInfo.audio.length)
251 {
252 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
253 } else
254 {
255 args.push('-an');
256 }
257
258 const variantMap = [];
259 if (streamInfo.audio.length)
260 {
261 variantMap.push('v:0,agroup:audio,name:video');
262 streamInfo.audio.forEach((audioStream, index) =>
263 {
264 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
265 if (audioStream.disposition.default) entry += ',default:yes';
266 variantMap.push(entry);
267 });
268 } else
269 {
270 variantMap.push('v:0,name:video');
271 }
272
273 args.push(
274 '-f', 'hls',
275 '-hls_time', '4',
276 '-hls_list_size', '0',
277 '-hls_flags', 'independent_segments+temp_file',
278 '-master_pl_name', 'master.m3u8',
279 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
280 '-var_stream_map', variantMap.join(' '),
281 path.join(outputDir, 'stream_%v.m3u8'),
282 );
283
284 return args;
285}
286
287function startHlsTranscode (session, streamInfo)
288{
289 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
290 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
291 let stderr = '';
292
293 child.stderr.on('data', (chunk) =>
294 {
295 stderr += chunk.toString();
296 if (stderr.length > 16000) stderr = stderr.slice(-16000);
297 });
298
299 child.on('error', (err) =>
300 {
301 if (err && err.code === 'ENOENT')
302 {
303 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
304 return;
305 }
306 session.error = err;
307 });
308
309 child.on('close', (code) =>
310 {
311 session.process = null;
312 if (code === 0 || session.error) return;
313 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
314 });
315
316 session.process = child;
317}
318
319function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
320{
321 const startedAt = Date.now();
322
323 return new Promise((resolve, reject) =>
324 {
325 const check = () =>
326 {
327 if (fs.existsSync(targetPath))
328 {
329 resolve();
330 return;
331 }
332
333 if (session.error)
334 {
335 reject(session.error);
336 return;
337 }
338
339 if (Date.now() - startedAt >= timeoutMs)
340 {
341 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
342 return;
343 }
344
345 setTimeout(check, 120);
346 };
347
348 check();
349 });
350}
351
352function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
353{
354 const startedAt = Date.now();
355
356 return new Promise((resolve, reject) =>
357 {
358 const check = () =>
359 {
360 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
361 {
362 resolve();
363 return;
364 }
365
366 if (session.error)
367 {
368 reject(session.error);
369 return;
370 }
371
372 if (!session.process)
373 {
374 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
375 return;
376 }
377
378 if (Date.now() - startedAt >= timeoutMs)
379 {
380 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
381 return;
382 }
383
384 setTimeout(check, 120);
385 };
386
387 check();
388 });
389}
390
391async function initialiseSession (session)
392{
393 const streamInfo = await probeInput(session.sourcePath);
394 if (!streamInfo.video.length)
395 {
396 throw makeHttpError(400, 'File does not contain a video stream.');
397 }
398
399 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
400 session.subtitleTracks = subtitleResult.tracks;
401 session.subtitleSummary = {
402 total: streamInfo.subtitles.length,
403 converted: subtitleResult.tracks.length,
404 skipped: subtitleResult.skippedCount,
405 };
406 startHlsTranscode(session, streamInfo);
407 await waitForStreamStart(session);
408}
409
410async function createVideoStreamSession (sourcePath)
411{
412 pruneExpiredSessions();
413 ensureStreamRootDir();
414
415 const sessionId = crypto.randomBytes(18).toString('hex');
416 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
417 fs.mkdirSync(outputDir, { recursive: true });
418
419 const session = {
420 id: sessionId,
421 sourcePath,
422 outputDir,
423 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
424 process: null,
425 error: null,
426 subtitleTracks: [],
427 subtitleSummary: { total: 0, converted: 0, skipped: 0 },
428 readyPromise: null,
429 };
430
431 session.readyPromise = initialiseSession(session).catch((err) =>
432 {
433 session.error = err;
434 throw err;
435 });
436
437 sessions.set(sessionId, session);
438 await session.readyPromise;
439
440 return {
441 sessionId,
442 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
443 subtitleSummary: {
444 total: Number(session.subtitleSummary.total || 0),
445 converted: Number(session.subtitleSummary.converted || 0),
446 skipped: Number(session.subtitleSummary.skipped || 0),
447 },
448 };
449}
450
451function contentTypeForAsset (assetPath)
452{
453 const ext = path.extname(assetPath).toLowerCase();
454 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
455 if (ext === '.ts') return 'video/mp2t';
456 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
457 if (ext === '.aac') return 'audio/aac';
458 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
459 return 'application/octet-stream';
460}
461
462async function sendVideoStreamAsset (sessionId, assetPath, res)
463{
464 pruneExpiredSessions();
465 const session = sessions.get(sessionId);
466 if (!session) throw makeHttpError(404, 'Video stream session not found.');
467
468 refreshSession(session);
469 await session.readyPromise;
470
471 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
472 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
473 const resolvedRoot = path.resolve(session.outputDir);
474 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
475 {
476 throw makeHttpError(404, 'Stream asset not found.');
477 }
478
479 await waitForFile(resolvedPath, session);
480 res.set('Cache-Control', 'private, no-store');
481 res.type(contentTypeForAsset(resolvedPath));
482 res.sendFile(resolvedPath);
483}
484
485module.exports = {
486 STREAM_ROOT_DIR,
487 STREAM_SESSION_TTL_MS,
488 createVideoStreamSession,
489 sendVideoStreamAsset,
490};