From eb8eb0bd100a288298370362135991ad4710cc59 Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <alan@pippins.net>
Date: Sun, 11 Aug 2019 15:24:24 -0600
Subject: [PATCH] Updated scripts to support new version of HandBrake

Also updated handling of mp4 videos from new Panasonic camcorder
---
 avchd2h264           |  2 +-
 make_mkv             |  7 ++++++-
 merge_videos_by_day  |  9 +++++++--
 mkv_extract_chapter  |  2 +-
 organize_movies      | 14 +++++++++-----
 organize_videos      |  7 ++++---
 organize_videos.conf |  3 ++-
 7 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/avchd2h264 b/avchd2h264
index d5b1fd9..a869bbf 100755
--- a/avchd2h264
+++ b/avchd2h264
@@ -52,5 +52,5 @@ fi
 
 # 1080p
 QUALITY=25
-HandBrakeCLI -i "$INPUT" -o "$OUTPUT" -f mkv --denoise="weak" --deblock -e x264 -q $QUALITY -x b-adapt=2:rc-lookahead=120:deblock=1,1:subme=10:trellis=2 -v 2 -E $AUDIO_ENC -a 1 -6 dpl2 --strict-anamorphic --crop 0:0:0:0 --preset="High Profile" --decomb
+HandBrakeCLI -i "$INPUT" -o "$OUTPUT" -f mkv --denoise="weak" --deblock -e x264 -q $QUALITY -x b-adapt=2:rc-lookahead=120:deblock=1,1:subme=10:trellis=2 -v 2 -E $AUDIO_ENC -a 1 -6 dpl2 --auto-anamorphic --crop 0:0:0:0 --preset="Very Fast 1080p30" --decomb
 
diff --git a/make_mkv b/make_mkv
index f2c2e53..c769004 100755
--- a/make_mkv
+++ b/make_mkv
@@ -218,6 +218,11 @@ foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
     if($video_stream_info =~ / (\d+x\d+)[,| ]/) { $dimensions = "$1"; }
     else { print "-W- ffprobe was unable to find dimensions 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";
@@ -230,7 +235,7 @@ foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
     else { print "-W- ffprobe was unable to find audio codec for video: $video\n"; }
 
     # Now create our parameters string
-    my $parameters = "$dimensions.$audio_handler.$audio_codec";
+    my $parameters = "$dimensions.$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;
diff --git a/merge_videos_by_day b/merge_videos_by_day
index ec00da9..0737ee1 100755
--- a/merge_videos_by_day
+++ b/merge_videos_by_day
@@ -41,6 +41,7 @@ $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
 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; }
