# 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
 
 
     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";
     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;
 
 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",
        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");
        # 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));
 
 # 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
 
                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");
 
 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 => "$$"});
 
 # 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
 $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