-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
190 lines (168 loc) · 5.24 KB
/
index.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
import Promise from 'bluebird';
import path from 'path';
import _glob from 'glob';
let glob = Promise.promisify(_glob);
import {stat as _stat} from 'fs';
let stat = Promise.promisify(_stat);
import TranscodeError from './lib/transcode-error.js';
import VideoFile from './lib/video-file.js';
import defaultOptions from './lib/default-options.js';
let destExtensionRegex = /^\-{2}(mp4|m4v)$/i;
let dryRunRegex = /^\-\-dry\-run$/i;
function sumFileSizes(files, useProperty = 'currentPercent') {
return files.reduce(function (total, file) {
return total + (file[useProperty] * file.fileSize);
}, 0);
}
export default class BatchTranscodeVideo {
static get INACTIVE() { return 0; }
static get RUNNING() { return 1; }
static get FINISHED() { return 2; }
static get ERRORED() { return 3; }
/* This is the value to use before there is any data to calculate speed
* with estimateSpeed(). A value between 100 and 3000 seems reasonable.
*/
static get EST_MS_PER_MB() { return 1000.0; }
constructor(options, transcodeOptions) {
options['curDir'] = process.cwd();
options['input'] = path.relative(options['curDir'], options['input']);
options['dryRun'] = transcodeOptions.length ? transcodeOptions.reduce(function (prev, cur) {
return prev || dryRunRegex.test(cur.trim());
}, false) : false;
options['destExt'] = transcodeOptions.reduce(function (prev, cur) {
let curArg = cur.trim();
if (destExtensionRegex.test(curArg)) {
return curArg.match(destExtensionRegex)[1];
}
return prev;
}, 'mkv');
this.filePattern = path.normalize(options['input'] + path.sep + options['mask']);
this.options = Object.assign({}, defaultOptions, options);
this.transcodeOptions = transcodeOptions.slice(0);
this.status = BatchTranscodeVideo.INACTIVE;
this.files = [];
this.currentIndex = 0;
this.error = null;
this._ready = this.createEntries();
return this;
}
createEntries() {
return glob(this.filePattern, {})
.then((files) => {
if (files.length === 0) {
throw new TranscodeError('No files found for search pattern provided.', this.filePattern);
}
return files;
}, (err) => {
throw new TranscodeError('File system error encountered while scanning for media.', this.filePattern, err.message);
})
.map((file) => this.resolvePath(file), {
concurrency: 3
})
.then((files) => {
this.files = files;
return this.files;
});
}
transcodeAll() {
return this._ready
.then(() => {
if (!this.isReady) {
throw new TranscodeError('Batch has already been processed.', this.filePattern);
}
this.startTime = Date.now();
this.lastTime = this.startTime;
this.status = BatchTranscodeVideo.RUNNING;
return this.files;
})
.mapSeries((video, index) => {
this.lastTime = Date.now();
this.currentIndex = index;
return video.transcode();
})
.then(() => {
this.lastTime = Date.now();
this.stopTime = this.lastTime;
this.status = BatchTranscodeVideo.FINISHED;
this.currentIndex = -1;
let errored = this.files.reduce((t, file) => t + (file.isErrored ? 1 : 0), 0);
if (errored > 0) {
this.status = BatchTranscodeVideo.ERRORED;
}
})
.catch((err) => {
this.lastTime = Date.now();
this.stopTime = this.lastTime;
this.status = BatchTranscodeVideo.ERRORED;
this.error = err;
throw err;
});
}
resolvePath(filePath) {
return stat(filePath)
.then((stats) => {
return new VideoFile(filePath, stats, this.options, this.transcodeOptions, () => this.estimateSpeed());
});
}
estimateSpeed() {
let processed = sumFileSizes(this.files.slice(0, this.currentIndex), 'lastPercent');
if (processed > 0) {
// ms/MB
return (this.lastTime - this.startTime) / processed;
}
return BatchTranscodeVideo.EST_MS_PER_MB;
}
get processedFileSizes() {
return sumFileSizes(this.files);
}
get totalFileSizes() {
return this.files
.reduce(function (total, file) {
let useSize = file.fileSize;
if (file.isErrored) {
useSize *= file.currentPercent;
} else if (file.isSkipped) {
useSize = 0;
}
return total + useSize;
}, 0);
}
get currentPercent() {
return this.processedFileSizes / this.totalFileSizes;
}
get currentTime() {
return (this.isRunning ? Date.now() : this.stopTime) - this.startTime;
}
get totalTime() {
return this.isRunning ?
(this.currentTime / this.currentPercent) :
((this.stopTime || Date.now()) - this.startTime);
}
get remainingTime() {
return Math.max(this.totalTime - this.currentTime, 0);
}
get isReady() {
return this.status === BatchTranscodeVideo.INACTIVE;
}
get isRunning() {
return this.status === BatchTranscodeVideo.RUNNING;
}
get isDone() {
return this.isFinished || this.isErrored;
}
get isFinished() {
return this.status === BatchTranscodeVideo.FINISHED;
}
get isErrored() {
return this.status === BatchTranscodeVideo.ERRORED;
}
get ready() {
return this._ready;
}
get currentFile() {
if (this.currentIndex >= 0) {
return this.files[this.currentIndex];
}
return null;
}
};