-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (56 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import http from 'http';
import path from 'path';
import { spawn } from 'child_process';
import express from 'express';
import { Server as SocketIO } from 'socket.io';
const app = express();
const server = http.createServer(app);
const io = new SocketIO(server);
let ffmpegProcess;
app.use(express.static(path.resolve('./public')));
io.on('connection', (socket) => {
console.log('Socket Connected', socket.id);
socket.on('start-stream', (rtmpUrl) => {
if (ffmpegProcess) {
ffmpegProcess.kill('SIGINT');
}
const options = [
'-i',
'-',
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-tune', 'zerolatency',
'-r', `${25}`,
'-g', `${25 * 2}`,
'-keyint_min', 25,
'-crf', '25',
'-pix_fmt', 'yuv420p',
'-sc_threshold', '0',
'-profile:v', 'main',
'-level', '3.1',
'-c:a', 'aac',
'-b:a', '128k',
'-ar', 128000 / 4,
'-f', 'flv',
rtmpUrl,
];
ffmpegProcess = spawn('ffmpeg', options);
ffmpegProcess.stdout.on('data', (data) => {
socket.emit('ffmpeg-data', data.toString());
});
ffmpegProcess.stderr.on('data', (data) => {
socket.emit('ffmpeg-error', data.toString());
});
ffmpegProcess.on('close', (code) => {
socket.emit('ffmpeg-exit', code);
console.log(`ffmpeg process exited with code ${code}`);
});
socket.on('binarystream', (stream) => {
console.log('Binary Stream Incoming...');
ffmpegProcess.stdin.write(stream, (err) => {
if (err) console.log('Error writing stream', err);
});
});
});
});
server.listen(3000, () => console.log(`HTTP Server is running on PORT 3000`));