·6 min read
My video compressor picks a GPU encoder from a list, and never asks the GPU
The GPU selector in my video compressor does not ask the GPU anything. It asks FFmpeg which encoders were compiled into the binary, picks a name from that list, and if the list has no match it quietly encodes on the CPU. Reading the code again months after writing it, I found three places where that design can tell the user something untrue.
- Project
- Simple Video Compressor
- Date
- Mar 2026
- Stack
- Electron, FFmpeg, NVENC, AMF
- Commit
- b899b60
How the app picks an encoder
The app is an Electron shell around a bundled ffmpeg.exe. The user chooses a GPU type (CPU, NVIDIA or AMD), a video encoder (x264 or x265 style), a quality method and some options. The main process turns that into an FFmpeg command line. The first commit, 69ffe96, added the NVIDIA and AMD support; the redesign in b899b60 tidied it. The detection is:
function detectHardwareEncoders() {
try {
const result = spawnSync(ffmpegPath, ["-encoders"]);
const output = result.stdout.toString() + result.stderr.toString();
return {
nvenc: {
h264: output.includes("h264_nvenc") ? "h264_nvenc" : null,
hevc: output.includes("hevc_nvenc") ? "hevc_nvenc" : null,
},
amf: {
h264: output.includes("h264_amf") ? "h264_amf" : null,
hevc: output.includes("hevc_amf") ? "hevc_amf" : null,
},
};
} catch {
return { nvenc: {}, amf: {} };
}
}and the choice that uses it:
let encoder = videoEncoder;
if (gpuType === "nvidia") {
encoder = videoEncoder === "libx264" ? (hardware.nvenc.h264 || "libx264") : (hardware.nvenc.hevc || "libx265");
} else if (gpuType === "amd") {
encoder = videoEncoder === "libx264" ? (hardware.amf.h264 || "libx264") : (hardware.amf.hevc || "libx265");
}It is short and it works on the machine I wrote it on. The trouble is in what each line assumes.
Problem 1: a listed encoder is not a usable encoder
ffmpeg -encoders prints the encoders the binary was built with. The bundled FFmpeg is a single build that includes the NVENC and AMF wrappers whether or not the computer has an NVIDIA or AMD card. So on a machine with neither, hardware.nvenc.h264 is still "h264_nvenc". If that user picks NVIDIA, the code passes -c:v h264_nvenc to FFmpeg, which cannot open an encoder session and exits with a non-zero code.
The user chose the GPU option, so asking for the GPU encoder is not wrong. The wrong part is what the code believes: that a non-null name means the hardware is there. A better check would try a tiny encode of a generated test source (a one-second lavfi input to a null output) with the chosen encoder, and only then offer or use it.
Problem 2: the fallback is silent and the ternary is loose
When the build has no matching encoder at all, || "libx264" swaps in the CPU encoder and carries on. The user asked for GPU acceleration, gets CPU speed and is not told. For a tool whose point is speed, that is a visible lie by omission.
The second branch is looser than it reads. For NVIDIA the test is videoEncoder === "libx264", and anything else takes the HEVC branch. Today the UI only offers two encoders, so the two readings agree. If a third encoder were added, such as AV1, it would be routed to hevc_nvenc or libx265 without any error.
Problem 3: two vocabularies behind one set of controls
The UI shows one quality control and one preset list. FFmpeg's software and hardware encoders do not share a vocabulary for either, and the code translates:
if (isHardware) {
const hwPresets = ["default", "slow", "medium", "fast", "hp", "hq", "bd", "ll", "llhq", "llhp", "lossless"];
ffmpegArgs.push("-preset", hwPresets.includes(preset) ? preset : "default");
if (method === "CRF") ffmpegArgs.push("-cq", safeCRF);
else ffmpegArgs.push("-b:v", `${safeBitrate}k`, "-maxrate", `${Math.floor(safeBitrate * 1.1)}k`, "-bufsize", `${safeBitrate * 2}k`);
} else {
if (method === "CRF") ffmpegArgs.push("-crf", safeCRF);
else ffmpegArgs.push("-b:v", `${safeBitrate}k`);
if (preset) ffmpegArgs.push("-preset", preset);
}The CRF slider becomes -cq for hardware and -crf for software. A software-only preset such as veryslow is not in hwPresets, so it quietly becomes default. That is a reasonable guess, but the same slider position does not mean the same quality on the two encoders; a constant-quality number on NVENC is not comparable with a CRF number on x264, and the UI does not say so. For bitrate mode, the hardware path adds a -maxrate at 1.1 times the target and a -bufsize at twice the target, while the software path only sets -b:v. The software path was not given the same treatment, so the two bitrate modes are configured differently.
What the user sees when it fails
If FFmpeg cannot start the encoder, the close handler runs with a non-zero code and the renderer shows:
event.reply("compression-error", "FFmpeg exited with code " + code);The real reason, for example that the encoder could not be opened, is on FFmpeg's stderr. The code reads stderr for progress, matching time= and size=, and throws everything else away. So the most useful sentence FFmpeg printed is exactly the one that is discarded.
A related detail in the progress code is worth keeping because it is correct. The estimate is the wall-clock time elapsed divided by the media time encoded so far, multiplied by the total duration, minus the elapsed time:
const remaining = Math.max(0, (elapsed / currentTime) * duration - elapsed);That tracks real throughput, so it works the same for a fast NVENC encode and a slow x265 one, as long as currentTime is not zero. The if (timeMatch && duration) guard covers a missing duration, though a first progress line reporting time=00:00:00.00 would divide by zero and give Infinity, which Math.max leaves as Infinity. I have not hit it, and I have not checked what the UI prints in that case.
What I would change
I have not run this on both vendors' hardware for this write-up, so these are corrections from reading the code, not from a bug report:
- Probe the chosen hardware encoder with a one-second test encode and cache the result per run, instead of trusting
-encoders. - Tell the user when a fallback happened, in the progress text.
- Keep the last few lines of FFmpeg's stderr and include them in the error message.
- Route on an explicit encoder table instead of a ternary with an implicit else.
The general lesson is about capability detection. A name appearing in a list tells you the software supports something. Only trying the operation tells you the machine does, and when a feature can fail for reasons the user cannot see, the failure text has to carry the reason or the feature will look broken rather than unsupported.
#electron#ffmpeg#nvenc#error-handling
Keep reading
Have a project in mind?