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 print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
90 # Throw out files not in the current srcpath
91 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
93 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
94 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
96 # Get the date taken from the filename
97 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
100 $day = sprintf("%02d",$3);
102 $monthname = lc($month2monthname{$month});
104 # Get the date taken from the modification time
105 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
108 $day = sprintf("%02d",$2);
109 $monthnum = $monthname2month{$month};
110 $monthname = lc($month2monthname{$month});
112 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
116 # We are ready to pick a destination folder to put the merged video in
118 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
120 # Check for duplicate filenames at the destination
121 $newfile = $dstfile . "." . $video_suffix;
122 if(-e "$newfile.$ext") {
123 foreach $i ($video_suffix+1 .. '999') {
124 $newfile = $dstfile . "." . sprintf("%03d",$i);
125 if(! -e "$newfile.$ext") { last; }
130 # Set the name of our unique destination file
131 $dstfile = "$newfile.$ext";
133 # You can only merge videos into a single destination that have the same extension/type
134 push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\"");
137 # Check for duplicate filenames in the dstfiles being created for other exts
138 foreach $ext (sort keys %videos) {
139 foreach $video (sort keys %{$videos{$ext}}) {
140 # Make sure this video name is not in use as a destination for any other ext
141 foreach $checkext (sort keys %videos) {
142 if($checkext eq $ext) { next; }
143 foreach $checkvideo (sort keys %{$videos{$checkext}}) {
144 if("$video" eq "$checkvideo") {
145 if($video =~ /(.*?)\.(\d+)\.(\w+)$/) {
150 foreach $i ($dstnum .. '999') {
151 $newfile = $dstfile . "." . sprintf("%03d",$i);
152 if("$video" ne "$newfile.$dstext") { last; }
154 $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video};
155 delete $videos{$ext}{$video};
162 # Tell the user which videos we are going to merge
163 foreach $ext (sort keys %videos) {
164 foreach $video (sort keys %{$videos{$ext}}) {
165 foreach $srcfile (@{$videos{$ext}{$video}}) {
166 print " merging \"$srcfile\" into \"$video\"\n";
171 # Now actually do the merging
173 foreach $ext (sort keys %videos) {
174 foreach $video (sort keys %{$videos{$ext}}) {
176 my $videos = join(',', @{$videos{$ext}{$video}});
178 if($video =~ /(\d+)-(\d+)-(\d+)/) {
181 $day = sprintf("%02d",$3);
184 my $pwd = `pwd`; chomp($pwd);
186 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
187 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
188 if($requantize_input_video) { $cmd .= ' -q'; }
189 if($recompress_input_video) { $cmd .= ' -z'; }
190 if($use_compute_host) { $cmd .= "'"; }
192 # Print what will be done, but don't actually do it
193 print "\n-> Creating \"$video\"\n";
195 if(!defined $opt_k) {
196 foreach $video (@{$videos{$ext}{$video}}) {
197 print("rm -f $video\n");
201 # Create the merged video
202 my $errno = system("$cmd");
203 $errno = $errno >> 8;
204 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
205 system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there
206 # Fix the permissions
207 system("chown $owner \"$video\"");
208 system("chgrp $group \"$video\"");
209 system("chmod $mode \"$video\"");
210 # Remove the individual video files
211 if(!defined $opt_k) {
212 foreach $srcvideo (@{$videos{$ext}{$video}}) {
213 system("rm -f $srcvideo");