-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (143 loc) · 5.65 KB
/
main.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
'use strict';
const nmmes = require('nmmes-backend');
const onDeath = require('death');
const Path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs-extra');
module.exports = class Sample extends nmmes.Module {
constructor(args, logger) {
super(require('./package.json'), logger);
this.options = Object.assign(nmmes.Module.defaults(Sample), args);
this.options.length = this.options.length / 1000; // Convert milliseconds to seconds
}
async executable(map) {
let _self = this;
let options = this.options;
let video = this.video;
this.removeDeathListener = onDeath(function(signal, err) {
if (_self.encoder) {
_self.logger.trace('Signal receieved:', signal, err);
_self.encoder.kill(signal);
}
});
if (options.screenshots) {
this.logger.debug('Generating screenshots...');
let encoder = this.encoder = ffmpeg(video.output.path);
await (new Promise((resolve, reject) => {
encoder
.on('filenames', function(filenames) {
_self.logger.trace("Generating screenshots:", filenames);
})
.on('error', function(error, stdout, stderr) {
_self.removeDeathListener();
reject(error);
})
.on('end', function(stdout, stderr) {
_self.removeDeathListener();
resolve();
})
.screenshots({
count: options.screenshots,
filename: `%b-%0i[%r].${options['screenshot-format']}`,
folder: Path.resolve(video.output.dir, 'screenshots')
});
}));
}
if (options.video) {
let encoder = this.encoder = ffmpeg(video.output.path).outputOptions('-c', 'copy').outputOptions('-map', '0');
const seek = Sample.calculateSeek(options.seek, options.length, video),
output = Sample.generateOutputPath(options.output, video.output.path, options.suffix);
encoder.seekInput(Math.min(video.output.metadata.format.duration - seek, Math.max(0, seek)));
encoder.duration(options.length);
encoder.output(output);
return new Promise(function(resolve, reject) {
fs.ensureDir(Path.dirname(props.output), err => {
if (err)
return reject(err);
_self.encoder
.on('start', function(commandLine) {
_self.logger.trace('[FFMPEG] Query:', commandLine);
})
.on('error', function(error, stdout, stderr) {
_self.removeDeathListener();
reject(error);
})
.on('end', function(stdout, stderr) {
_self.removeDeathListener();
resolve();
}).run();
});
});
}
return {};
};
static options() {
return {
'length': {
default: 30000,
describe: 'Milliseconds to encode in preview mode. Max is half the length of input video.',
type: 'number',
group: 'General:'
},
'seek': {
default: 'middle',
describe: 'Milliseconds/percent to start preview at. Middle will take a preview of the middle of the video.',
type: 'string',
group: 'Advanced:'
},
'video': {
default: true,
describe: 'Create a sample of the finished encode.',
type: 'boolean',
group: 'General:'
},
'screenshots': {
default: 0,
describe: 'Create a number of screenshots of the finished encode. Set to 0 to disable.',
type: 'number',
group: 'General:'
},
'screenshot-format': {
default: 'jpeg',
describe: 'Image format of produced screenshots',
type: 'string',
choices: ['jpeg', 'png', 'bmp'],
group: 'Advanced:'
},
'suffix': {
default: '-sample',
describe: 'Sample output file suffix.',
type: 'string',
group: 'Advanced:'
},
};
}
// TODO: Make these 2 functions non static
static generateOutputPath(output, videoPath, suffix = "-sample") {
let parsed = Path.parse(videoPath);
if (!output) {
output = Path.format({
name: parsed.name + suffix,
ext: parsed.ext,
dir: parsed.dir
});
}
// TODO: windows support (D:\ or C:\)
if (!output.startsWith(Path.sep)) {
output = Path.resolve(parsed.dir, output);
}
return output;
}
static calculateSeek(requested, length, video) {
let duration = video.output.metadata.format.duration;
if (isNaN(requested)) {
if (requested.endsWith('%')) {
return duration * parseInt(requested, 10) / 100;
} else if (requested === 'middle') {
return (duration / 2) - (length / 2);
}
} else {
return parseInt(requested) / 1000;
}
}
}