-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.js
145 lines (120 loc) · 3.8 KB
/
record.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const { PassThrough } = require('stream')
const fs = require('fs')
const { RTCAudioSink, RTCVideoSink } = require('wrtc').nonstandard;
const ffmpegPath = require("ffmpeg-static");
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
const { StreamInput } = require('fluent-ffmpeg-multistream')
const VIDEO_OUTPUT_SIZE = '320x240'
const VIDEO_OUTPUT_FILE = './recording.mp4'
let UID = 0;
function beforeOffer(peerConnection) {
const audioTransceiver = peerConnection.addTransceiver('audio');
const videoTransceiver = peerConnection.addTransceiver('video');
const audioSink = new RTCAudioSink(audioTransceiver.receiver.track);
const videoSink = new RTCVideoSink(videoTransceiver.receiver.track);
const streams = [];
videoSink.addEventListener('frame', ({ frame: { width, height, data }}) => {
const size = width + 'x' + height;
if (!streams[0] || (streams[0] && streams[0].size !== size)) {
UID++;
const stream = {
recordPath: './recording-' + size + '-' + UID + '.mp4',
size,
video: new PassThrough(),
audio: new PassThrough()
};
const onAudioData = ({ samples: { buffer } }) => {
if (!stream.end) {
stream.audio.push(Buffer.from(buffer));
}
};
audioSink.addEventListener('data', onAudioData);
stream.audio.on('end', () => {
audioSink.removeEventListener('data', onAudioData);
});
streams.unshift(stream);
streams.forEach(item=>{
if (item !== stream && !item.end) {
item.end = true;
if (item.audio) {
item.audio.end();
}
item.video.end();
}
})
stream.proc = ffmpeg()
.addInput((new StreamInput(stream.video)).url)
.addInputOptions([
'-f', 'rawvideo',
'-pix_fmt', 'yuv420p',
'-s', stream.size,
'-r', '30',
])
.addInput((new StreamInput(stream.audio)).url)
.addInputOptions([
'-f s16le',
'-ar 48k',
'-ac 1',
])
.on('start', ()=>{
console.log('Start recording >> ', stream.recordPath)
})
.on('end', ()=>{
stream.recordEnd = true;
console.log('Stop recording >> ', stream.recordPath)
})
.size(VIDEO_OUTPUT_SIZE)
.output(stream.recordPath);
// .audioCodec('aac')
// .flvmeta()
// .format('flv')
// .output(stream.recordPath); // RTMP endpoint
stream.proc.run();
}
streams[0].video.push(Buffer.from(data));
});
const { close } = peerConnection;
peerConnection.close = function() {
audioSink.stop();
videoSink.stop();
streams.forEach(({ audio, video, end, proc, recordPath })=>{
if (!end) {
if (audio) {
audio.end();
}
video.end();
}
});
let totalEnd = 0;
const timer = setInterval(()=>{
streams.forEach(stream=>{
if (stream.recordEnd) {
totalEnd++;
if (totalEnd === streams.length) {
clearTimeout(timer);
const mergeProc = ffmpeg()
.on('start', ()=>{
console.log('Start merging into ' + VIDEO_OUTPUT_FILE);
})
.on('end', ()=>{
streams.forEach(({ recordPath })=>{
fs.unlinkSync(recordPath);
})
console.log('Merge end. You can play ' + VIDEO_OUTPUT_FILE);
});
streams.forEach(({ recordPath })=>{
mergeProc.addInput(recordPath)
});
mergeProc
.output(VIDEO_OUTPUT_FILE)
.run();
}
}
});
}, 1000)
return close.apply(this, arguments);
}
}
module.exports = { beforeOffer };