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 ####################################################################################################
12 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
13 my $compute_host = "pippin.pippins.net"; # I need this since this script is run from a virtual machine
14 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.
15 my $make_mkv = "/naspool/videos/bin/make_mkv"; # Update this to be the path to the make_mkv script
16 my $minage = "+10"; # Files must be older than X minutes to move
17 my $owner = "ajp"; # The owner of the files after they are moved
18 my $group = "pip"; # The group of the files after they are moved
19 my $mode = "664"; # The mode to set on each file after they are moved
20 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
21 my $video_title_prefix = "HomeVideos:"; # What text to put on the front of the title for the merged video being created
22 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
23 ####################################################################################################
25 # Command line options
29 print "usage: $0 [-tvrh] -s <srcpath>\n";
30 print " -s <srcpath> specify the path to search for videos to merge under\n";
31 print " -h <compute host> specify the remote compute host to submit the mkvmerge job to\n";
32 print " -v verbose mode; print extra information about what is being found/merged\n";
33 print " -t test mode; print what will happen, but don't do anything\n";
34 print " -r remove merged video clips; after a merge, remove the individual video files that were merged\n";
37 my $srcpathname = $opt_s;
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 . "." . $i;
125 if(! -e "$newfile.$ext") { last; }
129 $dstfile = "$newfile.$ext";
131 print " merging \"$srcdir/$srcfile\" into \"$dstfile\"\n";
132 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
135 foreach $video (sort keys %videos) {
137 my $videos = join(',', @{$videos{$video}});
139 # Only merge the videos if there is more than 1
140 my $num_videos = $#{$videos{$video}} + 1;
141 if($num_videos <= 1) { next; }
143 if($video =~ /(\d+)-(\d+)-(\d+)/) {
146 $day = sprintf("%02d",$3);
149 my $pwd = `pwd`; chomp($pwd);
151 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
152 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
153 if($use_compute_host) { $cmd .= "'"; }
155 print "\n-> Creating \"$video\"\n";
158 foreach $video (@{$videos{$video}}) {
159 print("rm -f $video\n");
163 # Create the merged video
164 my $errno = system("$cmd");
165 if($errno > 0) { $errno = $errno - 255; }
166 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
167 # Fix the permissions
168 system("chown $owner \"$video\"");
169 system("chgrp $group \"$video\"");
170 system("chmod $mode \"$video\"");
171 # Remove the individual video files
173 foreach $video (@{$videos{$video}}) {
174 system("rm -f $video");