-
Notifications
You must be signed in to change notification settings - Fork 108
/
app.js
89 lines (79 loc) · 2.58 KB
/
app.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
var fs = require('fs'),
util = require('util'),
mime = require('mime'),
path = require('path'),
ffmpeg = require('fluent-ffmpeg'),
stream = require('stream'),
mkdirp = require('mkdirp');
/*
* Possible argument positions
*
* converter pathVideo(string) pathVideoFrame(string) startVideoTime(number) endVideoTime(number)
* converter pathVideo(string) pathVideoFrame(string) startVideoTime(number)
* converter pathVideo(string) pathVideoFrame(string)
*
*/
if(process.argv.length > 3) {
var pathVideo = process.argv[2],
pathVideoFrame = process.argv[3],
startVideoTime = parseInt(process.argv[4]),
endVideoTime = parseInt(process.argv[5]),
tempDirName = path.resolve('/tmp/', '.images');
var json = {};
Promise.resolve().then(function(){
createOrCleanTempDir(tempDirName);
}).
then(function(){
return createVideoFrames(pathVideo, startVideoTime, endVideoTime, tempDirName);
}).
then(function(){
json.frames = generateStringVideoFrames(tempDirName);
saveVideoFrame(pathVideoFrame, JSON.stringify(json));
}).
catch(function(err){
console.log('Error -', err);
})
} else {
console.error('Not enough parameters supplied!');
}
function createOrCleanTempDir(tempDirName){
mkdirp(tempDirName, function(err){
fs.readdirSync(tempDirName).map(function(elem, index){
fs.unlinkSync(path.resolve(tempDirName, elem));
});
})
}
function createVideoFrames(_path, startTime, endTime, tempDirName){
return promise = new Promise(function(resolve, reject){
var command = ffmpeg(path.resolve(_path))
.noAudio()
.on('end', function(){
resolve();
})
.on('error', function(err){
reject(err);
})
if (startTime){
command = command.seekInput(startTime);
}
if (endTime){
if (startTime > endTime){
reject('Start time must be lower than end time')
}
command = command.duration(endTime - startTime);
}
command.save(path.resolve(tempDirName, 'image-%07d.jpg'));
});
}
function generateStringVideoFrames(tempDirName){
return fs.readdirSync(tempDirName).sort().map(function(elem, index){
return base64Img(path.resolve(tempDirName, elem));
})
}
function base64Img(src) {
var data = fs.readFileSync(src).toString('base64');
return util.format('data:%s;base64,%s', mime.lookup(src), data);
}
function saveVideoFrame(_path, data){
fs.writeFileSync(_path, data);
}