Fixed bug in how dates were extracted from video files
[videoscripts/.git] / organize_movies
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Find movies from a temporary dropbox location and compress and move them into their final destination.
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10 use File::Pid;
11
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
22 my $now = time;
23 my $quality = 18;
24 my $mailto = "admin\@pippins.net";
25
26 ####################################################################################################
27 # Find the movies to encode
28
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();
32 $pidfile->write();
33
34 # Encode the movies
35 #encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir);
36 encode_movies($movie_src_dir, $movie_dst_dir);
37
38 sub encode_movies {
39     my ($movie_src_dir, $movie_dst_dir) = @_;
40     
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";
68                 $cmd .= " -a 1,1";
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";
72                 } else {
73                     $cmd .= " -q $quality";
74                     $cmd .= " -6 6ch";
75                     $cmd .= " -E copy:ac3";
76                     $cmd .= " --preset=\"High Profile\"";
77                 }
78                 print "   $cmd\n";
79                 my $errno = system("$cmd > $encode_log 2>&1");
80                 if($errno == 0) {
81                     if(-l "$movie") {
82                         unlink "$movie";
83                     } else {
84                         system("mv \"$movie\" \"$movie_done_dir\"");
85                     }
86                 } else {
87                     system("mailx $mailto -s \"Encoding failed: $movie\" < $encode_log");
88                     system("touch \"$movie.FAILED\"");
89                 }
90                 $date = `date`; chomp $date;
91                 print LOG "$date: finished $movie => $output\n";
92                 close LOG;
93             }
94         }
95         closedir(CAT_DIR);
96     }
97     closedir(SRC_DIR);
98 }
99
100 ####################################################################################################