Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[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 .= " -N eng";
70                 $cmd .= " --audio-lang-list eng";
71                 $cmd .= " --first-audio";
72                 $cmd .= " -x b-adapt=2:rc-lookahead=50 -v 2";
73                 if($category =~ /Mobile/i || $movie_src_dir =~ /Mobile/i || $movie_dst_dir =~ /Mobile/i) {
74                     $cmd .= " --preset=\"Android 720p30\" -2 -B 256";
75                 } else {
76                     $cmd .= " -q $quality";
77                     #$cmd .= " -6 6ch";
78                     $cmd .= " -E copy";
79                     $cmd .= " --audio-copy-mask ac3,eac3,truehd,dts,dtshd";
80                     $cmd .= " --preset=\"HQ 1080p30 Surround\"";
81                 }
82                 print "   $cmd\n";
83                 my $errno = system("$cmd > $encode_log 2>&1");
84                 if($errno == 0) {
85                     if(-l "$movie") {
86                         unlink "$movie";
87                     } else {
88                         system("mv \"$movie\" \"$movie_done_dir\"");
89                     }
90                 } else {
91                     system("mailx $mailto -s \"Encoding failed: $movie\" < $encode_log");
92                     system("touch \"$movie.FAILED\"");
93                 }
94                 $date = `date`; chomp $date;
95                 print LOG "$date: finished $movie => $output\n";
96                 close LOG;
97             }
98         }
99         closedir(CAT_DIR);
100     }
101     closedir(SRC_DIR);
102 }
103
104 ####################################################################################################