#!/usr/bin/perl # Author: Alan J. Pippin # Description: Find movies from a temporary dropbox location and compress and move them into their final destination. use File::Copy; use File::Basename; use Getopt::Std; use File::stat; use Time::localtime; use File::Pid; #################################################################################################### # Configuration parameters my $movie_src_dir = "/naspool/dropbox/New Movies"; my $mobile_movie_src_dir = "/naspool/dropbox/New Movies/Mobile"; my $movie_dst_dir = "/movpool/movies"; my $mobile_movie_dst_dir = "/movpool/movies/Mobile"; my $movie_done_dir = "/naspool/dropbox/Encoded"; my $log = "/var/log/organize/organize_movies.log"; my $encode_log = "/var/log/organize/organize_movies_encode.log"; my $minage = 1; #in minutes my $now = time; my $quality = 18; my $mailto = "admin\@pippins.net"; #################################################################################################### # Find the movies to encode # Only one instance of this script should be running at a time my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"}); exit if $pidfile->running(); $pidfile->write(); # Encode the movies #encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir); encode_movies($movie_src_dir, $movie_dst_dir); sub encode_movies { my ($movie_src_dir, $movie_dst_dir) = @_; # Search the $movie_src_dirs for movies to encode $date = `date`; print "$date"; print "-> Searching $movie_src_dir for movies to encode to $movie_dst_dir\n"; opendir(SRC_DIR, $movie_src_dir) || die "-E- Unable to open $movie_src_dir\n"; while(my $category = readdir(SRC_DIR)) { next if ! -d "$movie_src_dir/$category"; next if $category =~ /^\.+$/; print "-> Found category $category\n"; opendir(CAT_DIR,"$movie_src_dir/$category") || die "-E- Unable to open $movie_src_dir/$category\n"; while(my $filename = readdir(CAT_DIR)) { next if $filename =~ /^\.+$/; next if $filename =~ /\.FAILED$/; my $movie = "$movie_src_dir/$category/$filename"; next if ((! -f $movie) && (! -l $movie)); next if -e "$movie.FAILED"; my $stat = stat("$movie"); if($stat->mtime < ($now - $minage*60)) { my ($base, $dir, $ext) = fileparse($movie,'\..*'); $base =~ s/_t\d+$//g; # remove any extra title numbers from the name my $output = "$movie_dst_dir/$category/$base.mkv"; print "-> Encoding $movie => $output\n"; my $date = `date`; chomp $date; open LOG, ">>$log" || die "-E- Unable to open logfile $log\n"; print LOG "$date: started $movie => $output\n"; my $cmd = "HandBrakeCLI"; $cmd .= " -i \"$movie\" -o \"$output\""; $cmd .= " -f mkv --loose-anamorphic --denoise=\"weak\" -e x264"; #$cmd .= " -a 1,1"; $cmd .= " -N eng"; $cmd .= " --audio-lang-list eng"; $cmd .= " --first-audio"; $cmd .= " -x b-adapt=2:rc-lookahead=50 -v 2"; if($category =~ /Mobile/i || $movie_src_dir =~ /Mobile/i || $movie_dst_dir =~ /Mobile/i) { $cmd .= " --preset=\"Android 720p30\" -2 -B 256"; } else { $cmd .= " -q $quality"; #$cmd .= " -6 6ch"; $cmd .= " -E copy"; $cmd .= " --audio-copy-mask ac3,eac3,truehd,dts,dtshd"; $cmd .= " --preset=\"HQ 1080p30 Surround\""; } print " $cmd\n"; my $errno = system("$cmd > $encode_log 2>&1"); if($errno == 0) { if(-l "$movie") { unlink "$movie"; } else { system("mv \"$movie\" \"$movie_done_dir\""); } } else { system("mailx $mailto -s \"Encoding failed: $movie\" < $encode_log"); system("touch \"$movie.FAILED\""); } $date = `date`; chomp $date; print LOG "$date: finished $movie => $output\n"; close LOG; } } closedir(CAT_DIR); } closedir(SRC_DIR); } ####################################################################################################