ec00da97e7fe64f3ea8fe1a20240b5689a397ce8
[videoscripts/.git] / merge_videos_by_day
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: For the given srcpath, merge all the videos that were taken on the same day into a single mkv file
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10
11 # Early command line options processing
12 getopts("qzkh:tvs:");
13 my $srcpathname = $opt_s;
14
15 ####################################################################################################
16 # Configuration parameters
17 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
18 # Default configuration values
19 require "organize_videos.conf";
20 # Override defaults with local customizations
21 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
22
23 ####################################################################################################
24
25 sub usage {
26     print "usage: $0 [-tvrhz] -s <srcpath>\n";
27     print "   -s <srcpath>          specify the path to search for videos to merge under\n";
28     print "   -h <compute host>     specify the remote compute host to submit the mkvmerge job to\n";
29     print "   -v                    verbose mode; print extra information about what is being found/merged\n";
30     print "   -t                    test mode; print what will happen, but don't do anything\n";
31     print "   -k                    keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
32     print "   -q                    Requantize input videos to decrease output video size (requires HandBrakeCLI)\n";
33     print "   -z                    Recompress input videos to decrease output video size (requires HandBrakeCLI)\n";
34     return 1;
35 }
36 if(defined $opt_h) { usage(); exit 1; }
37 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
38
39 ####################################################################################################
40 # Sanity checks
41 if((defined $opt_q || defined $opt_z) && !$use_compute_host && ! -x "$handbrake") { die "-E- Unable to find required program: handbrake\n"; }
42 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
43 if(defined $opt_h) { $compute_host = $opt_h; }
44
45 my %monthname2month = (
46                        "Jan" => "01",
47                        "Feb" => "02",
48                        "Mar" => "03",
49                        "Apr" => "04",
50                        "May" => "05",
51                        "Jun" => "06",
52                        "Jul" => "07",
53                        "Aug" => "08",
54                        "Sep" => "09",
55                        "Oct" => "10",
56                        "Nov" => "11",
57                        "Dec" => "12"
58                        );
59
60 my %month2monthname = (
61                        "01" => "Jan",
62                        "02" => "Feb",
63                        "03" => "Mar",
64                        "04" => "Apr",
65                        "05" => "May",
66                        "06" => "Jun",
67                        "07" => "Jul",
68                        "08" => "Aug",
69                        "09" => "Sep",
70                        "10" => "Oct",
71                        "11" => "Nov",
72                        "12" => "Dec"
73                        );
74
75 # Change directories to the srcpath to search for videos to merge
76 print "-> Finding all videos under '$srcpathname' to merge by day\n";
77 my %videos;
78 chdir "$srcpathname";
79 print "$find_cmd\n" if($opt_v);
80 foreach $file (sort `$find_cmd`) {
81
82     chomp($file);
83     $srcdir = dirname($file);
84     $file = basename($file);
85     $srcfile = $file;
86     $srcext = "";
87     if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
88     $ext = "mkv";
89
90     # Throw out files not in the current srcpath
91     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
92
93     # Throw out encoded files left over from a previous run
94     print "srcfile: $srcfile\n";
95     if($srcfile =~ /.hb.mp4/) { next; }
96
97     print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
98             
99     # Make a note of the month, year, and day this video was taken (from the modification time of the file)
100     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
101     
102     # Get the date taken from the filename
103     if(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/) {
104         $year = $1;
105         $month = $2;
106         $day = sprintf("%02d",$3);
107         $monthnum = $month;
108         $monthname = lc($month2monthname{$month});
109     }
110     elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
111         $year = $1;
112         $month = $2;
113         $day = sprintf("%02d",$3);
114         $monthnum = $month;
115         $monthname = lc($month2monthname{$month});
116     }
117     # Get the date taken from the srcdir
118     elsif(!$merge_by_modification_date && $srcdir =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
119         $year = $1;
120         $month = $2;
121         $day = sprintf("%02d",$3);
122         $monthnum = $month;
123         $monthname = lc($month2monthname{$month});
124     }
125     # Get the date taken from the modification time
126     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
127         $year = $3;
128         $month = $1;
129         $day = sprintf("%02d",$2);
130         $monthnum = $monthname2month{$month};
131         $monthname = lc($month);
132     } else {
133         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
134         next;
135     }
136
137     # We are ready to pick a destination folder to put the merged video in
138     $dstdir = $srcdir;
139     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
140
141     # Make a note of this video and its merged destination
142     push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
143 }
144
145 # Tell the user which videos we are going to merge
146 foreach $dstfile (sort keys %{$videos}) {
147     foreach $srcfile (@{$videos{$dstfile}}) { 
148         print "   merging $srcfile into \"$dstfile\"\n";
149     }
150 }
151
152 # Now actually do the merging
153 print "\n";
154 foreach $dstfile (sort keys %videos) {
155
156     my $videos = join(',', @{$videos{$dstfile}});
157     
158     if($dstfile =~ /(\d+)-(\d+)-(\d+)/) {
159         $year = $1;
160         $month = $2;
161         $day = sprintf("%02d",$3);
162     }
163     
164     my $pwd = `pwd`; chomp($pwd);
165     my $cmd = "";
166     if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
167     $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$dstfile\" -i $videos";
168     if($requantize_input_video) { $cmd .= ' -q'; }
169     if($recompress_input_video) { $cmd .= ' -z'; }
170     if($opt_v) { $cmd .= ' -v'; }
171     if($use_compute_host) { $cmd .= "'"; }
172     if(defined $opt_t) {
173         # Print what will be done, but don't actually do it
174         print "\n-> Creating \"$dstfile\"\n";
175         print "$cmd\n";
176         if(!defined $opt_k) { 
177             foreach $video (@{$videos{$dstfile}}) {
178                 if(($save_originals) && ($video =~ /\.$originals_file_ext/)) {
179                     print "-> Saving the original video $video\n";
180                     if(index($video, basename(dirname($video))) == -1) { 
181                         print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n");
182                     } else {
183                         print("mv $video \"$origpathname/".basename($video)."\n");
184                     }
185                 } else {
186                     print "-> Removing the original video $video\n";
187                     print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n");
188                 }
189             }
190         }
191     } else {
192         # Create the merged video
193         print "$cmd" if($opt_v);
194         my $errno = system("$cmd");
195         $errno = $errno >> 8;
196         if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
197         system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there
198
199         # Fix the permissions
200         system("chown $owner \"$dstfile\"*");
201         system("chgrp $group \"$dstfile\"*");
202         system("chmod $mode \"$dstfile\"*");
203
204         # Remove the individual video files
205         if(!defined $opt_k) { 
206             foreach $srcvideo (@{$videos{$dstfile}}) {
207                 if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/)) {
208                     print "-> Saving the original video $srcvideo to $origpathname\n";
209                     if(index($srcvideo, basename(dirname($srcvideo))) == -1) { 
210                         system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo));
211                     } else {
212                         system("mv $srcvideo \"$origpathname/".basename($srcvideo));
213                     }
214                 } else {
215                     print "-> Removing the original video $srcvideo\n";
216                     system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'");
217                 }
218             }
219         }
220     }
221 }