Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[videoscripts/.git] / make_mkv
index 042e38eaca0eed66ede829b9c8f4a05f8ddaf0eb..ac85eab8a48cdfa444ac0bfd9925c78e3ab839c4 100755 (executable)
--- a/make_mkv
+++ b/make_mkv
@@ -5,6 +5,7 @@
 # Pre-requisites:
 # mkvtoolnix - http://www.bunkus.org/videotools/mkvtoolnix/
 # ffmpeg
+# avconv
 
 ####################################################################################################
 # Includes
@@ -12,42 +13,48 @@ use File::Copy;
 use File::Basename;
 use Getopt::Std;
 use File::stat;
+use Data::Dumper;
 use DateTime;
 use DateTime::Duration;
 use DateTime::Format::Duration;
+use DateTime::Format::Strptime qw( );
 
-####################################################################################################
-# Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
-my $mkvmerge=`which mkvmerge`; chomp($mkvmerge);
-my $ffmpeg=`which ffmpeg`; chomp($ffmpeg);
-my $tmpfile = `tempfile`; chomp($tmpfile);
-my $chapter_file = $tmpfile;
-my $input_file_options = "-S";
-my $output_file_options = "--chapters $chapter_file --compression -1:none";
-my $timezone = `cat /etc/timezone`; chomp($timezone);
-####################################################################################################
+# Set our ffmpeg creation_time format
+$ffmpeg_time_format_utc = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
+$ffmpeg_time_format_local = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'local', on_error => 'croak');
 
+####################################################################################################
+# 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"; }
 
 ####################################################################################################
 # Command Line Options
-getopts("st:o:i:h");
+getopts("svqzt:o:i:h");
 
-if(! -x $mkvmerge) { die "-E- Unable to find required program: mkvmerge\n"; }
-if(! -x $ffmpeg) { die "-E- Unable to find required program: ffmpeg\n"; }
 if(! defined $opt_t) { &usage(); die "-E- Missing required title: -t <title>\n"; }
 if(! defined $opt_o) { &usage(); die "-E- Missing required argument output video names: -o <output.mkv>\n"; }
 if(! defined $opt_i) { &usage(); die "-E- Missing required argument input video names: -i <input,input,...>\n"; }
+if(! -x $ffmpeg) { die "-E- Missing required executable for ffmpeg: $ffmpeg\n"; }
+if(! -x $avconv) { die "-E- Missing required executable for avconv: $avconv\n"; }
 
 sub usage {
     print "usage: $0 -t <title> -o <output.mkv> -i <input,input,...>\n";
     print "  -t <title>            Sets the general title for the output video file\n";
-    print "  -o <output.mkv>       Sets the name of the output mkv file\n";
-    print "  -i <input,input,...>  Sets the name of the input files to make into an mkv file\n";
+    print "  -o <output>           Sets the basename of the output mkv file\n";
+    print "  -i <input,input,...>  Sets the names of the input files to combine into a new mkv file\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";
     print "  -s                    Simulate mode. Don't actually make the video, but tell us what you will do\n";
+    print "  -v                    Increase verbosity for debug\n";
     print "\n";
     return 1;
 }
 
+$SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
 ####################################################################################################
 # Helper Subroutines
 sub epoch_to_date {
@@ -64,69 +71,303 @@ sub epoch_to_date {
 my %videos;
 foreach $video (split(/,/, $opt_i)) {
     if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; }
-    my $mtime_epoch = stat("$video")->mtime;
+    my $mtime_epoch = 0;
+    my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
+    my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
+    if($creation_time) {
+       my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time);
+       if ($brands && $brands =~ /$local_tz_brands/) {
+           $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time);
+       }
+       $date_taken->set_time_zone('local');
+       $mtime_epoch = $date_taken->epoch;
+    } else {
+       $mtime_epoch = stat("$video")->mtime;
+    }
     my $mdate = epoch_to_date($mtime_epoch);
     $videos{$video} = $mtime_epoch;
 }
 
