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
11 # Early command line options processing
13 my $srcpathname = $opt_s;
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"; }
23 ####################################################################################################
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";
36 if(defined $opt_h) { usage(); exit 1; }
37 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
39 ####################################################################################################
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; }
45 my %monthname2month = (
60 my %month2monthname = (
75 # Change directories to the srcpath to search for videos to merge
76 print "-> Finding all videos under '$srcpathname' to merge by day\n";
79 print "$find_cmd\n" if($opt_v);
80 foreach $file (sort `$find_cmd`) {
83 $srcdir = dirname($file);
84 $file = basename($file);
87 if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
90 # Throw out files not in the current srcpath
91 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
93 # Throw out encoded files left over from a previous run
94 print "srcfile: $srcfile\n";
95 if($srcfile =~ /.hb.mp4/) { next; }
97 print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
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);
102 # Get the date taken from the filename
103 if(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/) {
106 $day = sprintf("%02d",$3);
108 $monthname = lc($month2monthname{$month});
110 elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
113 $day = sprintf("%02d",$3);
115 $monthname = lc($month2monthname{$month});
117 # Get the date taken from the srcdir
118 elsif(!$merge_by_modification_date && $srcdir =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
121 $day = sprintf("%02d",$3);
123 $monthname = lc($month2monthname{$month});
125 # Get the date taken from the modification time
126 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
129 $day = sprintf("%02d",$2);
130 $monthnum = $monthname2month{$month};
131 $monthname = lc($month);
133 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
137 # We are ready to pick a destination folder to put the merged video in
139 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
141 # Make a note of this video and its merged destination
142 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
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";
152 # Now actually do the merging
154 foreach $dstfile (sort keys %videos) {
156 my $videos = join(',', @{$videos{$dstfile}});
158 if($dstfile =~ /(\d+)-(\d+)-(\d+)/) {
161 $day = sprintf("%02d",$3);
164 my $pwd = `pwd`; chomp($pwd);
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 .= "'"; }
173 # Print what will be done, but don't actually do it
174 print "\n-> Creating \"$dstfile\"\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");
183 print("mv $video \"$origpathname/".basename($video)."\n");
186 print "-> Removing the original video $video\n";
187 print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n");
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
199 # Fix the permissions
200 system("chown $owner \"$dstfile\"*");
201 system("chgrp $group \"$dstfile\"*");
202 system("chmod $mode \"$dstfile\"*");
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));
212 system("mv $srcvideo \"$origpathname/".basename($srcvideo));
215 print "-> Removing the original video $srcvideo\n";
216 system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'");