#!/usr/bin/perl # Author: Alan J. Pippin # Description: For the given srcpath, merge all the videos that were taken on the same day into a single mkv file use File::Copy; use File::Basename; use Getopt::Std; use File::stat; use Time::localtime; # Early command line options processing getopts("qzkh:tvs:"); my $srcpathname = $opt_s; #################################################################################################### # Configuration parameters $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir"); # Default configuration values require "organize_videos.conf"; # Override defaults with local customizations if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; } #################################################################################################### sub usage { print "usage: $0 [-tvrhz] -s \n"; print " -s specify the path to search for videos to merge under\n"; print " -h specify the remote compute host to submit the mkvmerge job to\n"; print " -v verbose mode; print extra information about what is being found/merged\n"; print " -t test mode; print what will happen, but don't do anything\n"; print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n"; print " -q Requantize input videos to decrease output video size (requires HandBrakeCLI)\n"; print " -z Recompress input videos to decrease output video size (requires HandBrakeCLI)\n"; return 1; } if(defined $opt_h) { usage(); exit 1; } # Sanity checks if((defined $opt_q || defined $opt_z) && !$use_compute_host && ! -x "$handbrake") { die "-E- Unable to find required program: handbrake\n"; } if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; } if(defined $opt_h) { $compute_host = $opt_h; } my %monthname2month = ( "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12" ); my %month2monthname = ( "01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec" ); # Change directories to the srcpath to search for videos to merge print "-> Finding all videos under '$srcpathname' to merge by day\n"; my %videos; chdir "$srcpathname"; print "$find_cmd\n" if($opt_v); foreach $file (sort `$find_cmd`) { chomp($file); $srcdir = dirname($file); $file = basename($file); $srcfile = $file; $srcext = ""; if($srcfile =~ /\.(\w+)$/) { $srcext = $1; } $ext = "mkv"; print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v); # Throw out files not in the current srcpath if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; } # Make a note of the month, year, and day this video was taken (from the modification time of the file) $date_taken = ctime(stat("$srcdir/$srcfile")->mtime); # Get the date taken from the filename if(!$merge_by_modification_date && $srcfile =~ /^(\d+)-(\d+)-(\d+)/) { $year = $1; $month = $2; $day = sprintf("%02d",$3); $monthnum = $month; $monthname = lc($month2monthname{$month}); } # Get the date taken from the modification time elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) { $year = $3; $month = $1; $day = sprintf("%02d",$2); $monthnum = $monthname2month{$month}; $monthname = lc($month2monthname{$month}); } else { print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n"; next; } # We are ready to pick a destination folder to put the merged video in $dstdir = $srcdir; $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day; # Check for duplicate filenames at the destination $newfile = $dstfile . "." . $video_suffix; if(-e "$newfile.$ext") { foreach $i ($video_suffix+1 .. '999') { $newfile = $dstfile . "." . sprintf("%03d",$i); if(! -e "$newfile.$ext") { last; } } $dstfile = $newfile; } # Set the name of our unique destination file $dstfile = "$newfile.$ext"; # You can only merge videos into a single destination that have the same extension/type push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\""); } # Check for duplicate filenames in the dstfiles being created for other exts foreach $ext (sort keys %videos) { foreach $video (sort keys %{$videos{$ext}}) { # Make sure this video name is not in use as a destination for any other ext foreach $checkext (sort keys %videos) { if($checkext eq $ext) { next; } foreach $checkvideo (sort keys %{$videos{$checkext}}) { if("$video" eq "$checkvideo") { if($video =~ /(.*?)\.(\d+)\.(\w+)$/) { $dstfile = $1; $dstnum = $2; $dstext = $3; } foreach $i ($dstnum .. '999') { $newfile = $dstfile . "." . sprintf("%03d",$i); if("$video" ne "$newfile.$dstext") { last; } } $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video}; delete $videos{$ext}{$video}; } } } } } # Tell the user which videos we are going to merge foreach $ext (sort keys %videos) { foreach $video (sort keys %{$videos{$ext}}) { foreach $srcfile (@{$videos{$ext}{$video}}) { print " merging $srcfile into \"$video\"\n"; } } } # Now actually do the merging print "\n"; foreach $ext (sort keys %videos) { foreach $video (sort keys %{$videos{$ext}}) { my $videos = join(',', @{$videos{$ext}{$video}}); if($video =~ /(\d+)-(\d+)-(\d+)/) { $year = $1; $month = $2; $day = sprintf("%02d",$3); } my $pwd = `pwd`; chomp($pwd); my $cmd = ""; if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; } $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos"; if($requantize_input_video) { $cmd .= ' -q'; } if($recompress_input_video) { $cmd .= ' -z'; } if($use_compute_host) { $cmd .= "'"; } if(defined $opt_t) { # Print what will be done, but don't actually do it print "\n-> Creating \"$video\"\n"; print "$cmd\n"; if(!defined $opt_k) { foreach $video (@{$videos{$ext}{$video}}) { if(($save_originals) && ($video =~ /\.$originals_file_ext/)) { print "-> Saving the original video $video\n"; print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n"); } else { print "-> Removing the original video $video\n"; print("/bin/bash -c '[[ -e $video ]] && rm -f $video'\n"); } } } } else { # Create the merged video my $errno = system("$cmd"); $errno = $errno >> 8; if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; } system("ls -l \"$srcpathname/\" > /dev/null"); # Make sure the video file is there # Fix the permissions system("chown $owner \"$video\""); system("chgrp $group \"$video\""); system("chmod $mode \"$video\""); # Remove the individual video files if(!defined $opt_k) { foreach $srcvideo (@{$videos{$ext}{$video}}) { if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/)) { print "-> Saving the original video $srcvideo to $origpathname\n"; system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo)); } else { print "-> Removing the original video $srcvideo\n"; system("/bin/bash -c '[[ -e $srcvideo ]] && rm -f $srcvideo'"); } } } } } }