BlueGit

timfa filemanager

Files

filemanager / tests / video-stream.test.js

video-stream.test.js

fix subs maybe
timfa Tim Falken · pushed by timfa · 6 months ago
1a987d8b History
video-stream.test.js 8.0 KiB Download Raw
1'use strict';
2
3const { EventEmitter } = require('events');
4const fs = require('fs');
5const os = require('os');
6const path = require('path');
7
8jest.mock('child_process', () => ({
9 spawn: jest.fn(),
10}));
11
12function makeChildProcess ()
13{
14 const child = new EventEmitter();
15 child.stdout = new EventEmitter();
16 child.stderr = new EventEmitter();
17 child.kill = jest.fn();
18 return child;
19}
20
21describe('video stream bootstrap', () =>
22{
23 let mockSpawn;
24 let sourceFile;
25
26 beforeEach(() =>
27 {
28 jest.resetModules();
29 mockSpawn = require('child_process').spawn;
30 mockSpawn.mockReset();
31
32 const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-video-stream-test-'));
33 sourceFile = path.join(tmpDir, 'clip.mkv');
34 fs.writeFileSync(sourceFile, 'video');
35 });
36
37 test('createVideoStreamSession resolves once ffmpeg starts writing stream assets', async () =>
38 {
39 mockSpawn.mockImplementation((command, args) =>
40 {
41 const child = makeChildProcess();
42 const exeName = path.basename(String(command || '')).toLowerCase();
43
44 if (exeName.includes('ffprobe'))
45 {
46 process.nextTick(() =>
47 {
48 child.stdout.emit('data', Buffer.from(JSON.stringify({
49 streams: [
50 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
51 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
52 ],
53 })));
54 child.emit('close', 0);
55 });
56 return child;
57 }
58
59 expect(exeName.includes('ffmpeg')).toBe(true);
60
61 const outputPattern = String(args[args.length - 1] || '');
62 const outputDir = path.dirname(outputPattern);
63
64 process.nextTick(() =>
65 {
66 fs.mkdirSync(outputDir, { recursive: true });
67 setTimeout(() =>
68 {
69 fs.writeFileSync(path.join(outputDir, 'stream_0.m3u8'), '#EXTM3U\n');
70 }, 40);
71 });
72
73 return child;
74 });
75
76 const { createVideoStreamSession } = require('../server/video-stream');
77 const session = await createVideoStreamSession(sourceFile);
78
79 expect(session).toEqual({
80 sessionId: expect.any(String),
81 subtitleTracks: [],
82 subtitleSummary: {
83 total: 0,
84 converted: 0,
85 skipped: 0,
86 },
87 });
88 });
89
90 test('createVideoStreamSession configures flat segment filenames that match playlist URLs', async () =>
91 {
92 mockSpawn.mockImplementation((command, args) =>
93 {
94 const child = makeChildProcess();
95 const exeName = path.basename(String(command || '')).toLowerCase();
96
97 if (exeName.includes('ffprobe'))
98 {
99 process.nextTick(() =>
100 {
101 child.stdout.emit('data', Buffer.from(JSON.stringify({
102 streams: [
103 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
104 { codec_type: 'audio', codec_name: 'aac', tags: { handler_name: 'SoundHandler' }, disposition: { default: 1 } },
105 ],
106 })));
107 child.emit('close', 0);
108 });
109 return child;
110 }
111
112 const segmentArgIndex = args.indexOf('-hls_segment_filename');
113 expect(segmentArgIndex).toBeGreaterThan(-1);
114 expect(String(args[segmentArgIndex + 1])).toContain('stream_%v_segment_%06d.ts');
115
116 const playlistPath = String(args[args.length - 1] || '');
117 const outputDir = path.dirname(playlistPath);
118 process.nextTick(() =>
119 {
120 fs.mkdirSync(outputDir, { recursive: true });
121 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
122 });
123
124 return child;
125 });
126
127 const { createVideoStreamSession } = require('../server/video-stream');
128 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
129 sessionId: expect.any(String),
130 subtitleTracks: [],
131 subtitleSummary: {
132 total: 0,
133 converted: 0,
134 skipped: 0,
135 },
136 });
137 });
138
139 test('createVideoStreamSession does not become ready from subtitle files alone', async () =>
140 {
141 mockSpawn.mockImplementation((command, args) =>
142 {
143 const child = makeChildProcess();
144 const exeName = path.basename(String(command || '')).toLowerCase();
145
146 if (exeName.includes('ffprobe'))
147 {
148 process.nextTick(() =>
149 {
150 child.stdout.emit('data', Buffer.from(JSON.stringify({
151 streams: [
152 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
153 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
154 { codec_type: 'subtitle', codec_name: 'subrip', tags: { language: 'eng' }, disposition: {} },
155 ],
156 })));
157 child.emit('close', 0);
158 });
159 return child;
160 }
161
162 const mapIndex = args.indexOf('-map');
163 const mappedValue = mapIndex > -1 ? String(args[mapIndex + 1] || '') : '';
164
165 if (mappedValue.startsWith('0:s:'))
166 {
167 const subtitleTarget = String(args[args.length - 1] || '');
168 process.nextTick(() =>
169 {
170 fs.mkdirSync(path.dirname(subtitleTarget), { recursive: true });
171 fs.writeFileSync(subtitleTarget, 'WEBVTT\n\n');
172 child.emit('close', 0);
173 });
174 return child;
175 }
176
177 const playlistPath = String(args[args.length - 1] || '');
178 const outputDir = path.dirname(playlistPath);
179 process.nextTick(() =>
180 {
181 fs.mkdirSync(outputDir, { recursive: true });
182 setTimeout(() =>
183 {
184 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
185 }, 80);
186 });
187
188 return child;
189 });
190
191 const { createVideoStreamSession } = require('../server/video-stream');
192 const startedAt = Date.now();
193 const session = await createVideoStreamSession(sourceFile);
194 const elapsedMs = Date.now() - startedAt;
195
196 expect(elapsedMs).toBeGreaterThanOrEqual(60);
197 expect(session).toEqual({
198 sessionId: expect.any(String),
199 subtitleTracks: [
200 expect.objectContaining({
201 fileName: 'subtitle-0.vtt',
202 language: 'eng',
203 }),
204 ],
205 subtitleSummary: {
206 total: 1,
207 converted: 1,
208 skipped: 0,
209 },
210 });
211 });
212
213 test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () =>
214 {
215 mockSpawn.mockImplementation((command, args) =>
216 {
217 const child = makeChildProcess();
218 const exeName = path.basename(String(command || '')).toLowerCase();
219
220 if (exeName.includes('ffprobe'))
221 {
222 process.nextTick(() =>
223 {
224 child.stdout.emit('data', Buffer.from(JSON.stringify({
225 streams: [
226 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
227 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
228 { codec_type: 'subtitle', codec_name: 'hdmv_pgs_subtitle', tags: { language: 'eng' }, disposition: {} },
229 ],
230 })));
231 child.emit('close', 0);
232 });
233 return child;
234 }
235
236 const mapIndex = args.indexOf('-map');
237 const mappedValue = mapIndex > -1 ? String(args[mapIndex + 1] || '') : '';
238
239 // Fail subtitle extraction but keep HLS transcoding path successful.
240 if (mappedValue.startsWith('0:s:'))
241 {
242 process.nextTick(() =>
243 {
244 child.stderr.emit('data', Buffer.from('Subtitle codec not supported'));
245 child.emit('close', 1);
246 });
247 return child;
248 }
249
250 const playlistPath = String(args[args.length - 1] || '');
251 const outputDir = path.dirname(playlistPath);
252 process.nextTick(() =>
253 {
254 fs.mkdirSync(outputDir, { recursive: true });
255 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
256 });
257
258 return child;
259 });
260
261 const { createVideoStreamSession } = require('../server/video-stream');
262 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
263 sessionId: expect.any(String),
264 subtitleTracks: [],
265 subtitleSummary: {
266 total: 1,
267 converted: 0,
268 skipped: 1,
269 },
270 });
271 });
272});