-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvideo-processor-cmds.js
executable file
·318 lines (264 loc) · 8.82 KB
/
video-processor-cmds.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const shell = require('shelljs');
const ipfsAPI = require('ipfs-http-client');
const fs = require('fs');
const hbjs = require('handbrake-js')
var ipfsIp = process.env.IPFSIP || '127.0.0.1';
var ipfsPort = process.env.IPFSPORT || '5001';
var ipfsProtocol = process.env.IPFSPROTOCOL || 'http';
var cmds = {
ffprobe_cmds: {
createCmdString: (filePath) => {
str1 = "ffprobe -v error -of default=nw=1 -show_entries stream_tags=rotate:format=size,duration:stream=index,codec_name,pix_fmt,height,width,duration,nb_frames,avg_frame_rate,bit_rate "
str2 = " -print_format json"
return str1+filePath+str2
}
},
ipfs_cmds: {
// uploads file to ipfs, second parameter is the property to update within encoder response
ipfsUpload: (filePath, justHash, prop) => {
//Connceting to our http api
const ipfs = ipfsAPI(ipfsIp, ipfsPort, {protocol: ipfsProtocol})
//let videoFile = fs.readFileSync(filePath);
//let testBuffer = new Buffer.from(videoFile);
ipfs.add(videoFile, {"only-hash": justHash}, function (err, file) {
if (err) {
console.log(err);
process.exit();
}
// updating relevant encoder response fields
cmds.setObjPropToValue(cmds.encoderResponse, prop+".progress", "100.00%");
cmds.setObjPropToValue(cmds.encoderResponse, prop+".lastTimeProgress", Date());
cmds.setObjPropToValue(cmds.encoderResponse, prop+".step", "success");
cmds.setObjPropToValue(cmds.encoderResponse, prop+".hash", file[0].hash);
cmds.setObjPropToValue(cmds.encoderResponse, prop+".fileSize", file[0].size);
});
}
},
sprite_cmds: {
sprite: (filePath, vidLength, resDir) => {
var splitCmd = cmds.sprite_cmds.createVideoSplitCmd(filePath, vidLength, resDir);
var montCmd = cmds.sprite_cmds.createMontageCmd(resDir);
cmds.sprite_cmds.createSprite(splitCmd, montCmd);
},
// splits video into images
createVideoSplitCmd: (filePath, vidLength, resDir) => {
// if (vidLength > 600) {
// var frameRate = 100/vidLength;
// } else {
// var frameRate = 1;
// }
let frameRate = 100/vidLength;
return `ffmpeg -y -i `+filePath+ ` -r `+frameRate+` -vf scale=128:72 -f image2 `+resDir+`/img%03d`
},
// concatenates all the images together
createMontageCmd: (resDir) => {
return `montage -mode concatenate -tile 1x `+resDir+`/* `+resDir+`/sprite.png`
},
createSprite: (splitCmd, montCmd) => {
var timeoutfunc = setTimeout(()=>{
console.log("Sprite makin timed out")
console.log("Killing container")
process.exit();
}, 600000);
shell.exec(splitCmd,function(code, stdout, stderr) {
// code isn't 0 if error occurs
if (code) {
console.log(stderr);
process.exit();
} else {
shell.exec(montCmd, function(code, stdout, stderr){
// code isn't 0 if error occurs
if (code) {
console.log(stderr);
process.exit();
} else {
clearTimeout(timeoutfunc)
console.log("sprite completed")
//if no errors, update relevant encoder response fields and upload to ipfs
cmds.encoderResponse.sprite.spriteCreation.progress = "100.00%";
cmds.encoderResponse.sprite.spriteCreation.lastTimeProgress = Date();
cmds.encoderResponse.sprite.spriteCreation.step = "Success";
cmds.ipfs_cmds.ipfsUpload("./sprite/sprite.png", true, 'sprite.ipfsAddSprite');
return stdout;
}
});
}
});
}
},
encoder_cmds: {
encoderSettings: {
input: '',
output: '',
maxWidth: 0,
maxHeight: 0,
optimize: true,
format: "av_mp4",
encoder: "x264",
rate: "30"
},
changeSettings: (filePath, resName, maxWidth, maxHeight) => {
let settings = cmds.encoder_cmds.encoderSettings;
settings.input = filePath;
settings.output = resName;
settings.maxWidth = maxWidth;
settings.maxHeight = maxHeight;
return settings
},
encode: (settings, encodedVideoIndex, cb) => {
var timeoutfunc = setTimeout(()=>{
console.log("Encoder timed out")
console.log("Killing container")
process.exit();
},1800000);
var noop = function(){};
cb = cb || noop;
let propIpfs = 'encodedVideos[' + String(encodedVideoIndex) + '].ipfsAddEncodeVideo';
var outputName = settings.output;
hbjs.spawn(settings)
.on('error', err => {
// console.log(err);
cmds.encoderResponse.encodedVideos[encodedVideoIndex].encode.errorMessage = err;
console.log("Exiting process, encoding error");
process.exit();
})
.on('progress', progress => {
console.log("Encoding progress, video index: "+ encodedVideoIndex)
cmds.encoderResponse.encodedVideos[encodedVideoIndex].encode.progress = String(progress.percentComplete)+"%";
cmds.encoderResponse.encodedVideos[encodedVideoIndex].encode.lastTimeProgress = Date();
})
.on('complete', () => {
console.log("Encoding completed, video index: " + encodedVideoIndex)
clearTimeout(timeoutfunc)
// when complete, upload to ipfs
cmds.ipfs_cmds.ipfsUpload(outputName, true, propIpfs);
cb();
});
}
},
encoderResponse: {
finished: false,
debugInfo: null,
sourceAudioCpuEncoding: null,
sourceVideoGpuEncoding: null,
ipfsAddSourceVideo: {
progress: null,
encodeSize: "source",
lastTimeProgress: null,
errorMessage: null,
step: "Init",
positionInQueue: null,
hash: null,
fileSize: null
},
sprite: {
spriteCreation: {
progress: null,
encodeSize: "source",
lastTimeProgress: null,
errorMessage: null,
step: "Init",
positionInQueue: null
},
ipfsAddSprite: {
progress: null,
encodeSize: "source",
lastTimeProgress: null,
errorMessage: null,
step: "Init",
positionInQueue: null,
hash: null,
fileSize: null
}
},
encodedVideos: []
},
// adds encoded video data fields to encoder response
addEncodedVideoData: (encodeSize) => {
var num = encodeSize.length;
for (let i = 0; i < num; i++) {
cmds.encoderResponse.encodedVideos.push({
encode: {
progress: "Waiting in queue...",
encodeSize: "",
lastTimeProgress: null,
errorMessage: null,
step: "Waiting",
positionInQueue: null
},
ipfsAddEncodeVideo: {
progress: null,
encodeSize: "",
lastTimeProgress: null,
errorMessage: null,
step: "init",
positionInQueue: null,
hash: null,
fileSize: null
}
});
cmds.encoderResponse.encodedVideos[i].encode.encodeSize = encodeSize[i];
cmds.encoderResponse.encodedVideos[i].ipfsAddEncodeVideo.encodeSize = encodeSize[i];
}
},
// function for setting deep nested object property values
setObjPropToValue: (obj, path, value) => {
var i;
path = path.split(/(?:\.|\[|\])+/);
for (i = 0; i < path.length - 1; i++)
obj = obj[path[i]];
obj[path[i]] = value;
},
moveFiles: (filePath, numOfEncodedVids) => {
var oldEncodedVidPaths = ["fileres240.mp4", "fileres480.mp4"];
var is = fs.createReadStream(filePath);
var os = fs.createWriteStream("./longtermstore/" + cmds.encoderResponse.ipfsAddSourceVideo.hash);
is.pipe(os);
is.on('end',function() {
fs.unlinkSync(filePath);
});
for(let i=0; i<numOfEncodedVids; i++){
is = fs.createReadStream(oldEncodedVidPaths[i]);
os = fs.createWriteStream("./longtermstore/" + cmds.encoderResponse.encodedVideos[i].ipfsAddEncodeVideo.hash);
is.pipe(os);
is.on('end',function() {
fs.unlinkSync(oldEncodedVidPaths[i]);
});
}
is = fs.createReadStream("./sprite/sprite.png");
os = fs.createWriteStream("./longtermstore/" + cmds.encoderResponse.sprite.ipfsAddSprite.hash);
is.pipe(os);
is.on('end',function() {
fs.unlinkSync("./sprite/sprite.png");
});
},
// checking encoder response values to ensure everything is done before setting finished to true
checkIfFinished: (filePath) => {
var numOfEncodedVids = cmds.encoderResponse.encodedVideos.length
console.log("We should have encoded: "+numOfEncodedVids+" videos");
var func = setInterval(()=>{
// creating an array of encoded video hashes to iterate through in the "if"
var encodedVidsHash = [];
for (let i = 0; i < numOfEncodedVids; i++)
{
encodedVidsHash.push(cmds.encoderResponse.encodedVideos[i].ipfsAddEncodeVideo.hash);
}
if (cmds.encoderResponse.ipfsAddSourceVideo.hash && cmds.encoderResponse.sprite.ipfsAddSprite.hash && encodedVidsHash.every((hash) => {return hash})){
clearInterval(func);
console.log("Moving files to long term storage")
console.log(cmds.encoderResponse)
cmds.moveFiles(filePath, numOfEncodedVids)
// wait before setting finished to true and ending process
setTimeout(()=>{
cmds.encoderResponse.finished = true;
console.log("Encoder finished")
}, 1000);
setTimeout(()=>{
console.log("Killing container")
process.exit();
},10000);
}
},2000);
},
}
module.exports = cmds