-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscoding-server.js
96 lines (77 loc) · 2.61 KB
/
transcoding-server.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
var Transcoder = require('stream-transcoder');
var http = require('http');
var rar = require('./utils/rar-utils.js');
var internalIp = require('internal-ip');
var stream = require('stream');
var DelayedStream = require('delayed-stream');
var fs = require('fs');
var videoSupport = require('./utils/video-support');
var url = require('url');
function startTranscodingServer(path, port) {
return http.createServer(function(req, res) {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*'
});
console.log('http request');
// requestedPath = url.parse(req.url, true).pathname.substring(1);
var requestedPath = url.parse(req.url, true).query.path;
console.log(requestedPath);
path = requestedPath;
// get rarStream, analyse with ffprobe
// get rarStream again, set ffmpeg flags based on analysis from ffprobe
function openVideoStream() {
if (path.substring(path.length-4)=='.rar') {
return rar.openRarStream(path);
} else {
return fs.createReadStream(path);
}
}
videoSupport( openVideoStream(), function(support) {
console.log('got support ' + JSON.stringify(support) );
var videoStream = openVideoStream();
var trans = new Transcoder( videoStream )
.custom('strict', 'experimental')
.format('matroska')
// .format('mp4')
// .custom('ss', '00:20:00')
.on('finish', function() {
console.log('finished transcoding');
})
.on('error', function(err) {
console.log('transcoding error: %o', err);
})
.on('progress', function(progress) {
console.log('Progress: ' + progress.progress);
});
if (support.supportsAudio()) {
trans.audioCodec('copy');
} else {
trans.audioCodec('aac')
.custom('ac', '2');
}
if (support.supportsVideo()) {
trans.videoCodec('copy');
} else {
trans.videoCodec('libx264')
.custom('movflags', 'frag_keyframe');
}
var args = trans._compileArguments();
args = [ '-i', '-' ].concat(args);
args.push('pipe:1');
console.log('spawning ffmpeg ', args.join(' '));
var transStream = trans.stream();
transStream.pipe(res);
videoStream.on('end', function() { console.log('video stream ended'); });
req.on('close', stopStream);
req.on('end', stopStream);
function stopStream() {
console.log('Connection closed');
transStream.unpipe();
transStream.destroy();
}
});
}).listen(port);
}
module.exports = {
start : startTranscodingServer
};