+if(! -x $ffmpeg) { die "-E- Missing required executable for ffmpeg: $ffmpeg\n"; }
 
 my %monthname2month = (
 		       "Jan" => "01",
@@ -175,7 +176,9 @@ foreach $dstfile (sort keys %videos) {
 	print "$cmd\n";
 	if(!defined $opt_k) { 
 	    foreach $video (@{$videos{$dstfile}}) {
-		if(($save_originals) && ($video =~ /\.$originals_file_ext/)) {
+		if($opt_v) { print "   $ffmpeg -i \"$video\" 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
+		my $brands=`$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
+		if(($save_originals) && ($video =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {
 		    print "-> Saving the original video $video\n";
 		    if(index($video, basename(dirname($video))) == -1) { 
 			print("mv $video \"$origpathname/".basename(dirname($video))."_".basename($video)."\n");
@@ -204,7 +207,9 @@ foreach $dstfile (sort keys %videos) {
 	# Remove the individual video files
 	if(!defined $opt_k) { 
 	    foreach $srcvideo (@{$videos{$dstfile}}) {
-		if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/)) {
+		if($opt_v) { print "   $ffmpeg -i \"$srcvideo\" 2>&1 | grep \"compatible_brands\" | tail -n 1\n"; }
+		my $brands=`$ffmpeg -i "$srcvideo" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
+		if(($save_originals) && ($srcvideo =~ /\.$originals_file_ext/) && (!$brands || $brands !~ /$originals_no_copy_brands/)) {		
 		    print "-> Saving the original video $srcvideo to $origpathname\n";
 		    if(index($srcvideo, basename(dirname($srcvideo))) == -1) { 
 			system("mv $srcvideo \"$origpathname/".basename(dirname($srcvideo))."_".basename($srcvideo));
diff --git a/mkv_extract_chapter b/mkv_extract_chapter
index d8825ae..ca33215 100755
--- a/mkv_extract_chapter
+++ b/mkv_extract_chapter
@@ -129,7 +129,7 @@ if(!$progressive) {
 # For each chapter specified on the command line, use ffmpeg to extract a video clip from that chapter
 my @ffmpeg_info = `$ffmpeg -i $opt_i 2>&1`;
 foreach $line (@ffmpeg_info) {
-    if($line =~ /Chapter #\d+\.(\d+): start (\S+), end (\S+)/) {
+    if($line =~ /Chapter #\d+\:(\d+): start (\S+), end (\S+)/) {
 	$chapter = $1;
 	$start = $2;
         if($start > 0) { $start += 1; } # Add some margin to prevent taking a piece of the previous clip
diff --git a/organize_movies b/organize_movies
index f2c9215..f64787b 100755
--- a/organize_movies
+++ b/organize_movies
@@ -65,15 +65,19 @@ sub encode_movies {
 		my $cmd = "HandBrakeCLI";
 		$cmd .= " -i \"$movie\" -o \"$output\"";
 		$cmd .= " -f mkv --loose-anamorphic --denoise=\"weak\" -e x264";
-		$cmd .= " -a 1,1";
+		#$cmd .= " -a 1,1";
+		$cmd .= " -N eng";
+		$cmd .= " --audio-lang-list eng";
+		$cmd .= " --first-audio";
 		$cmd .= " -x b-adapt=2:rc-lookahead=50 -v 2";
 		if($category =~ /Mobile/i || $movie_src_dir =~ /Mobile/i || $movie_dst_dir =~ /Mobile/i) {
-		    $cmd .= " --preset=\"Android Tablet\" -2 -B 256";
+		    $cmd .= " --preset=\"Android 720p30\" -2 -B 256";
 		} else {
 		    $cmd .= " -q $quality";
-		    $cmd .= " -6 6ch";
-		    $cmd .= " -E copy:ac3";
-		    $cmd .= " --preset=\"High Profile\"";
+		    #$cmd .= " -6 6ch";
+		    $cmd .= " -E copy";
+		    $cmd .= " --audio-copy-mask ac3,eac3,truehd,dts,dtshd";
+		    $cmd .= " --preset=\"HQ 1080p30 Surround\"";
 		}
 		print "   $cmd\n";
 		my $errno = system("$cmd > $encode_log 2>&1");
diff --git a/organize_videos b/organize_videos
index ea92012..05edab4 100755
--- a/organize_videos
+++ b/organize_videos
@@ -128,9 +128,10 @@ $video_files_found=`$find_cmd_with_mkv`;
 if(!$video_files_found) { exit 0; }
 
 # Only proceed if no files have changed in the past $cmin minutes
-if($opt_f) { $find_changed_cmd =~ s/cmin \S+ /cmin +0 /; }
-$changed_files_found=`$find_changed_cmd`;
-if($changed_files_found) { exit 0; }
+if(! $opt_f) { 
+  $changed_files_found=`$find_changed_cmd`;
+  if($changed_files_found) { exit 0; }
+}
 
 # Only one instance of this script running at a time
 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
diff --git a/organize_videos.conf b/organize_videos.conf
index aebd225..bac6fb4 100644
--- a/organize_videos.conf
+++ b/organize_videos.conf
@@ -20,6 +20,7 @@ $dstpathname = "/naspool/videos/HomeVideos";
 # Path to move the originals to
 $origpathname = "/naspool/dropbox/Originals";
 $originals_file_ext = qr/(mov|mp4)/i;
+$originals_no_copy_brands = qr/mp42avc1/i; # Don't copy videos from camcorder to Originals because we keep these videos on those cards
 $save_originals = 1;
 
 # Path to a dir (or dirs separated by semis) to watch for videos to move to $srcpathname to be organized
@@ -71,7 +72,7 @@ $video_suffix = "000";
 $movie_file_ext = "-iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.avi\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mpg\" -o -iregex \".*\.mts\" -o -iregex \".*\.m4v\"";
 
 # Which movie file extensions should not be recompressed for merging
-$no_recompress_file_ext = qr/\.(mp4)$/;
+$no_recompress_file_ext = qr/\.(mp4)$/i;
 
 # Video file creation dates must not have changed in the last X minutes to process any of the video files
 # This is done to ensure that all videos from a given upload from a camera have completed prior to looking for videos to merge
-- 
2.34.1