2 # Author: Alan J. Pippin
3 # Description: Find movies from a temporary dropbox location and compress and move them into their final destination.
12 ####################################################################################################
13 # Configuration parameters
14 my $movie_src_dir = "/naspool/dropbox/New Movies";
15 my $mobile_movie_src_dir = "/naspool/dropbox/New Movies/Mobile";
16 my $movie_dst_dir = "/movpool/movies";
17 my $mobile_movie_dst_dir = "/movpool/movies/Mobile";
18 my $movie_done_dir = "/naspool/dropbox/Encoded";
19 my $log = "/var/log/organize/organize_movies.log";
20 my $encode_log = "/var/log/organize/organize_movies_encode.log";
21 my $minage = 1; #in minutes
24 my $mailto = "admin\@pippins.net";
26 ####################################################################################################
27 # Find the movies to encode
29 # Only one instance of this script should be running at a time
30 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
31 exit if $pidfile->running();
35 #encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir);
36 encode_movies($movie_src_dir, $movie_dst_dir);
39 my ($movie_src_dir, $movie_dst_dir) = @_;
41 # Search the $movie_src_dirs for movies to encode
42 $date = `date`; print "$date";
43 print "-> Searching $movie_src_dir for movies to encode to $movie_dst_dir\n";
44 opendir(SRC_DIR, $movie_src_dir) || die "-E- Unable to open $movie_src_dir\n";
45 while(my $category = readdir(SRC_DIR)) {
46 next if ! -d "$movie_src_dir/$category";
47 next if $category =~ /^\.+$/;
48 print "-> Found category $category\n";
49 opendir(CAT_DIR,"$movie_src_dir/$category") || die "-E- Unable to open $movie_src_dir/$category\n";
50 while(my $filename = readdir(CAT_DIR)) {
51 next if $filename =~ /^\.+$/;
52 next if $filename =~ /\.FAILED$/;
53 my $movie = "$movie_src_dir/$category/$filename";
54 next if ((! -f $movie) && (! -l $movie));
55 next if -e "$movie.FAILED";
56 my $stat = stat("$movie");
57 if($stat->mtime < ($now - $minage*60)) {
58 my ($base, $dir, $ext) = fileparse($movie,'\..*');
59 $base =~ s/_t\d+$//g; # remove any extra title numbers from the name
60 my $output = "$movie_dst_dir/$category/$base.mkv";
61 print "-> Encoding $movie => $output\n";
62 my $date = `date`; chomp $date;
63 open LOG, ">>$log" || die "-E- Unable to open logfile $log\n";
64 print LOG "$date: started $movie => $output\n";
65 my $cmd = "HandBrakeCLI";
66 $cmd .= " -i \"$movie\" -o \"$output\"";
67 $cmd .= " -f mkv --loose-anamorphic --denoise=\"weak\" -e x264";
69 $cmd .= " -x b-adapt=2:rc-lookahead=50 -v 2";
70 if($category =~ /Mobile/i || $movie_src_dir =~ /Mobile/i || $movie_dst_dir =~ /Mobile/i) {
71 $cmd .= " --preset=\"Android Tablet\" -2 -B 256";
73 $cmd .= " -q $quality";
75 $cmd .= " -E copy:ac3";
76 $cmd .= " --preset=\"High Profile\"";
79 my $errno = system("$cmd > $encode_log 2>&1");
84 system("mv \"$movie\" \"$movie_done_dir\"");
87 system("mailx $mailto -s \"Encoding failed: $movie\" < $encode_log");
88 system("touch \"$movie.FAILED\"");
90 $date = `date`; chomp $date;
91 print LOG "$date: finished $movie => $output\n";
100 ####################################################################################################