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; }
39 if((defined $opt_q || defined $opt_z) && !$use_compute_host && ! -x "$handbrake") { die "-E- Unable to find required program: handbrake\n"; }
40 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
41 if(defined $opt_h) { $compute_host = $opt_h; }
43 my %monthname2month = (
58 my %month2monthname = (
73 # Change directories to the srcpath to search for videos to merge
74 print "-> Finding all videos under '$srcpathname' to merge by day\n";
77 print "$find_cmd\n" if($opt_v);
78 foreach $file (sort `$find_cmd`) {
81 $srcdir = dirname($file);
82 $file = basename($file);
85 if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
88 # Throw out files not in the current srcpath
89 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
91 # Throw out encoded files left over from a previous run
92 print "srcfile: $srcfile\n";
93 if($srcfile =~ /.hb.mp4/) { next; }
95 print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
97 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
98 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
100 # Get the date taken from the filename
101 if(!$merge_by_modification_date && $srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
104 $day = sprintf("%02d",$3);
106 $monthname = lc($month2monthname{$month});
108 elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
111 $day = sprintf("%02d",$3);
113 $monthname = lc($month2monthname{$month});
115 # Get the date taken from the srcdir
116 elsif(!$merge_by_modification_date && $srcdir =~ /(\d+)-(\d+)-(\d+)/) {
119 $day = sprintf("%02d",$3);
121 $monthname = lc($month2monthname{$month});
123 # Get the date taken from the modification time
124 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
127 $day = sprintf("%02d",$2);
128 $monthnum = $monthname2month{$month};
129 $monthname = lc($month);
131 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
135 # We are ready to pick a destination folder to put the merged video in
137 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
139 # Check for duplicate filenames at the destination
140 $newfile = $dstfile . "." . $video_suffix;
141 if(-e "$newfile.$ext") {
142 foreach $i ($video_suffix+1 .. '999') {
143 $newfile = $dstfile . "." . sprintf("%03d",$i);
144 if(! -e "$newfile.$ext") { last; }
149 # Set the name of our unique destination file
150 $dstfile = "$newfile.$ext";
152 # You can only merge videos into a single destination that have the same extension/type
153 push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\"");
156 # Check for duplicate filenames in the dstfiles being created for other exts
159 foreach $ext (sort keys %videos) {
160 #print "checking: $ext\n";
161 foreach $video (sort keys %{$videos{$ext}}) {
162 #print "checking: $ext $video\n";
163 # Make sure this video name is not in use as a destination for any other ext
164 foreach $checkext (sort keys %videos) {
165 #print "checking: $ext $video $checkext\n";
166 if($checkext eq $ext) { next; }
167 foreach $checkvideo (sort keys %{$videos{$checkext}}) {
168 #print "checking: $ext $video $checkext $checkvideo\n";
169 if("$video" eq "$checkvideo") {
170 if($video =~ /(.*?)\.(\d+)\.(\w+)$/) {
175 foreach $i ($last_dstnum .. '999') {
176 $last_dstnum = $i + 1;
177 $newfile = $dstfile . "." . sprintf("%03d",$i);
178 if("$video" ne "$newfile.$dstext") { last; }
180 $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video};
181 #print "for ext $ext changed destination to: $newfile.$dstext: $videos{$ext}{$video};\n";
182 delete $videos{$ext}{$video};
187 if($changed_dst) { last; }
189 if($changed_dst) { last; }
194 # Tell the user which videos we are going to merge
195 foreach $ext (sort keys %videos) {
196 foreach $video (sort keys %{$videos{$ext}}) {
197 foreach $srcfile (@{$videos{$ext}{$video}}) {
198 print " merging $ext $srcfile into \"$video\"\n";
203 # Now actually do the merging
205 foreach $ext (sort keys %videos) {
206 foreach $video (sort keys %{$videos{$ext}}) {
208 my $videos = join(',', @{$videos{$ext}{$video}});
210 if($video =~ /(\d+)-(\d+)-(\d+)/) {
213 $day = sprintf("%02d",$3);
216 my $pwd = `pwd`; chomp($pwd);
218 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
219 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
220 if($requantize_input_video) { $cmd .= ' -q'; }
221 if($recompress_input_video) { $cmd .= ' -z'; }
222 if($opt_v) { $cmd .= ' -v'; }
223 if($use_compute_host) { $cmd .= "'"; }
225 # Print what will be done, but don't actually do it
226 print "\n-> Creating \"$video\"\n";
228 if(!defined $opt_k) {
229 foreach $video (@{$videos{$ext}{$video}}) {
230 if(($save_originals) && ($video =~ /\.$originals_file_ext/)) {
231 print "-> Saving the original video $video\n";
232 if(index($video, basename(dirname($video))) == -1) {
233 print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n");
235 print("mv $video \"$origpathname/".basename($video)."\n");
238 print "-> Removing the original video $video\n";
239 print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n");
244 # Create the merged video
245 print "$ext: $cmd" if($opt_v);
246 my $errno = system("$cmd");
247 $errno = $errno >> 8;
248 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
249 system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there
250 # Fix the permissions
252 system("chown $owner \"$video\"");
253 system("chgrp $group \"$video\"");
254 system("chmod $mode \"$video\"");
256 # Remove the individual video files
257 if(!defined $opt_k) {
258 foreach $srcvideo (@{$videos{$ext}{$video}}) {
259 if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/)) {
260 print "-> Saving the original video $srcvideo to $origpathname\n";
261 if(index($srcvideo, basename(dirname($srcvideo))) == -1) {
262 system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo));
264 system("mv $srcvideo \"$origpathname/".basename($srcvideo));
267 print "-> Removing the original video $srcvideo\n";
268 system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'");