-# Create the chapters file for the mkv file.
-# Make each video file it's own chapter in the MKV file.
-# Name the chapter with the date and time the video clip was taken (modified date).
-print "-> Creating $opt_o with title '$opt_t' from the following video files in last modified date order:\n";
-open(CHAPTERS,">$chapter_file") || die "-E- Unable to create chapter file: $chapter_file\n";
-my $chapter_num = 0;
-my $chapter_timecode = DateTime::Duration->new(years => 2000, hours => 0, minutes => 0, seconds => 0);
-my $timecode_format = DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N', normalize => 1);
+####################################################################################################
+# 1) Create the mkv video
+print "-> Creating MKV video files with basename '$opt_o'\n";
+
+# Make our input file command line options for all the videos
+my @video_tmp_files;
+my %merge_videos;
 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
-    $chapter_num++;
-    my $hour = 0;
-    my $min  = 5;
-    my $sec  = 0;
-    my $mtime_epoch = stat("$video")->mtime;
-    my $mdate = epoch_to_date($mtime_epoch);
-    my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`;
-    if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) {
-       $hour = $1;
-       $min = $2;
-       $sec = "$3.$4";
+    # Make a note of the video extension
+    my $video_ext = $video;
+    $video_ext =~ s/.*\.(\S+)$/$1/;
+
+    # Detect if the input file is interlaced or not.
+    # There is a bug in mkvmerge 5.0.1 and earlier where if the video content is 1080i, it doesn't mux it properly, and it stutters.
+    # The quick solution to this issue is to run the interlaced video file through ffmpeg and tell it to copy the video/audio streams to a mkv container.
+    # We will then merge this temporarily created mkv container into the final mkv container instead of the original interlaced video.
+    # http://ubuntuforums.org/showthread.php?t=1627194
+    # http://forum.doom9.org/showthread.php?t=155732&page=31
+
+    # This is the best way to detect if content is interlaced or progressive:
+    # http://www.aktau.be/2013/09/22/detecting-interlaced-video-with-ffmpeg/
+    my $progressive = 0;
+    my $detect_cmd = "$ffmpeg -filter:v idet -frames:v 100 -an -f rawvideo -y /dev/null -i \"$video\" 2>&1 | grep Parsed_idet"; 
+    if($opt_v) { print "   $detect_cmd\n"; }
+    my $detect_output = `$detect_cmd`;
+    if($detect_output !~ /Progressive:0/) { $progressive = 1; }
+    if(!$progressive) {
+       my $video_mkv = $video;
+       print "   Detected interlaced video content: $video\n";
+       # We don't need to do this anymore since it is not an issue with the new mkvmerge
+       if($video_ext !~ /mkv/i) {
+           $video_mkv =~ s/\.[^.]*$//; $video_mkv .= ".ffmpeg.mkv";
+           if(-f $video_mkv) { next; }
+           print "   Re-muxing the interlaced video content as an mkv file: $video_mkv\n";
+           my $make_mkv_cmd = "$avconv -y -i \"$video\" -scodec copy -acodec copy -vcodec copy -f matroska \"$video_mkv\" >> \"$tmpfile\" 2>&1";
+           my $errors = 0;
+           if($opt_v) { print "   $make_mkv_cmd\n"; }
+           if(! defined $opt_s) {
+               my $errno = system("$make_mkv_cmd");
+               $errno = $errno >> 8;
+               if($errno > 1) {
+                   unlink "$video_mkv";
+                   print "-W- avconv encountered some errors with exit code $errno\n";
+                   $errors = 1;
+               }
+           }
+           if($errors == 0) { 
+               # Push the name of our intermediate video file onto a list of files to delete on exit
+               push(@video_tmp_files, "$video_mkv");
+               # Copy the modification date from the src video to the dst video
+               system("touch \"$video_mkv\" -r \"$video\"");
+               # Update the name of our video file to equal the name of our new intermediate video file name
+               $video = $video_mkv;
+           }
+       }
+    } else { 
+       print "   Detected progressive video content: $video\n";
     }
-    my $timecode = $timecode_format->format_duration($chapter_timecode);
-    print "$mdate $hour:$min:$sec  -> $video \n";
-    print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."=".$timecode."\n";
-    print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."NAME=".$mdate."\n";
-    my $dt = DateTime::Duration->new(years => 2000, hours => $hour, minutes => $min, seconds => $sec);
-    $chapter_timecode = $chapter_timecode + $dt;
+
+    # Re-quantize or re-compress the input video to reduce the resulting output filesize
+    # This also gives us a chance to deinterlace the video as well
+    # Only re-quantize for .MTS videos
+    # We can re-compress any input video
+    my $rotated = "none";
+    if(((defined $opt_q) && ($video_ext =~ /mts/i)) || (defined $opt_z)) {
+
+       my $handbrake_options = "";
+
+       # Set our output video filename
+       my $video_mp4 = $video; $video_mp4 =~ s/\.[^.]*$//; $video_mp4 .= ".hb.mp4";
+       
+       # Don't recompress certain file extensions
+       # Unless the video is not progressive or is rotated, then we do need to recompress it to deinterlace or unrotate it
+       my $no_recompress = 0;
+       if($no_recompress_file_ext && $progressive && ($video =~ $no_recompress_file_ext)) { $no_recompress = 1; } 
+       
+       # Set our rotate option accordingly to rotate videos taken in portrait mode to landscape
+       my $rotate = `$ffprobe "$video" 2>&1 | grep -e rotate | awk '{print \$3}'`; chomp($rotate);
+       if($rotate ne "") {
+           print "   Detected rotated input video ($rotate). Rotating video to: $video_mp4\n";
+           if($rotate eq "90")  { $handbrake_options .= " --rotate=4"; $rotated = "90"; }
+           if($rotate eq "180") { $handbrake_options .= " --rotate=3"; $rotated = "none"; }
+           $no_recompress = 0; # We have to recompress this video to unrotate it
+       }
+
+        # Set our requantize factor accordingly
+       if(defined $opt_q and ! -f $video_mp4) {
+           print "   Re-quantizing input video content to: $video_mp4\n";
+           $handbrake_options = $handbrake_requantize_options;
+           if(!$progressive) { $handbrake_options = "-q $interlaced_requantize_quality"; }
+           else { $handbrake_options = "-q $progressive_requantize_quality"; }
+       };
+
+       # Set our recompress options accordingly
+       if(defined $opt_z and ! -f $video_mp4 and ! $no_recompress) {
+           print "   Re-compressing input video content to: $video_mp4\n";
+           $handbrake_options = $handbrake_recompress_options;
+           # We want our audio to be passed-through by default, so detect how the audio of the input is encoded, and tell handbrake to make the output match
+           if($opt_v) { print "   $ffmpeg -i \"$video\" 2>&1 | grep \"Audio\" | sed -r -e 's/.*?Audio: (\\S+).*?/\\1/' | tail -n 1\n"; }
+           $AUDIO_CODEC=`$ffmpeg -i "$video" 2>&1 | grep "Audio" | sed -r -e 's/.*?Audio: (\\S+).*?/\\1/' | tail -n 1`; chomp($AUDIO_CODEC);
+           $AUDIO_CODEC =~ s/,$//g;
+           if($AUDIO_CODEC eq "") { print "-W- Unable to extract audio track encoding from input video file: $video\n"; $handbrake_options .= " -E copy"; }
+           else { $handbrake_options .= " -E copy:$AUDIO_CODEC"; }
+       }
+       
+       # Set our de-interlace option accordingly
+       my $deinterlace_option = "";
+       if(!$progressive) { $deinterlace_option = "-d"; }
+
+       # Use HandBrake to requantize/recompress/deinterlace the input video
+       if(! $no_recompress and ! -f $video_mp4) { 
+           my $handbrake_cmd = "$handbrake $deinterlace_option $handbrake_options -i \"$video\" -o \"$video_mp4\" >> \"$tmpfile\" 2>&1";
+           if($opt_v) { print "   $handbrake_cmd\n"; }
+           if(! defined $opt_s and ! -f $video_mp4) { 
+               my $errno = system("$handbrake_cmd");
+               $errno = $errno >> 8;
+               if($errno > 1) {
+                   unlink "$video_mp4";
+                   die "-E- handbrake encountered some errors with exit code $errno\n";
+               }
+               # Restore the metadata from the original video to the intermediate video to preserve the creation_time of the original video metadata in it
+               `$ffmpeg -i \"$video\" -i \"$video_mp4\" -map 1 -map_metadata 0 -c copy -movflags use_metadata_tags \"$video_mp4.fixed.mp4\" >> \"$tmpfile\" 2>&1`;
+               $errno=system("mv \"$video_mp4.fixed.mp4\" \"$video_mp4\" 2>/dev/null");
+               if($errno) { print "-E- Error moving fixed metadata video back to dstfile: $video_mp4.fixed.mp4 -> $video_mp4\n"; }
+           }
+       } else {
+           if($no_recompress) { 
+               print "   Copying input video content to: $video_mp4\n";
+               system("cp \"$video\" \"$video_mp4\"") if(!$opt_s);
+           }
+       }
+       
+       # Push the name of our intermediate video file onto a list of files to delete on exit
+       push(@video_tmp_files, "$video_mp4");
+       # Copy the modification date from the src video to the dst video
+       system("touch \"$video_mp4\" -r \"$video\"");
+       # Update the name of our video file to equal the name of our new intermediate video file name
+       $video = $video_mp4;
+    }
+
+    
+    ####################################################################################################
+    # 2) Group the encoded videos together by their parameters
+    #    We can merge videos that have the same parameters together in a destination video file.
+
+    # Dimensions
+    my $video_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Video"`; chomp($video_stream_info);
+    my $dimensions = "unknown";
+    if($video_stream_info =~ / (\d+x\d+)[,| ]/) { $dimensions = "$1"; }
+    else { print "-W- ffprobe was unable to find dimensions for video: $video\n"; }
+
+    # Video Codec
+    my $video_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Video"`; chomp($video_stream_info);
+    my $video_codec = "unknown";
+    if($video_stream_info =~ / Video: (\S+) /) { $video_codec = "$1"; }
+    else { print "-W- ffprobe was unable to find video codec for video: $video\n"; }
+
+    # Color space
+    my $color_space = "unknown";
+    if($video_stream_info =~ /, (\S+)\(.*?\)/) { $color_space = "$1"; }
+    else { print "-W- ffprobe was unable to find color space for video: $video\n"; }
+
+    # Audio Handler
+    my $audio_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Audio"`; chomp($video_stream_info);
+    my $audio_handler = "unknown";
+    if($audio_stream_info =~ / Hz, ([^,]+), /) { $audio_handler = "$1"; }
+    else { print "-W- ffprobe was unable to find audio handler for video: $video\n"; }
+
+    # Audio Codec
+    my $audio_codec = "unknown";
+    if($audio_stream_info =~ /Audio: (\S+) /) { $audio_codec = "$1"; }
+    else { print "-W- ffprobe was unable to find audio codec for video: $video\n"; }
+
+    # Now create our parameters string
+    my $parameters = "$dimensions.$video_codec.$color_space.$audio_handler.$audio_codec";
+    
+    print "   Adding video $video to be merged into output video file: $opt_o.$parameters.mkv\n" if($opt_v);
+    push @{$merge_videos{"$parameters"}}, $video;
+}
+
+if($opt_v) { 
+    print "\n-> Merge videos hash:\n";
+    print Dumper(\%merge_videos);
+    print "\n";
 }
