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
10 use DateTime::Format::Strptime qw( );
12 # Set our ffmpeg creation_time format
13 $ffmpeg_time_format = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
14 $ctime_format = DateTime::Format::Strptime->new(pattern=>'%a %b %d %H:%M:%S %Y', time_zone => 'local', on_error => 'croak');
16 # Early command line options processing
18 my $srcpathname = $opt_s;
20 ####################################################################################################
21 # Configuration parameters
22 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
23 # Default configuration values
24 require "organize_videos.conf";
25 # Override defaults with local customizations
26 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
28 ####################################################################################################
31 print "usage: $0 [-tvrhz] -s <srcpath>\n";
32 print " -s <srcpath> specify the path to search for videos to merge under\n";
33 print " -h <compute host> specify the remote compute host to submit the mkvmerge job to\n";
34 print " -v verbose mode; print extra information about what is being found/merged\n";
35 print " -t test mode; print what will happen, but don't do anything\n";
36 print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
37 print " -q Requantize input videos to decrease output video size (requires HandBrakeCLI)\n";
38 print " -z Recompress input videos to decrease output video size (requires HandBrakeCLI)\n";
41 if(defined $opt_h) { usage(); exit 1; }
42 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
44 ####################################################################################################
46 if((defined $opt_q || defined $opt_z) && !$use_compute_host && ! -x "$handbrake") { die "-E- Unable to find required program: handbrake\n"; }
47 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
48 if(defined $opt_h) { $compute_host = $opt_h; }
49 if(! -x $ffmpeg) { die "-E- Missing required executable for ffmpeg: $ffmpeg\n"; }
51 my %monthname2month = (
66 my %month2monthname = (
81 # Change directories to the srcpath to search for videos to merge
82 print "-> Finding all videos under '$srcpathname' to merge by day\n";
85 print "$find_cmd\n" if($opt_v);
86 foreach $file (sort `$find_cmd`) {
89 $srcdir = dirname($file);
90 $file = basename($file);
93 if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
96 # Throw out files not in the current srcpath
97 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
99 # Throw out encoded files left over from a previous run
100 #print "srcfile: $srcfile\n" if($opt_v);
101 if($srcfile =~ /.hb.mp4/) { next; }
103 print "Found video: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
105 # Make a note of the month, year, and day this video was taken
106 $creation_time = `$ffmpeg -i "$srcdir/$srcfile" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
108 $date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
110 # From the modification time of the file since we couldn't find it in the video file
111 $date_modified = ctime(stat("$srcdir/$srcfile")->mtime);
114 # Get the date taken from the ffmpeg creation_time
115 if(!$merge_by_modification_time && $date_taken) {
116 $date_taken->set_time_zone('local');
117 $year = $date_taken->year;
118 $month = sprintf("%02d",$date_taken->month);
119 $day = sprintf("%02d",$date_taken->day);
120 $monthnum = sprintf("%02d",$date_taken->month);
121 $monthname = lc($month2monthname{$month});
122 #print "date_taken: $year-$month-$day ".$date_taken->hour.":".$date_taken->minute.":".$date_taken->second." ".$date_taken->time_zone."\n";
124 # Get the date taken from the filename
125 elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/) {
128 $day = sprintf("%02d",$3);
130 $monthname = lc($month2monthname{$month});
132 elsif(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
135 $day = sprintf("%02d",$3);
137 $monthname = lc($month2monthname{$month});
139 # Get the date taken from the srcdir
140 elsif(!$merge_by_modification_date && $srcdir =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
143 $day = sprintf("%02d",$3);
145 $monthname = lc($month2monthname{$month});
147 # Get the date taken from the modification time
148 elsif($date_modified =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
151 $day = sprintf("%02d",$2);
152 $monthnum = $monthname2month{$month};
153 $monthname = lc($month);
155 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
159 # We are ready to pick a destination folder to put the merged video in
161 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
163 # Make a note of this video and its merged destination
164 push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
167 # Tell the user which videos we are going to merge
168 foreach $dstfile (sort keys %{$videos}) {
169 foreach $srcfile (@{$videos{$dstfile}}) {
170 print " merging $srcfile into \"$dstfile\"\n";
174 # Now actually do the merging
176 foreach $dstfile (sort keys %videos) {
178 my $videos = join(',', @{$videos{$dstfile}});
180 if($dstfile =~ /(\d+)-(\d+)-(\d+)$/) {
183 $day = sprintf("%02d",$3);
186 my $pwd = `pwd`; chomp($pwd);
188 if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
189 $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$dstfile\" -i $videos";
190 if($requantize_input_video) { $cmd .= ' -q'; }
191 if($recompress_input_video) { $cmd .= ' -z'; }
192 if($opt_v) { $cmd .= ' -v'; }
193 if($use_compute_host) { $cmd .= "'"; }
195 # Print what will be done, but don't actually do it
196 print "\n-> Creating \"$dstfile\"\n";
198 if(!defined $opt_k) {
199 foreach $video (@{$videos{$dstfile}}) {
200 if($opt_v) { print " $ffmpeg -i $video 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
201 my $brands=`$ffmpeg -i $video 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
202 if(($save_originals) && ($video =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {
203 print "-> Saving the original video $video\n";
204 if(index($video, basename(dirname($video))) == -1) {
205 print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n");
207 print("mv $video \"$origpathname/".basename($video)."\n");
210 print "-> Removing the original video $video\n";
211 print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n");
216 # Create the merged video
217 print "$cmd\n" if($opt_v);
218 my $errno = system("$cmd");
219 $errno = $errno >> 8;
220 if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
221 system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there
223 # Fix the permissions
224 system("chown $owner \"$dstfile\"*");
225 system("chgrp $group \"$dstfile\"*");
226 system("find \"$dstfile\"* -type f -exec chmod $mode -- {} +");
228 # Remove the individual video files
229 if(!defined $opt_k) {
230 foreach $srcvideo (@{$videos{$dstfile}}) {
231 if($opt_v) { print " $ffmpeg -i $srcvideo 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
232 my $brands=`$ffmpeg -i $srcvideo 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
233 if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {
234 print "-> Saving the original video $srcvideo to $origpathname\n";
235 if(index($srcvideo, basename(dirname($srcvideo))) == -1) {
236 system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo));
238 system("mv $srcvideo \"$origpathname/".basename($srcvideo));
241 print "-> Removing the original video $srcvideo\n";
242 system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'");