-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (66 loc) · 2.77 KB
/
server.js
File metadata and controls
72 lines (66 loc) · 2.77 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
var http = require('http');
var url = require('url');
var multiparty = require('multiparty');
var t2map = require('through2-map');
var spawn = require('child_process').spawn;
var stream = null;
var ffmpeg = process.env.FFMPEG_BINARY || 'ffmpeg';
var extractAudio = (ffmpeg + " -i - -codec:a libmp3lame -qscale:a 2 -vn -f mp3 -").split(" ");
console.log("FFMpeg Binary: " + process.env.FFMPEG_BINARY);
var replaceExtension = function(filename, newExt) {
if(filename.indexOf(".") === -1) {
return filename + "." + newExt;
} else {
return filename.replace(/\.[^.]*$/, "." + newExt);
}
}
var server = http.createServer(function(req, res) {
if(url.parse(req.url).pathname == "/upload") {
stream = req;
res.writeHead(302, {
"Location": "/",
});
stream.on('end', function() {
res.end();
});
} else if(url.parse(req.url).pathname == "/download") {
setTimeout(function() {
if(!stream) {
setTimeout(arguments.callee, 200);
} else {
var form = new multiparty.Form();
form.on('part', function(part) {
if(part.filename !== 'null') {
outputFilename = replaceExtension(part.filename, 'mp3');
res.writeHead(200, {
'Content-Type': 'binary/octet-stream',
'Content-Disposition': 'attachment; filename="' + outputFilename + '"',
'Content-Transfer-Encoding': 'binary',
'Transfer-Encoding': 'chunked',
});
var ffmpeg = spawn(extractAudio[0], extractAudio.slice(1));
ffmpeg.stderr.pipe(process.stdout, { end: false });
part.pipe(ffmpeg.stdin);
ffmpeg.stdout.pipe(res);
part.on('error', function(err) { console.log(err); });
ffmpeg.stdout.on('error', function(err) { console.log(err); });
} else {
part.resume();
}
});
stream.on('error', function(err) { console.log(err); });
form.on('error', function(err) { console.log(err); });
form.parse(stream);
}
}, 50);
} else {
res.end([
"<!DOCTYPE html><head></head><body>",
"<form action='/upload' method='post' enctype='multipart/form-data'>",
"<input type='file' name='up' />",
"<input type='submit' value='OK' onclick='window.open(\"/download\", \"_blank\")'/>",
"</form></body>",
].join("\n"));
}
});
server.listen(process.env.PORT || 8000);