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
17 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
18 # Default configuration values
19 require "organize_videos.conf";
20 # Override defaults with local customizations
21 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
23 ####################################################################################################
26 print "usage: $0 [-tvrhz] -s <srcpath>\n";
27 print " -s <srcpath> specify the path to search for videos to merge under\n";
28 print " -h <compute host> specify the remote compute host to submit the mkvmerge job to\n";
29 print " -v verbose mode; print extra information about what is being found/merged\n";
30 print " -t test mode; print what will happen, but don't do anything\n";
31 print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
32 print " -q Requantize input videos to decrease output video size (requires HandBrakeCLI)\n";
33 print " -z Recompress input videos to decrease output video size (requires HandBrakeCLI)\n";
36 if(defined $opt_h) { usage(); exit 1; }
37 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
39 ####################################################################################################
41 if((defined $opt_q || defined $opt_z) && !$use_compute_host && ! -x "$handbrake") { die "-E- Unable to find required program: handbrake\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; }
44 if(! -x $ffmpeg) { die "-E- Missing required executable for ffmpeg: $ffmpeg\n"; }
46 my %monthname2month = (
61 my %month2monthname = (
76 # Change directories to the srcpath to search for videos to merge
77 print "-> Finding all videos under '$srcpathname' to merge by day\n";
80 print "$find_cmd\n" if($opt_v);
81 foreach $file (sort `$find_cmd`) {
84 $srcdir = dirname($file);
85 $file = basename($file);
88 if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
91 # Throw out files not in the current srcpath
92 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
94 # Throw out encoded files left over from a previous run
95 #print "srcfile: $srcfile\n" if($opt_v);
96 if($srcfile =~ /.hb.mp4/) { next; }
98 print "Found video: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
100 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
101 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
103 # Get the date taken from the filename
104 if(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/) {
107 $day = sprintf("%02d",$3);
109 $monthname = lc($month2monthname{$month});
111 elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
114 $day = sprintf("%02d",$3);
116 $monthname = lc($month2monthname{$month});
118 # Get the date taken from the srcdir
119 elsif(!$merge_by_modification_date && $srcdir =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
122 $day = sprintf("%02d",$3);
124 $monthname = lc($month2monthname{$month});
126 # Get the date taken from the modification time
127 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
130 $day = sprintf("%02d",$2);
131 $monthnum = $monthname2month{$month};
132 $monthname = lc($month);
134 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
138 # We are ready to pick a destination folder to put the merged video in
140 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
142 # Make a note of this video and its merged destination
143 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
146 # Tell the user which videos we are going to merge
147 foreach $dstfile (sort keys %{$videos}) {
148 foreach $srcfile (@{$videos{$dstfile}}) {
149 print " merging $srcfile into \"$dstfile\"\n";
153 # Now actually do the merging
155 foreach $dstfile (sort keys %videos) {
157 my $videos = join(',', @{$videos{$dstfile}});
159 if($dstfile =~ /(\d+)-(\d+)-(\d+)/) {
162 $day = sprintf("%02d",$3);
165 my $pwd = `pwd`; chomp($pwd);
167 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
168 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$dstfile\" -i $videos";
169 if($requantize_input_video) { $cmd .= ' -q'; }
170 if($recompress_input_video) { $cmd .= ' -z'; }
171 if($opt_v) { $cmd .= ' -v'; }
172 if($use_compute_host) { $cmd .= "'"; }
174 # Print what will be done, but don't actually do it
175 print "\n-> Creating \"$dstfile\"\n";
177 if(!defined $opt_k) {
178 foreach $video (@{$videos{$dstfile}}) {
179 if($opt_v) { print " $ffmpeg -i $video 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
180 my $brands=`$ffmpeg -i $video 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
181 if(($save_originals) && ($video =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {
182 print "-> Saving the original video $video\n";
183 if(index($video, basename(dirname($video))) == -1) {
184 print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n");
186 print("mv $video \"$origpathname/".basename($video)."\n");
189 print "-> Removing the original video $video\n";
190 print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n");
195 # Create the merged video
196 print "$cmd\n" if($opt_v);
197 my $errno = system("$cmd");
198 $errno = $errno >> 8;
199 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
200 system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there
202 # Fix the permissions
203 system("chown $owner \"$dstfile\"*");
204 system("chgrp $group \"$dstfile\"*");
205 system("find \"$dstfile\"* -type f -exec chmod $mode -- {} +");
207 # Remove the individual video files
208 if(!defined $opt_k) {
209 foreach $srcvideo (@{$videos{$dstfile}}) {
210 if($opt_v) { print " $ffmpeg -i $srcvideo 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
211 my $brands=`$ffmpeg -i $srcvideo 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
212 if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {
213 print "-> Saving the original video $srcvideo to $origpathname\n";
214 if(index($srcvideo, basename(dirname($srcvideo))) == -1) {
215 system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo));
217 system("mv $srcvideo \"$origpathname/".basename($srcvideo));
220 print "-> Removing the original video $srcvideo\n";
221 system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'");