Explorar o código

Set a maximum size for x265 encoding, at least until CPUs are faster or hardware support for x265 is broader
Define widths in terms of lines and let the computer do the math

Zachary Kotlarek %!s(int64=9) %!d(string=hai) anos
pai
achega
d77eadf027
Modificáronse 1 ficheiros con 19 adicións e 14 borrados
  1. 19 14
      encode.pl

+ 19 - 14
encode.pl

@@ -32,7 +32,8 @@ my %ENCODER_OPTS      = (
 );
 );
 
 
 # Applicaton configuration
 # Applicaton configuration
-my $HD_WIDTH        = 1350;
+my $X265_MAX_WIDTH = 1080 * (16 / 9) * 1.1;
+my $HD_WIDTH       = 720 *  (16 / 9) * 1.1;
 my $MIN_VIDEO_WIDTH = 100;
 my $MIN_VIDEO_WIDTH = 100;
 my $MAX_CROP_DIFF   = .1;
 my $MAX_CROP_DIFF   = .1;
 my $MAX_DURA_DIFF   = 5;
 my $MAX_DURA_DIFF   = 5;
@@ -127,13 +128,15 @@ if ($ENV{'HEIGHT'}) {
 	if (!($ENV{'HEIGHT'} =~ /^\d+$/)) {
 	if (!($ENV{'HEIGHT'} =~ /^\d+$/)) {
 		die($0 . ': Invalid HEIGHT: ' . $ENV{'HEIGHT'} . "\n");
 		die($0 . ': Invalid HEIGHT: ' . $ENV{'HEIGHT'} . "\n");
 	}
 	}
-	push(@video_params, '--maxHeight', $ENV{'HEIGHT'});
+	$HEIGHT = $ENV{'HEIGHT'};
+	push(@video_params, '--maxHeight', $HEIGHT);
 }
 }
 if ($ENV{'WIDTH'}) {
 if ($ENV{'WIDTH'}) {
 	if (!($ENV{'WIDTH'} =~ /^\d+$/)) {
 	if (!($ENV{'WIDTH'} =~ /^\d+$/)) {
 		die($0 . ': Invalid WIDTH: ' . $ENV{'WIDTH'} . "\n");
 		die($0 . ': Invalid WIDTH: ' . $ENV{'WIDTH'} . "\n");
 	}
 	}
-	push(@video_params, '--maxWidth', $ENV{'WIDTH'});
+	$WIDTH = $ENV{'WIDTH'};
+	push(@video_params, '--maxWidth', $WIDTH);
 }
 }
 
 
 # Allow autocrop disable
 # Allow autocrop disable
@@ -153,7 +156,6 @@ if ($ENV{'ENCODER'}) {
 	}
 	}
 	$VIDEO_ENCODER = $ENV{'ENCODER'};
 	$VIDEO_ENCODER = $ENV{'ENCODER'};
 }
 }
-push(@video_params, '--encoder', $VIDEO_ENCODER, @{ $ENCODER_OPTS{$VIDEO_ENCODER} });
 
 
 # Additional arguments for HandBrake, to allow options not supported directly by this script
 # Additional arguments for HandBrake, to allow options not supported directly by this script
 # Split on spaces; if you need spaces you'll have to work out something else
 # Split on spaces; if you need spaces you'll have to work out something else
@@ -255,18 +257,12 @@ if ($OUT_DIR) {
 	$out_file = $OUT_DIR . '/' . basename($out_file);
 	$out_file = $OUT_DIR . '/' . basename($out_file);
 }
 }
 
 
