impl CameraCapture {
    #[cfg(not(coverage))]
    fn new_ffmpeg_hevc_nvenc(
        dev_label: &str,
        source_profile: CameraSourceProfile,
        capture_profile: (u32, u32, u32),
        output_profile: (u32, u32, u32),
        keyframe_interval: u32,
        preview_tap_path: Option<PathBuf>,
    ) -> anyhow::Result<Self> {
        let bitrate_kbit = env_u32("LESAVKA_CAM_HEVC_KBIT", 3000).max(250);
        let (capture_width, capture_height, capture_fps) = capture_profile;
        let (width, height, fps) = output_profile;
        let fps = fps.max(1);
        let capture_fps = capture_fps.max(1);
        let keyframe_interval = keyframe_interval.max(1);
        let preview_tap_enabled = preview_tap_path.is_some();
        let mut command = Command::new("ffmpeg");
        command
            .arg("-hide_banner")
            .arg("-loglevel")
            .arg(if preview_tap_enabled {
                "quiet".to_string()
            } else {
                std::env::var("LESAVKA_FFMPEG_LOGLEVEL").unwrap_or_else(|_| "warning".into())
            })
            .arg("-nostdin")
            .arg("-fflags")
            .arg("nobuffer")
            .arg("-flags")
            .arg("low_delay")
            .arg("-use_wallclock_as_timestamps")
            .arg("1");

        if dev_label.starts_with("/dev/") {
            command
                .arg("-f")
                .arg("v4l2")
                .arg("-framerate")
                .arg(capture_fps.to_string())
                .arg("-video_size")
                .arg(format!("{capture_width}x{capture_height}"));
            if matches!(
                source_profile,
                CameraSourceProfile::Mjpeg | CameraSourceProfile::AutoDecode
            ) {
                command.arg("-input_format").arg("mjpeg");
            }
            command.arg("-i").arg(dev_label);
        } else if dev_label.starts_with("videotestsrc:") {
            command
                .arg("-f")
                .arg("lavfi")
                .arg("-i")
                .arg(format!(
                    "testsrc2=size={capture_width}x{capture_height}:rate={capture_fps}"
                ));
        } else {
            anyhow::bail!(
                "FFmpeg HEVC NVENC route does not understand camera source '{dev_label}'"
            );
        }

        let video_filter =
            format!("scale={width}:{height}:flags=fast_bilinear,fps={fps},format=nv12");
        let preview_filter = format!(
            "[0:v]scale={width}:{height}:flags=fast_bilinear,fps={fps},split=2[vencsrc][vprevsrc];\
             [vencsrc]format=nv12[vencout];[vprevsrc]format=rgba[vprevout]"
        );
        let bitrate = format!("{bitrate_kbit}k");
        command.arg("-an").arg("-sn").arg("-dn");
        if preview_tap_enabled {
            command
                .arg("-filter_complex")
                .arg(preview_filter)
                .arg("-map")
                .arg("[vencout]");
        } else {
            command.arg("-vf").arg(video_filter);
        }
        command
            .arg("-c:v")
            .arg("hevc_nvenc")
            .arg("-preset")
            .arg("p1")
            .arg("-tune")
            .arg("ll")
            .arg("-rc")
            .arg("cbr")
            .arg("-b:v")
            .arg(&bitrate)
            .arg("-maxrate")
            .arg(&bitrate)
            .arg("-bufsize")
            .arg(&bitrate)
            .arg("-g")
            .arg(keyframe_interval.to_string())
            .arg("-bf")
            .arg("0")
            .arg("-forced-idr")
            .arg("1")
            .arg("-f")
            .arg("hevc")
            .arg("pipe:1");
        if preview_tap_enabled {
            command
                .arg("-map")
                .arg("[vprevout]")
                .arg("-c:v")
                .arg("rawvideo")
                .arg("-pix_fmt")
                .arg("rgba")
                .arg("-f")
                .arg("rawvideo")
                .arg("pipe:2")
                .stderr(Stdio::piped());
        } else {
            command.stderr(Stdio::null());
        }
        command.stdout(Stdio::piped());

        tracing::info!(
            device = dev_label,
            capture_width,
            capture_height,
            capture_fps,
            output_width = width,
            output_height = height,
            output_fps = fps,
            bitrate_kbit,
            keyframe_interval,
            preview_tap = preview_tap_enabled,
            "📸 using FFmpeg hevc_nvenc hardware encoder"
        );

        let mut child = command
            .spawn()
            .context("starting FFmpeg hevc_nvenc camera encoder")?;
        let preview_tap_running = if let Some(path) = preview_tap_path {
            let stderr = child
                .stderr
                .take()
                .context("FFmpeg hevc_nvenc preview stream was not piped")?;
            Some(spawn_ffmpeg_raw_preview_tap(stderr, path, width, height))
        } else {
            None
        };
        let stdout = child
            .stdout
            .take()
            .context("FFmpeg hevc_nvenc stdout was not piped")?;
        let fd = stdout.into_raw_fd();
        let desc = format!(
            "fdsrc fd={fd} blocksize=1048576 ! \
             h265parse config-interval=-1 ! \
             video/x-h265,stream-format=byte-stream,alignment=au ! \
             queue max-size-buffers=30 leaky=downstream ! \
             appsink name=asink emit-signals=true max-buffers=60 drop=true"
        );
        let pipeline: gst::Pipeline = match gst::parse::launch(&desc) {
            Ok(element) => element.downcast::<gst::Pipeline>().expect("not a pipeline"),
            Err(err) => {
                let _ = child.kill();
                return Err(err).context("gst parse_launch(ffmpeg hevc nvenc)");
            }
        };
        let sink: gst_app::AppSink = pipeline
            .by_name("asink")
            .context("appsink element not found for FFmpeg HEVC route")?
            .downcast::<gst_app::AppSink>()
            .expect("appsink down-cast");

        spawn_camera_bus_logger(&pipeline, format!("{dev_label} via ffmpeg hevc_nvenc"));
        if let Err(err) = pipeline.set_state(gst::State::Playing) {
            let _ = pipeline.set_state(gst::State::Null);
            let _ = child.kill();
            return Err(err).context("starting FFmpeg HEVC GStreamer handoff pipeline");
        }

        Ok(Self {
            pipeline,
            sink,
            ffmpeg_child: Some(child),
            preview_tap_running,
            pts_rebaser: crate::live_capture_clock::DurationPacedSourcePtsRebaser::default(),
            frame_duration_us: (1_000_000u64 / u64::from(fps.max(1))).max(1),
        })
    }
}
