added hardware encoding
This commit is contained in:
parent
56d2144bcc
commit
966970c256
2 changed files with 131 additions and 10 deletions
|
|
@ -18,6 +18,13 @@ let
|
|||
rtspUrl = cfg.rtspUrl;
|
||||
framerate = cfg.framerate;
|
||||
videoSize = cfg.videoSize;
|
||||
preset = cfg.preset;
|
||||
bitrateKbps = cfg.bitrateKbps;
|
||||
maxrateKbps = cfg.maxrateKbps;
|
||||
bufsizeKbps = cfg.bufsizeKbps;
|
||||
useVaapi = cfg.useVaapi;
|
||||
vaapiDevice = cfg.vaapiDevice;
|
||||
vaapiQp = cfg.vaapiQp;
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -47,6 +54,51 @@ in
|
|||
default = "1280x720";
|
||||
description = "Input video size for the webcam stream.";
|
||||
};
|
||||
preset = mkOption {
|
||||
type = types.enum [
|
||||
"ultrafast"
|
||||
"superfast"
|
||||
"veryfast"
|
||||
"faster"
|
||||
"fast"
|
||||
"medium"
|
||||
"slow"
|
||||
"slower"
|
||||
"veryslow"
|
||||
];
|
||||
default = "veryfast";
|
||||
description = "x264 preset used for software encoding.";
|
||||
};
|
||||
bitrateKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Target bitrate in kbps. Set to null for CRF-like unconstrained output.";
|
||||
};
|
||||
maxrateKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Maximum bitrate in kbps. Defaults to bitrateKbps when unset.";
|
||||
};
|
||||
bufsizeKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Rate-control buffer in kbps. Defaults to 2x bitrateKbps when unset.";
|
||||
};
|
||||
useVaapi = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = "Use VAAPI hardware encoding (h264_vaapi) instead of libx264.";
|
||||
};
|
||||
vaapiDevice = mkOption {
|
||||
type = types.str;
|
||||
default = "/dev/dri/renderD128";
|
||||
description = "VAAPI render device path used when useVaapi = true.";
|
||||
};
|
||||
vaapiQp = mkOption {
|
||||
type = types.int;
|
||||
default = 24;
|
||||
description = "VAAPI quality parameter (lower means better quality, higher CPU/bitrate).";
|
||||
};
|
||||
streams = mkOption {
|
||||
type = types.attrsOf (
|
||||
types.submodule {
|
||||
|
|
@ -69,6 +121,51 @@ in
|
|||
default = "1280x720";
|
||||
description = "Input video size for this stream.";
|
||||
};
|
||||
preset = mkOption {
|
||||
type = types.enum [
|
||||
"ultrafast"
|
||||
"superfast"
|
||||
"veryfast"
|
||||
"faster"
|
||||
"fast"
|
||||
"medium"
|
||||
"slow"
|
||||
"slower"
|
||||
"veryslow"
|
||||
];
|
||||
default = "veryfast";
|
||||
description = "x264 preset used for software encoding.";
|
||||
};
|
||||
bitrateKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Target bitrate in kbps. Set to null for unconstrained output.";
|
||||
};
|
||||
maxrateKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Maximum bitrate in kbps. Defaults to bitrateKbps when unset.";
|
||||
};
|
||||
bufsizeKbps = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = "Rate-control buffer in kbps. Defaults to 2x bitrateKbps when unset.";
|
||||
};
|
||||
useVaapi = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = "Use VAAPI hardware encoding (h264_vaapi) instead of libx264.";
|
||||
};
|
||||
vaapiDevice = mkOption {
|
||||
type = types.str;
|
||||
default = "/dev/dri/renderD128";
|
||||
description = "VAAPI render device path used when useVaapi = true.";
|
||||
};
|
||||
vaapiQp = mkOption {
|
||||
type = types.int;
|
||||
default = 24;
|
||||
description = "VAAPI quality parameter for h264_vaapi.";
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
|
|
@ -81,6 +178,27 @@ in
|
|||
config = mkIf cfg.enable {
|
||||
systemd.services = mapAttrs' (
|
||||
streamName: streamCfg:
|
||||
let
|
||||
maxrateKbps =
|
||||
if streamCfg.maxrateKbps != null then streamCfg.maxrateKbps else streamCfg.bitrateKbps;
|
||||
bufsizeKbps =
|
||||
if streamCfg.bufsizeKbps != null then
|
||||
streamCfg.bufsizeKbps
|
||||
else if streamCfg.bitrateKbps != null then
|
||||
streamCfg.bitrateKbps * 2
|
||||
else
|
||||
null;
|
||||
rateControlArgs =
|
||||
if streamCfg.bitrateKbps != null then
|
||||
"-b:v ${toString streamCfg.bitrateKbps}k -maxrate ${toString maxrateKbps}k -bufsize ${toString bufsizeKbps}k"
|
||||
else
|
||||
"";
|
||||
videoCodecArgs =
|
||||
if streamCfg.useVaapi then
|
||||
"-vaapi_device ${streamCfg.vaapiDevice} -vf format=nv12,hwupload -c:v h264_vaapi -qp ${toString streamCfg.vaapiQp}"
|
||||
else
|
||||
"-vcodec libx264 -tune zerolatency -preset ${streamCfg.preset}";
|
||||
in
|
||||
nameValuePair "webcam-rtsp-publisher-${sanitizeName streamName}" {
|
||||
description = "Publish webcam stream '${streamName}' to MediaMTX over RTSP";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
|
|
@ -91,14 +209,7 @@ in
|
|||
Type = "simple";
|
||||
Restart = "always";
|
||||
RestartSec = "2";
|
||||
ExecStart = ''
|
||||
${pkgs.ffmpeg}/bin/ffmpeg \
|
||||
-hide_banner -loglevel warning \
|
||||
-f v4l2 -framerate ${toString streamCfg.framerate} -video_size ${streamCfg.videoSize} \
|
||||
-i ${streamCfg.device} \
|
||||
-vcodec libx264 -tune zerolatency -preset veryfast \
|
||||
-f rtsp ${streamCfg.rtspUrl}
|
||||
'';
|
||||
ExecStart = "${pkgs.ffmpeg}/bin/ffmpeg -hide_banner -loglevel warning -f v4l2 -framerate ${toString streamCfg.framerate} -video_size ${streamCfg.videoSize} -i ${streamCfg.device} ${videoCodecArgs} ${rateControlArgs} -f rtsp ${streamCfg.rtspUrl}";
|
||||
};
|
||||
}
|
||||
) effectiveStreams;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue