#!/usr/bin/perl # Finds all VIDEO_TS folders inside input folder and individually # process all titles found. Please note: this script may run for # weeks if you have a slow computer and/or a lot of VIDEO_TS # folders to process. # # David Colbert # Sun Apr 8 18:06:57 EDT 2007 use strict; use File::Find (); # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; # Declare subroutine for File::Find sub wanted; # # Process input args # my $narg = scalar( @ARGV ); my $compressargs = ""; my $allargs = ""; my $infolder = ""; my $outfolder = ""; my $ext = ".mp4"; my $helpstr = "Usage: mediaforker.pl \n\ This script will recursively process all titles in all VIDEO_TS folders inside . is required. \n\ is optional. If specified, this is where the movie files will be \ placed. If not specified, files will be placed at same level \ as the DVD folder that contains the VIDEO_TS folder. \n\ are optional. If no args are specified, defaults to \ H264/mp4 deinterlaced 2-pass 256k audio 2200k video \n\ Use \"MediaForkCLI --help\" to find out how to set your own compression args."; if( $narg == 0 ) { die $helpstr; } elsif( $narg == 1 ) { # Use high quality mp4/h264 as defaults. 2200 is high, but -q 1.0 generates only white video $infolder = $ARGV[0]; $compressargs = " -e x264 -f mp4 -2 -d -B 256 -b 2200 "; $allargs = $allargs . " $ARGV[0]"; } elsif( $narg == 2 ) { # Use high quality mp4/h264 as defaults. 2200 is high, but -q 1.0 generates only white video $infolder = $ARGV[0]; $outfolder = $ARGV[1]; $compressargs = " -e x264 -f mp4 -2 -d -B 256 -b 2200 "; $allargs = $allargs . " $ARGV[0] $ARGV[1]"; } elsif( $narg > 2 ) { # More switches used than just folder, so build up user-specified args from inputs $infolder = $ARGV[0]; $outfolder = $ARGV[1]; $allargs = $allargs . " $ARGV[0] $ARGV[1]"; for( my $thisArg=2; $thisArg < $narg; $thisArg++ ) { $compressargs = $compressargs . " $ARGV[$thisArg]"; $allargs = $allargs . " $ARGV[$thisArg]"; } } # Spit out some help if asked if( $allargs =~ /(-h|-help|--help|--h)/ ) { die $helpstr; } # Set filename extension for output movies if( $compressargs =~ /avi/ ) { $ext = ".avi"; } elsif( $compressargs =~ /ogm/ ) { $ext = ".ogm"; } else { $ext = ".mp4"; } # # Find MediaForkCLI (was handbrake) # my $MediaForkCLI = "/usr/local/bin/MediaForkCLI"; if ( !( -x $MediaForkCLI) ) { die "No MediaForkCLI at $MediaForkCLI\nPlease download it and place it in /usr/local/bin"; } # # Find VIDEO_TS folders # my @allvidts = (); # Traverse $infolder, push VIDEO_TS folders into allvidts File::Find::find({wanted => \&wanted}, $infolder); # # Processing loop # foreach my $vidts (@allvidts) { # Find all titles in this video_ts my @titles = (); open( SCANNER, "$MediaForkCLI -i \"$vidts\" -t 0 2>&1 |" ); while( ) { chomp(); push( @titles, $2) if( $_ =~ /(Scanning title )(\d)( of )(\d)*/ ); } close( SCANNER ); # Loop thought titles process each one foreach my $title (@titles) { # Set output file and output folder my $outfile = ""; if( $outfolder eq "" ) { my @ts_split = split( /\/VIDEO_TS/, $vidts ); $outfile = $ts_split[0] . ".title$title$ext"; } else { my @slash_split = split( /\//, $vidts ); $outfile = $outfolder . "\/" . $slash_split[-2] . ".title$title$ext"; } # finally, execute system call and process title my @mfcmd = ( "$MediaForkCLI -i \"$vidts\" -o \"$outfile\" -t $title $compressargs" ); system( @mfcmd ); } } # # Subroutines # # generated by find2perl sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); /^VIDEO_TS\z/s && (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -d _ && push( @allvidts, $name); }