-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamProcess.js
69 lines (57 loc) · 1.76 KB
/
StreamProcess.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
69
import express from 'express'
import { spawn } from 'child_process'
export default class StreamProcess {
constructor(apikey, video, audio) {
this.server = express()
this.ffmpegCommand = [
'ffmpeg',
'-stream_loop', '-1',
'-re',
'-i', video,
'-stream_loop', '-1',
'-re',
'-i', audio,
'-vcodec', 'libx264',
'-pix_fmt', 'yuvj420p',
'-maxrate', '2048k',
'-preset', 'ultrafast',
'-r', '12',
'-framerate', '1',
'-g', '50',
'-crf', '51',
'-c:a', 'aac',
'-b:a', '128k',
'-ar', '44100',
'-strict', 'experimental',
'-video_track_timescale', '100',
'-b:v', '1500k',
'-f', 'flv',
`rtmp://a.rtmp.youtube.com/live2/${apikey}`,
];
this.child = spawn(this.ffmpegCommand[0], this.ffmpegCommand.slice(1));
this.child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
this.child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
this.child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
this.child.on('error', (err) => {
console.error(`Child process error: ${err}`);
});
this.server.use('/', (req, res) => {
res.send('Your Live Streaming Is All Ready Live')
})
this.server.listen(3000, () => {
console.log('live stream is ready')
})
}
logCommand() {
console.log(this.ffmpegCommand)
}
stopStream() {
this.child.kill()
}
}