-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_folder
executable file
·305 lines (241 loc) · 7.56 KB
/
encode_folder
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env node
var fs = require( 'fs' )
, path = require( 'path' )
, child = require( 'child_process' )
, spawn = child.spawn
, program = require( 'commander' )
, charm = require( 'charm' )( process )
, colors = require( 'colors/colors' )
, exts = '3g2,3gp,aepx,ale,asf,asx,avi,avp,avs,bdm,bik,bsf,camproj,cpi,divx,dmsm,dream,dvdmedia,dvr-ms,dzm,dzp,edl,f4v,fbr,fcproject,flv,hdmov,imovieproj,m2p,m2ts,m4v,mkv,mod,moi,mov,mp4,mpeg,mpg,mts,mxf,ogv,pds,prproj,psh,r3d,rcproject,rm,rmvb,scm,smil,sqz,srt,stx,swf,swi,tix,trp,ts,veg,vf,vob,vro,webm,wlmp,wmv,wtv,xvid,yuv'
, toEncode = []
, chrOk = String.fromCharCode( 10003 )
, chrFail = String.fromCharCode( 10007 )
, folder, progressBar, extRx, activeEncoder;
/**
* Allow app to exit!
**/
charm.on( '^C', process.exit );
/**
* Program Options
**/
program
.version( '0.1' )
.usage ( '[options] folder' )
.option ( '-R, --recursive' , 'Recursively scan directory' )
.option ( '-d, --delete' , 'Delete the original video on successful encoding' )
.option ( '-f, --force' , 'Force over-write of existing files' )
.option ( '-k, --keep' , 'Keep partially encoded files from encoding failures' )
.option ( '-w, --watch' , 'Watch the folder indefinitely for new video files' )
.option ( '-Z, --preset <name>' , 'Handbrake video preset (default: Normal)' )
.option ( '-H, --handbrake <path>' , 'Path to handbrake-cli (default: /Applications/HandBrakeCLI)' )
.option ( '-c, --cpu <count>' , 'Set CPU count (default: autodetected)', parseInt )
.option ( '-x, --extensions <extensions>', 'Comma-separated list of file extensions to process (default: [long list])')
.option ( '-X, --outputext <ext>' , 'Extension for generated files (default: m4v)' )
.option ( '-O, --outputfolder <folder>' , 'Folder in which to place completed videos (default: same-as-original)' )
.parse ( process.argv );
/**
* Process arguments and start folder scan
**/
process.nextTick( function(){
folder = resolve( folder );
program.outputext || ( program.outputext = 'm4v' );
program.preset || ( program.preset = 'Normal' );
program.handbrake || ( program.handbrake = '/Applications/HandBrakeCLI' );
if( !path.existsSync( program.handbrake ) ){ die( 'HandBrakeCLI not found' ); }
extRx = new RegExp( '\\.(' +
( program.extensions || exts )
.replace( /,/g, '|' )
+ ')$' );
if( program.outputfolder ){
program.outputfolder = resolve( program.outputfolder );
}
scan( folder, program.recursive );
encode();
});
folder = program.args[0];
/**
* Display help when invalid arguments are passed in
**/
if( !folder || ( program.args.length > 1 ) ){
console.error( program.helpInformation() );
die();
}
/**
* Watch for process exit
**/
process.on( 'exit', function(){
activeEncoder && activeEncoder.abandon();
});
/**
* Kill app with an optional death message
**/
function die( msg ){
msg && console.error( msg.red );
process.exit();
}
/**
* Resolve a folder or die
**/
function resolve( folder ){
if( folder.indexOf( '/' ) ){
folder = path.join( process.cwd(), folder );
}
if( path.existsSync( folder ) ){
return folder;
}else{
die( 'Folder does not exist: '.red + folder.red.underline );
}
return folder;
}
/**
* Scan folder seeking out any video files
**/
function scan( folder, recursive ){
var files = fs.readdirSync( folder );
for( var i=0, l=files.length; i<l; i++ ){
var fPath = path.join( folder, files[i] );
if( fs.statSync( fPath ).isDirectory() ){
if( recursive ){
scan( fPath, true );
}
} else {
if( extRx.test( fPath ) ){
addFile( fPath );
}
}
}
if( program.watch ){
setTimeout( function(){
scan( folder, recursive );
encode();
}, 3E5 ); // re-scan every 5 minutes
}
}
/**
* Add video file to queue
**/
function addFile( path ){
toEncode.push( path );
console.log( 'Found File: '.green + path.green.underline );
}
/**
* Encode next file in queue
**/
function encode(){
// wait your turn!
if( activeEncoder && activeEncoder.running ){ return ; }
if( !toEncode.length ){
return;
}
activeEncoder = new Encoder( toEncode.shift() );
}
/**
* Core Prototypes
**/
String.prototype.times = function( n ){
return new Array( n+1 ).join( this );
};
Number.prototype.toPercent = function(){
return (
+ ( 0|this )
+ '.'
+ ((( 0|this%1*100 ) || 0 ) + 100 ).toString().substr(1)
+ '%'
);
};
Number.prototype.toTimeString = function(){
var s = 0|this/1000
, m = 0|s/60%60
, h = 0|s/3600
, s = s%60;
return (
h ? h + ':' + ( 100 + m ).toString().substr(1) + ':' + ( 100 + s ).toString().substr(1) :
m ? m + ':' + ( 100 + s ).toString().substr(1) :
s + 's'
);
};
/**
* File encoder
**/
function Encoder( fPath ){
this.startTime = Date.now();
this.inPath = fPath;
this.inFile = fPath.split( /(\/|\\)/ ).pop();
this.inFolder = fPath.replace( /[\/\\][^\/\\]+$/, '' );
this.outPath = path.join( program.outputfolder || this.inFolder, this.inFile.replace( /\.[^\.]+$/, '' ) + '.' + program.outputext );
charm.write( 'Encoding: ' + this.inFile + ' ' );
if( this.inPath === this.outPath ){
return this.abandon( 'Source & Destination are the same' );
}
if( !program.force && path.existsSync( this.outPath ) ){
return this.abandon( 'Destination file already exists' );
}
this.drawProgress();
var args = [];
if( program.cpu ){ args.push( '-c', program.cpu ); }
args.push( '-Z', program.preset );
args.push( '-i', this.inPath );
args.push( '-o', this.outPath );
this.started = true;
this.encoder = spawn( program.handbrake, args );
this.encoder.stdout.on( 'data', this.onChildData.bind( this ) );
this.encoder .on( 'exit', this.onChildExit.bind( this ) );
}
Encoder.prototype = {
drawProgress : function( p ){
p || ( p = 0 );
var str = ' [' + '#'.times( 0|p/5 ) + ' '.times( 0|(104.99-p)/5 ) + '] ' + p.toPercent() + ' ETA: ' + ( ( Date.now() - this.startTime ) * ( 100 / p - 1 ) ).toTimeString();
charm.write( str );
this.charmLen = str.length;
return this;
}
, clearProgress : function(){
charm.left( this.charmLen || 0 ).erase( 'end' );
return this;
}
, onChildData : function( data ){
var pDone = parseFloat( /([\.0-9]+) %/.exec( data.toString() ) );
this.clearProgress().drawProgress( pDone );
}
, onChildExit : function( code ) {
if( code === 0 ){
// Success!
this.clearProgress().removeInfile().success();
process.nextTick( encode );
}else{
this.abandon( 'Unknown error encoding ' );
// Dunno
}
this.exit();
}
, exit : function(){
console.log( '' );
return this;
}
, fail : function( msg ){
charm.foreground( 'red' ).write( ' ' + chrFail + ' ' + msg ).foreground( 'white' );
return this;
}
, success : function(){
charm.foreground( 'green' ).write( ' ' + chrOk + ' Success!' ).foreground( 'white' );
return this;
}
, abandon : function( msg ){
if( this.abandoned ){ return this; }
this.clearProgress();
this.encoder && this.encoder.kill();
this.removeOutfile();
// start next encoder -- nextTick in case abandon is called due to process.exit
process.nextTick( encode );
this.abandoned = true;
return this.fail( msg ).exit();
}
, removeOutfile : function(){
!program.keep && this.started && path.existsSync( this.outPath ) && fs.unlinkSync( this.outPath );
return this;
}
, removeInfile : function(){
program.delete && path.existsSync( this.inPath ) && fs.unlinkSync( this.inPath );
return this;
}
};