nixos-config/nixos/modules/services/videoencoder.nix

108 lines
2.4 KiB
Nix
Raw Normal View History

2019-10-24 02:20:38 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.service.videoencoder;
# todo : escape output and input File
2019-12-20 05:54:26 +01:00
createEncoder = tmpFolder: inputFile: outputFile: # sh
''
2019-10-24 02:20:38 +02:00
mkdir -p ${tmpFolder}
rm -rf ${tmpFolder}/*
TMP_FILE=`mktemp --dry-run ${tmpFolder}/XXXXXXXX.${cfg.format}`
if [ ! -f "${outputFile}" ]
then
${pkgs.ffmpeg}/bin/ffmpeg \
-i "${inputFile}" \
-filter:v scale=h='min(720\,ih)':w='min(1280\,iw)' \
-vcodec libx264 \
-preset veryslow \
-profile:v ${cfg.profile} \
${optionalString (cfg.tune != null) "-tune ${cfg.tune}"} \
-acodec aac \
"$TMP_FILE" \
-hide_banner \
&& cp $TMP_FILE "${outputFile}.${cfg.format}" \
&& rm $TMP_FILE
fi
'';
in {
options.service.videoencoder = {
enable = mkEnableOption "enable service.videoencoder";
profile = mkOption {
2019-12-20 05:54:26 +01:00
type = with types; string;
2019-10-24 02:20:38 +02:00
default = "main";
description = ''
-profile:v
'';
};
tune = mkOption {
2019-12-20 05:54:26 +01:00
type = with types;
nullOr (enum [ "film" "animation" "grain" "stillimage" ]);
2019-10-24 02:20:38 +02:00
default = null;
description = ''
-tune
'';
};
format = mkOption {
2019-12-20 05:54:26 +01:00
type = with types; enum [ "mp4" "mkv" ];
2019-10-24 02:20:38 +02:00
default = "mp4";
description = ''
the format
'';
};
fileConfig = mkOption {
2019-12-20 05:54:26 +01:00
type = with types;
listOf (submodule {
options = {
inputFile = mkOption {
# todo make this path
type = with types; string;
description = ''
full path to the inputFile
'';
};
outputFile = mkOption {
type = with types; string;
description = ''
full path to the ouputFile
folder must exist
'';
};
2019-10-24 02:20:38 +02:00
};
2019-12-20 05:54:26 +01:00
});
2019-10-24 02:20:38 +02:00
description = ''
list of files to encode.
'';
};
};
config = mkIf cfg.enable {
systemd.services."videoEncoding" = {
2019-12-20 05:54:26 +01:00
wantedBy = [ "multi-user.target" ];
2019-10-24 02:20:38 +02:00
enable = true;
2019-12-20 05:54:26 +01:00
script = let
myList = map (value:
createEncoder "/tmp/videoencoder" value.inputFile value.outputFile)
cfg.fileConfig;
in ''
set -x
${concatStringsSep "\n" myList}
'';
2019-10-24 02:20:38 +02:00
};
};
}