-# Keep copies of our defaults so we can reset between tracks
-my $format_default = $FORMAT;
-
 # Encode each title
 # Encode each title
 foreach my $title (keys(%titles)) {
 foreach my $title (keys(%titles)) {
 	if ($DEBUG) {
 	if ($DEBUG) {
 		print STDERR 'Setting options for title: ' . $title . "\n";
 		print STDERR 'Setting options for title: ' . $title . "\n";
 	}
 	}
 
 
-	# Reset
-	$FORMAT = $format_default;
-
 	# Select a title
 	# Select a title
 	my $scan = $titles{$title};
 	my $scan = $titles{$title};
 
 
@@ -300,6 +296,13 @@ foreach my $title (keys(%titles)) {
 		$title_quality = $HD_QUALITY;
 		$title_quality = $HD_QUALITY;
 	}
 	}
 
 
+	# Fall back to x264 if the video is larger than $X265_MAX_WIDTH
+	# Even x86 systems have trouble with x265 @ 4k and hardware support is (as of 2017) rare
+	my $encoder = $VIDEO_ENCODER;
+	if ($scan->{'size'}[0] > $X265_MAX_WIDTH && (!defined($WIDTH) || $WIDTH > $X265_MAX_WIDTH)) {
+		$encoder = 'x264';
+	}
+
 	# Detect unlikely autocrop values
 	# Detect unlikely autocrop values
 	my $bad_crop = 0;
 	my $bad_crop = 0;
 	if (   abs($scan->{'crop'}[0] - $scan->{'crop'}[1]) > $scan->{'size'}[0] * $MAX_CROP_DIFF
 	if (   abs($scan->{'crop'}[0] - $scan->{'crop'}[1]) > $scan->{'size'}[0] * $MAX_CROP_DIFF
@@ -319,9 +322,10 @@ foreach my $title (keys(%titles)) {
 	}
 	}
 
 
 	# Force MKV muxing if the output contains PGS subtitles (there's no support in the MP4 muxer)
 	# Force MKV muxing if the output contains PGS subtitles (there's no support in the MP4 muxer)
+	my $format = $FORMAT;
 	foreach my $track (values(%{ $scan->{'subtitle_selected'} })) {
 	foreach my $track (values(%{ $scan->{'subtitle_selected'} })) {
 		if ($track->{'type'} eq 'PGS') {
 		if ($track->{'type'} eq 'PGS') {
-			$FORMAT = 'av_mkv';
+			$format = 'av_mkv';
 			last;
 			last;
 		}
 		}
 	}
 	}
@@ -329,7 +333,7 @@ foreach my $title (keys(%titles)) {
 	# Force MKV muxing if the output contains DTS audio (technically MP4 supports it but QuickTime hates it)
 	# Force MKV muxing if the output contains DTS audio (technically MP4 supports it but QuickTime hates it)
 	foreach my $track (values(%{ $scan->{'audio_selected'} })) {
 	foreach my $track (values(%{ $scan->{'audio_selected'} })) {
 		if ($track->{'codec'} =~ /DTS/i) {
 		if ($track->{'codec'} =~ /DTS/i) {
-			$FORMAT = 'av_mkv';
+			$format = 'av_mkv';
 			last;
 			last;
 		}
 		}
 	}
 	}
@@ -337,7 +341,7 @@ foreach my $title (keys(%titles)) {
 	# Select a file name extension that matches the format
 	# Select a file name extension that matches the format
 	my $title_out_file = $out_file;
 	my $title_out_file = $out_file;
 	$title_out_file =~ s/\.(?:\w{2,4}|dvdmedia)$//i;
 	$title_out_file =~ s/\.(?:\w{2,4}|dvdmedia)$//i;
-	if ($FORMAT eq 'av_mp4') {
+	if ($format eq 'av_mp4') {
 		$title_out_file .= '.m4v';
 		$title_out_file .= '.m4v';
 	} else {
 	} else {
 		$title_out_file .= '.mkv';
 		$title_out_file .= '.mkv';
@@ -359,9 +363,10 @@ foreach my $title (keys(%titles)) {
 	push(@args, '--title',   $title);
 	push(@args, '--title',   $title);
 	push(@args, '--input',   $in_file);
 	push(@args, '--input',   $in_file);
 	push(@args, '--output',  $title_out_file);
 	push(@args, '--output',  $title_out_file);
-	push(@args, '--format',  $FORMAT);
+	push(@args, '--format',  $format);
 	push(@args, '--quality', $title_quality);
 	push(@args, '--quality', $title_quality);
 	push(@args, '--crop',    join(':', @{ $scan->{'crop'} }));
 	push(@args, '--crop',    join(':', @{ $scan->{'crop'} }));
+	push(@args, '--encoder', $encoder, @{ $ENCODER_OPTS{$encoder} });
 	push(@args, @video_params);
 	push(@args, @video_params);
 
 
 	# Include subtitles if available
 	# Include subtitles if available