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 - CHANGE THESE TO SUITE YOUR NEEDS
17 my $compute_host = "pippin.pippins.net"; # I need this since this script is run from a virtual machine
18 my $use_compute_host = 1; # Set to 1 to use a remote compute host to run the mkvmerge command. Set to 0 to use the local host to run it.
19 my $make_mkv = "/naspool/videos/bin/make_mkv"; # Update this to be the path to the make_mkv script
20 my $minage = "+10"; # Files must be older than X minutes to move
21 my $owner = "ajp"; # The owner of the files after they are moved
22 my $group = "pip"; # The group of the files after they are moved
23 my $mode = "664"; # The mode to set on each file after they are moved
24 my $video_suffix = "000"; # What number to start with when adding an incrementing suffix to the end of the video clip to avoid name collisons
25 my $video_title_prefix = "HomeVideos:"; # What text to put on the front of the title for the merged video being created
26 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
27 ####################################################################################################
30 print "usage: $0 [-tvrh] -s <srcpath>\n";
31 print " -s <srcpath> specify the path to search for videos to merge under\n";
32 print " -h <compute host> specify the remote compute host to submit the mkvmerge job to\n";
33 print " -v verbose mode; print extra information about what is being found/merged\n";
34 print " -t test mode; print what will happen, but don't do anything\n";
35 print " -r remove merged video clips; after a merge, remove the individual video files that were merged\n";
38 if(defined $opt_h) { usage(); exit 1; }
41 if(! -x $make_mkv) { die "-E- Unable to find required script: make_mkv\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);
88 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $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; }
129 $dstfile = "$newfile.$ext";
131 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
134 # Tell the user which videos we are going to merge
135 foreach $video (sort keys %videos) {
136 # Only merge the videos if there is more than 1
137 my $num_videos = $#{$videos{$video}} + 1;
138 if($num_videos <= 1) { next; }
140 foreach $srcfile (@{$videos{$video}}) {
141 print " merging \"$srcfile\" into \"$video\"\n";
146 # Now actually do the merging
147 foreach $video (sort keys %videos) {
149 my $videos = join(',', @{$videos{$video}});
151 # Only merge the videos if there is more than 1
152 my $num_videos = $#{$videos{$video}} + 1;
153 if($num_videos <= 1) { next; }
155 if($video =~ /(\d+)-(\d+)-(\d+)/) {
158 $day = sprintf("%02d",$3);
161 my $pwd = `pwd`; chomp($pwd);
163 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
164 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
165 if($use_compute_host) { $cmd .= "'"; }
167 print "\n-> Creating \"$video\"\n";
170 foreach $video (@{$videos{$video}}) {
171 print("rm -f $video\n");
175 # Create the merged video
176 my $errno = system("$cmd");
177 $errno = $errno >> 8;
178 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
179 # Fix the permissions
180 system("chown $owner \"$video\"");
181 system("chgrp $group \"$video\"");
182 system("chmod $mode \"$video\"");
183 # Remove the individual video files
185 foreach $video (@{$videos{$video}}) {
186 system("rm -f $video");