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);
87 if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
90 print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
92 # Throw out files not in the current srcpath
93 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
95 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
96 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
98 # Get the date taken from the filename
99 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
102 $day = sprintf("%02d",$3);
104 $monthname = lc($month2monthname{$month});
106 # Get the date taken from the modification time
107 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
110 $day = sprintf("%02d",$2);
111 $monthnum = $monthname2month{$month};
112 $monthname = lc($month2monthname{$month});
114 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
118 # We are ready to pick a destination folder to put the merged video in
120 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
122 # Check for duplicate filenames at the destination
123 $newfile = $dstfile . "." . $video_suffix;
124 if(-e "$newfile.$ext") {
125 foreach $i ($video_suffix+1 .. '999') {
126 $newfile = $dstfile . "." . sprintf("%03d",$i);
127 if(! -e "$newfile.$ext") { last; }
132 # Set the name of our unique destination file
133 $dstfile = "$newfile.$ext";
135 # You can only merge videos into a single destination that have the same extension/type
136 push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\"");
139 # Only merge the videos if there is more than 1 video to merge on a given day for a given ext
140 foreach $ext (sort keys %videos) {
141 foreach $video (sort keys %{$videos{$ext}}) {
142 my $num_videos = $#{$videos{$ext}{$video}} + 1;
143 if($num_videos <= 1) {
144 delete $videos{$ext}{$video};
150 # Check for duplicate filenames in the dstfiles being created for other exts
151 foreach $ext (sort keys %videos) {
152 foreach $video (sort keys %{$videos{$ext}}) {
153 # Make sure this video name is not in use as a destination for any other ext
154 foreach $checkext (sort keys %videos) {
155 if($checkext eq $ext) { next; }
156 foreach $checkvideo (sort keys %{$videos{$checkext}}) {
157 if("$video" eq "$checkvideo") {
158 if($video =~ /(.*?)\.(\d+)\.(\w+)$/) {
163 foreach $i ($dstnum .. '999') {
164 $newfile = $dstfile . "." . sprintf("%03d",$i);
165 if("$video" ne "$newfile.$dstext") { last; }
167 $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video};
168 delete $videos{$ext}{$video};
175 # Tell the user which videos we are going to merge
176 foreach $ext (sort keys %videos) {
177 foreach $video (sort keys %{$videos{$ext}}) {
178 foreach $srcfile (@{$videos{$ext}{$video}}) {
179 print " merging \"$srcfile\" into \"$video\"\n";
184 # Now actually do the merging
186 foreach $ext (sort keys %videos) {
187 foreach $video (sort keys %{$videos{$ext}}) {
189 my $videos = join(',', @{$videos{$ext}{$video}});
191 # Only merge the videos if there is more than 1
192 #my $num_videos = $#{$videos{$ext}{$video}} + 1;
193 #if($num_videos <= 1) { next; }
195 if($video =~ /(\d+)-(\d+)-(\d+)/) {
198 $day = sprintf("%02d",$3);
201 my $pwd = `pwd`; chomp($pwd);
203 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
204 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
205 if($use_compute_host) { $cmd .= "'"; }
207 print "\n-> Creating \"$video\"\n";
210 foreach $video (@{$videos{$ext}{$video}}) {
211 print("rm -f $video\n");
215 # Create the merged video
216 my $errno = system("$cmd");
217 $errno = $errno >> 8;
218 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
219 # Fix the permissions
220 system("chown $owner \"$video\"");
221 system("chgrp $group \"$video\"");
222 system("chmod $mode \"$video\"");
223 # Remove the individual video files
225 foreach $video (@{$videos{$ext}{$video}}) {
226 system("rm -f $video");