-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (100 loc) · 3.75 KB
/
Copy pathserver.js
File metadata and controls
124 lines (100 loc) · 3.75 KB
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
// Last time updated at July 07, 2014, 19:21:23
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Experiments - github.com/muaz-khan/WebRTC-Experiment
// RecordRTC - github.com/muaz-khan/WebRTC-Experiment/tree/master/RecordRTC
// RecordRTC over Socket.io - github.com/muaz-khan/WebRTC-Experiment/tree/master/RecordRTC/RecordRTC-over-Socketio
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
uuid = require('node-uuid'),
port = process.argv[2] || 8888;
var app = http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
fs.exists(filename, function (exists) {
if (!exists) {
response.writeHead(404, {
"Content-Type": "text/plain"
});
response.write('404 Not Found: ' + filename + '\n');
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, 'binary', function (err, file) {
if (err) {
response.writeHead(500, {
"Content-Type": "text/plain"
});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
});
});
}).listen(parseInt(port, 10));
var sys = require('sys'),
path = require('path'),
exec = require('child_process').exec;
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
var fileName = uuid.v4();
socket.emit('ffmpeg-output', 0);
writeToDisk(data.audio.dataURL, fileName + '.wav');
// if it is chrome
if (data.video) {
writeToDisk(data.video.dataURL, fileName + '.webm');
merge(socket, fileName);
}
// if it is firefox or if user is recording only audio
else socket.emit('merged', fileName + '.wav');
});
});
// isn't it redundant?
// app.listen(8888);
function writeToDisk(dataURL, fileName) {
var fileExtension = fileName.split('.').pop(),
fileRootNameWithBase = './uploads/' + fileName,
filePath = fileRootNameWithBase,
fileID = 2,
fileBuffer;
// @todo return the new filename to client
while (fs.existsSync(filePath)) {
filePath = fileRootNameWithBase + '(' + fileID + ').' + fileExtension;
fileID += 1;
}
dataURL = dataURL.split(',').pop();
fileBuffer = new Buffer(dataURL, 'base64');
fs.writeFileSync(filePath, fileBuffer);
console.log('filePath', filePath);
}
function merge(socket, fileName) {
var FFmpeg = require('fluent-ffmpeg');
var audioFile = path.join(__dirname, 'uploads', fileName + '.wav'),
videoFile = path.join(__dirname, 'uploads', fileName + '.webm'),
mergedFile = path.join(__dirname, 'uploads', fileName + '-merged.webm');
new FFmpeg({
source: videoFile
})
.addInput(audioFile)
.on('error', function (err) {
socket.emit('ffmpeg-error', 'ffmpeg : An error occurred: ' + err.message);
})
.on('progress', function (progress) {
socket.emit('ffmpeg-output', Math.round(progress.percent));
})
.on('end', function () {
socket.emit('merged', fileName + '-merged.webm');
console.log('Merging finished !');
// removing audio/video files
fs.unlink(audioFile);
fs.unlink(videoFile);
})
.saveToFile(mergedFile);
}