From 4b245ee5bd63274783c1dbd7e6ffb3313bcda52a Mon Sep 17 00:00:00 2001 From: j Date: Sun, 12 Oct 2025 19:51:39 +1300 Subject: [PATCH] Update transcode_bench.py --- transcode_bench.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/transcode_bench.py b/transcode_bench.py index 654200b..2700098 100755 --- a/transcode_bench.py +++ b/transcode_bench.py @@ -25,40 +25,56 @@ class HardwareAcceleration: Returns: (name, encoder, hwaccel_args) """ # Check NVIDIA NVENC - if HardwareAcceleration._check_encoder('h264_nvenc'): + if HardwareAcceleration._test_hardware('h264_nvenc', '-hwaccel cuda -hwaccel_output_format cuda'): return ('NVIDIA NVENC', 'h264_nvenc', '-hwaccel cuda -hwaccel_output_format cuda') # Check Intel QSV - if HardwareAcceleration._check_encoder('h264_qsv'): + if HardwareAcceleration._test_hardware('h264_qsv', '-hwaccel qsv -hwaccel_output_format qsv'): return ('Intel Quick Sync', 'h264_qsv', '-hwaccel qsv -hwaccel_output_format qsv') # Check AMD AMF (Windows/Linux) - if HardwareAcceleration._check_encoder('h264_amf'): + if HardwareAcceleration._test_hardware('h264_amf', ''): return ('AMD AMF', 'h264_amf', '') # Check VideoToolbox (macOS/iOS - ARM) - if HardwareAcceleration._check_encoder('h264_videotoolbox'): + if HardwareAcceleration._test_hardware('h264_videotoolbox', '-hwaccel videotoolbox'): return ('VideoToolbox', 'h264_videotoolbox', '-hwaccel videotoolbox') # Check VA-API (Linux Intel/AMD) - if HardwareAcceleration._check_encoder('h264_vaapi'): + if HardwareAcceleration._test_hardware('h264_vaapi', '-hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128'): return ('VA-API', 'h264_vaapi', '-hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128') # Fallback to software return ('Software (libx264)', 'libx264', '') @staticmethod - def _check_encoder(encoder: str) -> bool: - """Check if FFmpeg supports a specific encoder.""" + def _test_hardware(encoder: str, hwaccel_args: str) -> bool: + """Test if hardware encoder is actually available by attempting to use it.""" try: + # Build a quick test encode command + cmd = ['ffmpeg', '-y', '-hide_banner', '-loglevel', 'error'] + + # Add hardware acceleration args if present + if hwaccel_args: + cmd.extend(hwaccel_args.split()) + + # Generate 1 frame of test video and try to encode it + cmd.extend([ + '-f', 'lavfi', + '-i', 'testsrc2=size=1920x1080:duration=0.1:rate=1', + '-frames:v', '1', + '-c:v', encoder, + '-f', 'null', + '-' if os.name != 'nt' else 'NUL' + ]) + result = subprocess.run( - ['ffmpeg', '-hide_banner', '-encoders'], + cmd, capture_output=True, - text=True, - timeout=5 + timeout=10 ) - return encoder in result.stdout - except (subprocess.TimeoutExpired, FileNotFoundError): + return result.returncode == 0 + except (subprocess.TimeoutExpired, FileNotFoundError, Exception): return False