feat: Add support for AMD hardware encoding via x265-amd codec

This commit is contained in:
Rémi Heredero 2025-04-27 14:46:02 +02:00
parent 72904f4412
commit 3588d9ad14
Signed by: Klagarge
GPG Key ID: 735B36B074A65F0F

View File

@ -20,13 +20,20 @@ def encode(input_file, codec, remove_source=False):
crf = 26
folder = "h265"
extra_params = ["-crf", str(crf)]
elif codec == "x265-amd":
ffmpeg_codec = "hevc_amf"
folder = "h265"
extra_params = ["-quality", "quality"]
elif codec == "av1":
ffmpeg_codec = "av1_nvenc"
cq = 32
folder = "av1"
extra_params = ["-pix_fmt", "yuv420p", "-preset", "p4", "-cq", str(cq), "-rc", "vbr", "-b:v", "0"]
else:
raise ValueError("Unsupported codec")
raise ValueError(f"Unsupported codec: {codec}")
try:
duration = get_duration(input_file)
@ -52,7 +59,7 @@ def encode(input_file, codec, remove_source=False):
"-y", output_file
]
print(f"\n🎬 Encoding {filename}{folder}/{name}.mkv")
print(f"\n🎬 Encoding {filename}{folder}/{name}.mkv with codec [{ffmpeg_codec}]")
with open(log_file, "w") as log:
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
@ -77,7 +84,6 @@ def encode(input_file, codec, remove_source=False):
if process.returncode != 0:
print(f"\n❌ [Error] FFmpeg failed for {filename}. Check the log: {log_file}")
# Clean broken output
if os.path.exists(output_file):
os.remove(output_file)
else:
@ -105,10 +111,10 @@ def encode_batch(directory, codec, remove_source=False):
continue
def main():
parser = argparse.ArgumentParser(description="Encode video(s) to x265 or AV1 with GPU.")
parser = argparse.ArgumentParser(description="Encode video(s) to x265 or AV1.")
parser.add_argument("input", nargs="?", help="Path to input file")
parser.add_argument("-d", "--directory", help="Path to a directory for batch encoding")
parser.add_argument("--codec", choices=["x265", "av1"], default="x265", help="Codec to use (default: x265)")
parser.add_argument("--codec", choices=["x265", "x265-amd", "av1"], default="x265", help="Codec to use")
parser.add_argument("--remove-source", action="store_true", help="Remove the source file after successful encoding")
args = parser.parse_args()