forked from nwronski/batch-transcode-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-cli.js
142 lines (132 loc) · 3.69 KB
/
index-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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Promise from 'bluebird';
import BatchTranscodeVideo from './index.js';
import Progress from './lib/progress.js';
import help from './lib/help.js';
import VideoFile from './lib/video-file.js';
import _charm from 'charm';
import ChildPromise from './lib/child-promise.js';
export default class CliBatchTranscodeVideo extends BatchTranscodeVideo {
static get INTERVAL_MS() { return 1000; }
static get FIRST_TAB() { return 10; }
constructor(options, transcodeOptions) {
super(options, transcodeOptions);
this.timer = null;
this.files = [];
this.charm = _charm(process);
if (this.options['help'] === true) {
help(this.charm);
process.exit(0);
}
this.progress = new Progress(this.charm, CliBatchTranscodeVideo.FIRST_TAB);
ChildPromise.debug = this.options['debug'];
return this;
}
cli() {
process.on('uncaughtException', (err) => {
this.error = err;
this.onError(err);
});
process.on('exit', () => {
// Handle SIGINT
if (!this.isDone) {
this.status = BatchTranscodeVideo.ERRORED;
}
if (this.files && this.files.length) {
for (let file of this.files) {
file.kill();
}
}
this.finish();
});
if (this.options['quiet'] !== true) {
this.progress.start();
}
if (this.options['debug'] !== true && this.options['quiet'] !== true) {
this.progress.write(this.state());
this.timer = setInterval(() => {
let state = this.state();
this.progress.clear();
this.progress.write(state);
}, CliBatchTranscodeVideo.INTERVAL_MS);
}
return this.transcodeAll()
.then(res => this.onSuccess(res))
.catch(err => this.onError(err));
}
finish() {
if (this.timer !== null) {
clearInterval(this.timer);
this.timer = null;
}
if (this.options['quiet'] !== true) {
// this.progress.clear();
this.progress.finish();
this.progress.summary(this.finalState());
}
if (!this.isSuccess) {
process.reallyExit(1);
}
}
finalState() {
let processed = this.processedFileSizes;
let total = this.totalFileSizes;
let seconds = this.totalTime / 1000.0;
let speed = processed / seconds;
let workCount = this.files.reduce((t, file) => {
return t + file.currentPercent;
}, 0);
let average = workCount > 0 ? (this.totalTime / workCount) : 0;
return {
files: this.files,
processed: processed.toFixed(2),
total: total.toFixed(2),
speed: speed.toFixed(2),
success: this.isFinished,
error: this.error,
elapsed: this.totalTime,
average
};
}
state() {
try {
let processed = this.processedFileSizes;
let remaining = this.totalFileSizes.toFixed(2);
return {
current: {
file: this.currentFile.fileName,
percent: this.currentFile.currentPercent,
elapsed: this.currentFile.currentTime,
remaining: this.currentFile.remainingTime
},
total: {
percent: this.currentPercent,
elapsed: this.currentTime,
remaining: this.remainingTime,
processed: `${processed > 0 ? processed.toFixed(2) : '(estimating)'} MB of ${remaining} MB`,
files: this.files
}
};
} catch (e) {
return {
current: {
file: `[Scanning ${this.files.length} files...]`,
percent: 0,
elapsed: 0,
remaining: 0
},
total: {
percent: 0,
elapsed: 0,
remaining: 0,
status: '[Calculating remaining time...]'
}
};
}
}
onSuccess(result) {
process.exit(0);
}
onError(err) {
process.exit(1);
}
};