Various bug fixes and improvements.
[videoscripts/.git] / merge_videos_by_day
index 6d48bfc5dc6dc36ac43dfdd8abaa6c549cf63cb5..cd980405fd0abff79574b1f4c67da4133bda5418 100755 (executable)
@@ -8,6 +8,10 @@ use Getopt::Std;
 use File::stat;
 use Time::localtime;
 
+# Early command line options processing
+getopts("rh:tvs:");
+my $srcpathname = $opt_s;
+
 ####################################################################################################
 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
 my $compute_host = "pippin.pippins.net"; # I need this since this script is run from a virtual machine 
@@ -22,9 +26,6 @@ my $video_title_prefix = "HomeVideos:"; # What text to put on the front of the t
 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
 ####################################################################################################
 
-# Command line options
-getopts("rh:tvs:");
-
 sub usage {
     print "usage: $0 [-tvrh] -s <srcpath>\n";
     print "   -s <srcpath>          specify the path to search for videos to merge under\n";
@@ -34,7 +35,6 @@ sub usage {
     print "   -r                    remove merged video clips; after a merge, remove the individual video files that were merged\n";
     return 1;
 }
-my $srcpathname = $opt_s;
 if(defined $opt_h) { usage(); exit 1; }
 
 # Sanity checks
@@ -121,17 +121,29 @@ foreach $file (sort `$find_cmd`) {
     $newfile = $dstfile . "." . $video_suffix;
     if(-e "$newfile.$ext") {
        foreach $i ($video_suffix+1 .. '999') {
-           $newfile = $dstfile . "." . $i;
+           $newfile = $dstfile . "." . sprintf("%03d",$i);
            if(! -e "$newfile.$ext") { last; }
        }
        $dstfile = $newfile;
     }
     $dstfile = "$newfile.$ext";
 
-    print "   merging \"$srcdir/$srcfile\" into \"$dstfile\"\n";
     push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
 }
 
+# Tell the user which videos we are going to merge
+foreach $video (sort keys %videos) {
+    # Only merge the videos if there is more than 1
+    my $num_videos = $#{$videos{$video}} + 1;
+    if($num_videos <= 1) { next; }
+
+    foreach $srcfile (@{$videos{$video}}) { 
+       print "   merging \"$srcfile\" into \"$video\"\n";
+    }
+}
+print "\n";
+
+# Now actually do the merging
 foreach $video (sort keys %videos) {
 
     my $videos = join(',', @{$videos{$video}});
@@ -162,7 +174,7 @@ foreach $video (sort keys %videos) {
     } else {
        # Create the merged video
        my $errno = system("$cmd");
-       if($errno > 0) { $errno = $errno - 255; }
+       $errno = $errno >> 8;
        if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
        # Fix the permissions
        system("chown $owner \"$video\"");