Permalink
Browse files

First Commit

  • Loading branch information...
0 parents commit 92178fc01584ed8269883cee934038688efaca0b @zyklus zyklus committed Sep 20, 2011
Showing with 173 additions and 0 deletions.
  1. +9 −0 .gitmodules
  2. +161 −0 encode_folder
  3. +1 −0 node_modules/colors
  4. +1 −0 node_modules/commander
  5. +1 −0 node_modules/progress
@@ -0,0 +1,9 @@
+[submodule "node_modules/commander"]
+ path = node_modules/commander
+ url = https://github.com/visionmedia/commander.js.git
+[submodule "node_modules/progress"]
+ path = node_modules/progress
+ url = https://github.com/visionmedia/node-progress.git
+[submodule "node_modules/colors"]
+ path = node_modules/colors
+ url = https://github.com/Marak/colors.js.git
@@ -0,0 +1,161 @@
+#!/usr/bin/env node
+
+var fs = require( 'fs' )
+ , path = require( 'path' )
+ , spawn = require( 'child_process' ).spawn
+
+ , program = require( 'commander' )
+ , Progress = require( 'progress' )
+ , 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,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 = []
+ , folder, progressBar;
+
+
+program
+ .version( '0.1' )
+ .usage ( '[options] folder' )
+
+ .option ( '-R, --recursive' , 'Recursively scan directory' )
+ .option ( '-d, --delete' , 'Delete the original video on successful encoding' )
+ .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] - outputext)')
+ .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 );
+
+
+folder = program.args[0];
+
+if( !folder || ( program.args.length > 1 ) ){
+ console.log( program.helpInformation() );
+}else {
+ 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' ); }
+
+ var extRx = new RegExp( '\\.(' +
+ ( program.extensions || exts )
+ .replace( new RegExp( '(^' + program.outputext + ',|,' + program.outputext + '$)' ), '' )
+ .replace( ',' + program.outputext + ',', ',' )
+ .replace( /,/g, '|' )
+ + ')$' );
+
+ if( program.outputfolder ){
+ program.outputfolder = resolve( program.outputfolder );
+ }
+
+ scan( folder, program.recursive );
+ encode();
+}
+
+
+function die( msg ){
+ console.error( msg );
+ process.exit();
+}
+
+
+function resolve( folder ){
+ if( folder.indexOf( '/' ) ){
+ folder = path.join( __dirname, folder );
+ }
+
+ if( path.existsSync( folder ) ){
+ return folder;
+ }else{
+ die( 'Folder does not exist: '.red + folder.red.underline );
+ }
+
+ return folder;
+}
+
+
+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 );
+ }
+ }
+ }
+}
+
+
+function addFile( path ){
+ toEncode.push( path );
+ console.log( 'Found File: '.green + path.green.underline );
+}
+
+
+function encode(){
+ if( !toEncode.length ){
+ console.log( 'Done!'.green );
+ return;
+ }
+
+ progressBar = new Progress( ' [:bar] :percent :etas', {
+ complete : '='
+ , incomplete : ' '
+ , width : 50
+ , total : 100
+ });
+
+ new Encoder( toEncode.shift() );
+}
+
+
+function Encoder( fPath ){
+ var sPath = fPath.split( '/' )
+ , name = /^(.*\.)([^\.]+)$/.exec( sPath.pop() )
+ , base = name[ 1 ]
+ , ext = name[ 2 ]
+ , args = []
+ , done = 0;
+
+ sPath[0] = '/';
+
+ console.log( 'Encoding: '.yellow + fPath.yellow.underline );
+
+ args.push( '-Z', program.preset );
+ if( program.cpu ){ args.push( '-c', program.cpu ); }
+ args.push( '-i', fPath );
+ args.push( '-o', path.join.apply( path, [].concat( program.outputfolder || sPath ).concat( base + program.outputext ) ) );
+
+ encoder = spawn( program.handbrake, args );
+ encoder.stdout.on( 'data', function( data ){
+ var pDone = parseFloat( /([\.0-9]+) %/.exec( data.toString() ) ),
+ diff = pDone - done;
+
+ progressBar.tick( diff );
+ done = pDone;
+ });
+
+ encoder.on( 'exit', function( code ){
+ if( code === 0 ){
+ // Success!
+ progressBar.tick( 100 );
+ encode();
+
+ }else{
+ console.error( 'Unknown error encoding '.red + fPath.red.underline );
+ // Dunno
+ }
+ });
+}
Submodule colors added at 4a6371
Submodule commander added at 03f3b6
Submodule progress added at 172064

0 comments on commit 92178fc

Please sign in to comment.