13 print "usage: $0 [-t] -s <srcpath>\n";
14 print " -s <srcpath> specify the path to search for videos to merge under\n";
15 print " -h <compute host> specify the remote compute host to submit the mkvmerge job to\n";
16 print " -v verbose mode; print extra information about what is being found/merged\n";
17 print " -t test mode; print what will happen, but don't do anything\n";
20 if(defined $opt_h) { usage(); exit 1; }
22 # Configuration parameters
23 my $compute_host = "pippin.pippins.net";
24 my $srcpathname = $opt_s;
25 my $minage = "+10"; # Files must be older than X minutes to move
26 my $owner = "ajp"; # The owner of the files after they are moved
27 my $group = "pip"; # The group of the files after they are moved
28 my $mode = "664"; # The mode to set on each file after they are moved
29 my $make_mkv = "/naspool/videos/bin/make_mkv"; chomp($make_mkv);
30 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
33 if(! -x $make_mkv) { die "-E- Unable to find required script: make_mkv\n"; }
34 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
35 if(defined $opt_h) { $compute_host = $opt_h; }
37 my %monthname2month = (
52 my %month2monthname = (
67 # Change directories to the srcpath to search for videos to merge
68 print "-> Finding all videos under '$srcpathname' to merge by day\n";
71 print "$find_cmd\n" if($opt_v);
72 foreach $file (sort `$find_cmd`) {
75 $srcdir = dirname($file);
76 $file = basename($file);
80 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
82 # Throw out files not in the current srcpath
83 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
85 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
86 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
88 # Get the date taken from the filename
89 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
92 $day = sprintf("%02d",$3);
94 $monthname = lc($month2monthname{$month});
96 # Get the date taken from the modification time
97 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
100 $day = sprintf("%02d",$2);
101 $monthnum = $monthname2month{$month};
102 $monthname = lc($month2monthname{$month});
104 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
108 # We are ready to pick a destination folder to put the merged video in
110 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
112 # Check for duplicate filenames at the destination
113 $newfile = $dstfile . "." . "000";
114 if(-e "$newfile.$ext") {
115 foreach $i ('001' .. '999') {
116 $newfile = $dstfile . "." . $i;
117 if(! -e "$newfile.$ext") { last; }
121 $dstfile = "$newfile.$ext";
123 print " merging \"$srcdir/$srcfile\" into \"$dstfile\"\n";
124 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
127 foreach $video (sort keys %videos) {
129 my $videos = join(',', @{$videos{$video}});
131 # Only merge the videos if there is more than 1
132 my $num_videos = $#{$videos{$video}} + 1;
133 if($num_videos <= 1) { next; }
135 if($video =~ /(\d+)-(\d+)-(\d+)/) {
138 $day = sprintf("%02d",$3);
141 my $pwd = `pwd`; chomp($pwd);
142 my $cmd = "ssh $compute_host 'cd \"$pwd\"; $make_mkv -t \"HomeVideos: $year-$month-$day\" -o \"$video\" -i $videos'\n";
144 print "\n-> Creating \"$video\"\n";
146 foreach $video (@{$videos{$video}}) {
147 print("rm -f $video\n");
150 # Create the merged video
151 my $errno = system("$cmd");
152 if($errno > 0) { $errno = $errno - 255; }
153 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
154 # Fix the permissions
155 system("chown $owner \"$video\"");
156 system("chgrp $group \"$video\"");
157 system("chmod $mode \"$video\"");
158 # Remove the individual video files
159 foreach $video (@{$videos{$video}}) {
160 system("rm -f $video");