forked from C9E4F7/DTube-video-processor
-
Notifications
You must be signed in to change notification settings - Fork 6
/
video-processor.js
executable file
·159 lines (124 loc) · 5.14 KB
/
video-processor.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const http = require('http');
const shell = require('shelljs');
const uuidv4 = require('uuid/v4');
const formidable = require('formidable');
var cmds = require('./video-processor-cmds.js');
var config = require('./config.js')
// variable to assure only one upload request happens
var reqhappened = false;
// generated token
const genToken = uuidv4();
http.createServer(function (req, res) {
res.setHeader('access-control-allow-headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range');
res.setHeader('access-control-allow-origin', config.cors);
res.setHeader('access-control-allow-credentials', 'true');
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('access-control-max-age', '600');
if (req.url == '/getStatus') {
console.log("getStatus Hit")
res.statusCode = 200;
if (!reqhappened)
res.end('{"version":"'+config.version+'","currentWaitingInQueue":{"audioCpuToEncode":0,"videoGpuToEncode":0,"audioVideoCpuToEncode":0,"spriteToCreate":0,"ipfsToAdd":0}}');
else
res.end('{"version":"'+config.version+'","currentWaitingInQueue":{"audioCpuToEncode":0,"videoGpuToEncode":0,"audioVideoCpuToEncode":99,"spriteToCreate":0,"ipfsToAdd":99}}');
}
// sending encoder progress to user
if (req.url.match(/\/getProgressByToken.*/)) {
console.log("getProgressByToken Hit")
res.end(JSON.stringify(cmds.encoderResponse));
}
// file upload
if (req.url == '/uploadVideo?videoEncodingFormats=240p,480p,720p,1080p&sprite=true' && !reqhappened) {
if (req.method === 'OPTIONS') {
console.log("uploadVideo Options Hit")
res.statusCode = 204;
res.end();
} else {
var fileUploadLimit = setTimeout(() => {
console.log("Upload timed out")
console.log("Killing container")
process.exit();
}, 3600000);
res.statusCode = 200;
console.log("uploadVideo Non-Options Hit")
var form = new formidable.IncomingForm();
//Sane Form options
form.maxFields = 1;
form.encoding = 'utf-8';
form.maxFileSize = '4096000000';
form.parse(req, function (err, fields, files) {
});
// file is moved to upload folder and renamed to uuid
form.on('fileBegin', function (name, file) {
reqhappened = true;
console.log("File upload Began")
file.path = "./upload/" + genToken;
});
form.on('file', function (name, file) {
console.log("File upload Finished, setting reqhappened")
clearTimeout(fileUploadLimit)
//frontend needs to know if upload was successful and receive the token
var successResponse = { success: "", token: "" };
// check if file is valid
shell.exec(cmds.ffprobe_cmds.createCmdString(file.path), function (code, stdout, stderr) {
// code isn't 0 if error occurs
if (code) {
// if error, success is false, no token, end process
console.log(stderr);
console.log("Bad File, killing.")
successResponse.success = "false";
res.end(JSON.stringify(successResponse));
process.exit();
}
var fileData = JSON.parse(stdout);
// if file is valid, success is true and provide token
successResponse.success = "true";
successResponse.token = genToken;
console.log(successResponse + " sent to user")
res.end(JSON.stringify(successResponse));
var videoHeight = null
for (let i = 0; i < fileData.streams.length; i++) {
if (fileData.streams[i].height) {
videoHeight = fileData.streams[i].height;
break
}
}
var fileDuration = fileData.format.duration;
//create sprite and upload it to ipfs (ipfs upload function is called within sprite function)
console.log("Making the sprite")
cmds.sprite_cmds.sprite(file.path, fileDuration, "./sprite");
// find ipfs hash of source file
cmds.ipfs_cmds.ipfsUpload(file.path, "ipfsAddSourceVideo", true);
// videos under 240 res or longer than 10 minutes not encoded
if (videoHeight <= 240 || fileDuration > config.maxEncodedDuration) {
console.log("No encoding")
} else if (videoHeight > 240 && videoHeight <= 480) {
console.log("Encoding 240p")
// adds data about encoded video to encoder response
cmds.addEncodedVideoData(["240p"]);
cmds.encoder_cmds.encode(cmds.encoder_cmds.changeSettings(file.path, "fileres240.mp4", 426, 240), 0);
// videos over 480 res get encoded to 240 and 480 res
} else if (videoHeight > 480) {
cmds.encoderResponse.sourceStored = false
console.log("encoding 240p and 480p")
cmds.addEncodedVideoData(["240p", "480p"]);
cmds.encoder_cmds.encode(cmds.encoder_cmds.changeSettings(file.path, "fileres240.mp4", 426, 240), 0, function () {
cmds.encoder_cmds.encode(cmds.encoder_cmds.changeSettings(file.path, "fileres480.mp4", 854, 480), 1);
});
}
// checks if all procedures are done so that finish status can be set
cmds.checkIfFinished(file.path);
});
});
form.on('error', function (err) {
console.error('Error', err);
process.exit();
});
}
} else {
res.statusCode = 503;
res.end("There's nothing here for you");
}
}).listen(config.port, () => {
console.log("listening on port "+config.port);
});