-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·123 lines (100 loc) · 3.12 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs');
const yargs = require('yargs');
const cliProgress = require('cli-progress');
const { parseFlipnote, FlipnoteConverter } = require('./index.js');
// split args
const [ bin, sourcePath, ...args ] = process.argv;
// everything after the -o flag goes to ffmpeg
const oFlag = args.indexOf('-o') + 1;
// get all args before the -o flag
const options = args.slice(0, oFlag === 0 ? args.length : oFlag);
// get all args after the -o flag
const outputOptions = args.slice(oFlag);
const cli = yargs
// input
.alias('i', 'input')
.describe('i', 'Flipnote filename')
.demandOption('i', 'Flipnote filename must be specified')
// probe
.describe('meta', 'Print Flipnote meta')
.boolean('meta')
// video filter
.array('vfilter')
.describe('vfilter', 'Add FFmpeg video filter')
// scale filter shorthand
.describe('scale', 'Scale output video')
// equalize filter
.describe('eq', 'Equalize audio')
.boolean('eq')
// output
// output
.alias('o', 'output')
.boolean('o')
.describe('o', 'Output options')
.epilogue('For more information see the manual at github.com/jaames/flipnote-video')
.help('help')
.parse(options);
if (!fs.existsSync(cli.input)) {
console.warn('Input file does not exist');
process.exit(1);
}
const inputFile = fs.readFileSync(cli.input);
parseFlipnote(inputFile.buffer).then(flipnote => {
const progressBar = new cliProgress.SingleBar({
clearOnComplete: true
});
if (flipnote === null) {
console.warn('Input file could not be parsed as a valid Flipnote .KWZ or .PPM file');
process.exit(1);
}
if (cli.meta) {
console.log('Showing metadata for', cli.input);
console.info(flipnote.meta);
}
if (!cli.output && !cli.meta) {
console.warn('Output options not specified');
process.exit(1);
}
if (cli.output) {
// returns a node-fluent-ffmpeg command object
// https://github.com/fluent-ffmpeg/node-fluent-ffmpeg
const converter = new FlipnoteConverter(flipnote, {
eq: cli.eq
});
if (cli.scale) {
converter.videoFilters(`scale=iw*${ cli.scale }:-1:flags=neighbor`);
}
if (cli.vfilter) {
cli.vfilter.forEach(filter => {
converter.videoFilters(filter);
});
}
// pass output options aside from the output filename to ffmpeg
converter.outputOptions.apply(converter, outputOptions.slice(0, outputOptions.length - 1));
// pass output filename
converter.output(outputOptions[outputOptions.length - 1]);
converter.on('start', () => {
progressBar.start(flipnote.frameCount, 0);
});
converter.on('progress', (progress) => {
progressBar.update(progress.frames);
});
converter.on('error', function(err, stdout, stderr) {
progressBar.stop();
console.log('Cannot convert Flipnote');
console.log(err.message);
process.exit(1);
});
converter.on('end', () => {
progressBar.stop();
});
process.on('SIGINT', function() {
progressBar.stop();
console.log('Flipnote conversion cancelled');
converter.kill('SIGSTOP');
process.exit();
});
converter.run();
}
});