-close(CHAPTERS);
-print "\n-> Creating the following chapter file for this video:\n";
-$chapter_file_contents = `cat $chapter_file`;
-print "$chapter_file_contents\n";
 
-# Create the mkv video
-print "-> Creating the MKV video file '$opt_o'\n";
+####################################################################################################
+# 3) Create a chapter file for each destination video file
 
-my $cmd = "$mkvmerge --title \"$opt_t\" $output_file_options -o \"$opt_o\"";
-my $video_count = 0;
-foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
-    if($video_count == 0) {
-       $cmd .= " $input_file_options \"$video\"";
-    } else {
-       $cmd .= " $input_file_options + \"$video\"";
+# tmp chapter file used by handbrake when creating mkv, but remove the 0 byte file it creates, we'll create it if we need it
+my $chapter_file = `mktemp`; chomp($chapter_file); unlink "$chapter_file";
+
+foreach my $key (keys %merge_videos) {   
+    # Create the chapters file for each output mkv file.
+    # Make each video file it's own chapter in the MKV file.
+    # Name the chapter with the date and time the video clip was taken (modified date).
+    print "-> Creating $opt_o.$key.mkv with title '$opt_t' from the following video files in last modified date order:\n";
+    open(CHAPTERS,">$chapter_file.$key") || die "-E- Unable to create chapter file: $chapter_file.$key\n";
+    my $chapter_num = 0;
+    my $chapter_timecode = DateTime::Duration->new(years => 2000, hours => 0, minutes => 0, seconds => 0);
+    my $timecode_format = DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N', normalize => 1);
+    foreach my $video (@{$merge_videos{$key}}) {
+       $chapter_num++;
+       my $hour = 0;
+       my $min  = 5;
+       my $sec  = 0;
+       my $mtime_epoch = 0;
+       my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
+       my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
+       if($creation_time) {
+            my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time);
+            if ($brands && $brands =~ /$local_tz_brands/) {
+                $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time);
+            }
+           $date_taken->set_time_zone('local');
+           $mtime_epoch = $date_taken->epoch;
+       } else {
+           $mtime_epoch = stat("$video")->mtime;
+       }
+       my $mdate = epoch_to_date($mtime_epoch);
+       my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`;
+       if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) {
+           $hour = $1;
+           $min = $2;
+           $sec = "$3.$4";
+       }
+       my $timecode = $timecode_format->format_duration($chapter_timecode);
+       print "$mdate $hour:$min:$sec  -> $video \n";
+       print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."=".$timecode."\n";
+       print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."NAME=".$mdate."\n";
+       my $dt = DateTime::Duration->new(years => 2000, hours => $hour, minutes => $min, seconds => $sec);
+       $chapter_timecode = $chapter_timecode + $dt;
     }
-    $video_count++;
+    close(CHAPTERS);
+    print "\n-> Creating the following chapter file $chapter_file.$key for this video:\n";
+    $chapter_file_contents = `cat $chapter_file.$key`;
+    print "$chapter_file_contents\n";
 }
-print "$cmd\n";
-if(! defined $opt_s) { 
-    my $errno = system("$cmd");
-    $errno = $errno >> 8;
-    if($errno > 1) {
-       unlink "$opt_o";
-       die "-E- mkvmerge encountered some errors with exit code $errno\n";
+    
+####################################################################################################
+# 4) Run mkvmerge to merge all of the videos together of the same parameters
+foreach my $key (keys %merge_videos) {
+    my $merge_cmd = "$mkvmerge --title \"$opt_t\" $output_file_options --chapters $chapter_file.$key -o \"$opt_o.$key.mkv\"";
+    my $i = 0;
+    foreach my $video (@{$merge_videos{$key}}) {
+       if($i == 0) { 
+           $merge_cmd .= " $input_file_options \"$video\"";
+       } else {
+           $merge_cmd .= " $input_file_options + \"$video\"";
+       }
+       $i++;
+    }
+    print "\n$merge_cmd\n";
+    if(! defined $opt_s) {
+       #if(-f "$opt_o.$key.mkv") { die "-E- Output video file $opt_o already exists. This is unexpected for key $key!"; }
+       my $errno = system("$merge_cmd");
+       $errno = $errno >> 8;
+       if($errno > 1) {
+           unlink "$opt_o.$key.mkv";
+           die "-E- mkvmerge encountered some errors with exit code $errno\n";
+       }
     }
 }
 
-# Remove the temporary file used for the chapter generation
-if(-e "$tmpfile") { unlink "$tmpfile"; }
+####################################################################################################
+# 5) Remove all temporary files
+if(!$opt_s) { 
+    # Remove the temporary file used for ffmpeg and handbrake output
+    if(-e "$tmpfile") { unlink "$tmpfile"; }
+
+    # Remove the temporary file used for the chapter generation
+    foreach my $key (keys %merge_videos) {   
+       if(-e "$chapter_file.$key") { unlink "$chapter_file.$key"; }
+    }
+
+    # Remove any temporary video files created during the merge process
+    foreach my $video (@video_tmp_files) {
+       if(-e "$video") { unlink "$video"; }
+    }
+}
 
 ####################################################################################################