fix: ensure GPU availability is checked before using hardware acceleration

- Check both preferHardware AND gpuAvailable before using NVENC codecs (h264_nvenc, hevc_nvenc)
- Check gpuAvailable before adding CUDA hwaccel flag for input decoding
- Fix test assertion to check correct call index for CUDA hwaccel verification

This prevents hardware acceleration attempts when GPU is unavailable, addressing code review feedback.
This commit is contained in:
rob_otman 2025-12-17 16:05:25 -07:00
parent db3acff4f7
commit 00931098c1
2 changed files with 5 additions and 4 deletions

View file

@ -867,14 +867,14 @@ export async function convert(
extraArgs.push("-c:v", "libaom-av1");
break;
case "h264":
if (preferHardware) {
if (preferHardware && gpuAvailable) {
extraArgs.push("-c:v", "h264_nvenc");
} else {
extraArgs.push("-c:v", "libx264");
}
break;
case "h265":
if (preferHardware) {
if (preferHardware && gpuAvailable) {
extraArgs.push("-c:v", "hevc_nvenc");
} else {
extraArgs.push("-c:v", "libx265");
@ -893,7 +893,7 @@ export async function convert(
// This only applies if FFMPEG_ARGS doesn't already specify a hardware accelerator
const hasHardwareAccel = ffmpegArgs.includes("-hwaccel");
if (preferHardware && !hasHardwareAccel) {
if (preferHardware && gpuAvailable && !hasHardwareAccel) {
const supportsCuda = await isCudaSupportedCodec(filePath, fileType, execFile);
if (supportsCuda) {
ffmpegArgs.push("-hwaccel", "cuda");

View file

@ -253,7 +253,8 @@ test("does not add CUDA hwaccel for image input when hardware preferred", async
console.log = originalConsoleLog;
expect(calls[0]).not.toEqual(expect.arrayContaining(["-hwaccel", "cuda"]));
// calls[0] is nvidia-smi, calls[1] is ffprobe, calls[2] is ffmpeg
expect(calls[2]).not.toEqual(expect.arrayContaining(["-hwaccel", "cuda"]));
expect(loggedMessage).toBe("stdout: Fake stdout");
delete process.env.FFMPEG_PREFER_HARDWARE;