nixos-config/modules/programs/ffmpeg.nix

142 lines
3.6 KiB
Nix
Raw Normal View History

2019-10-24 02:20:38 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.custom.ffmpeg;
2019-12-20 05:54:26 +01:00
ffmpegTemplate = name:
2021-02-27 21:48:40 +01:00
{ profile, preset, tune ? null, width ? 1280, height ? 720
, resolution ? "720p" }:
2019-10-24 02:20:38 +02:00
pkgs.writeShellScriptBin "ffmpeg-${name}" ''
if [ $# -eq 0 ]
then
cat <<EOF
2021-02-27 21:48:40 +01:00
ffmpeg-<profile>-<preset>-<tunes>-${resolution} <input> <output>
2019-10-24 02:20:38 +02:00
profiles =
"baseline" - Primarily for low-cost applications that require additional data loss robustness
"main" - This profile is used for standard-definition digital TV broadcasts that use the MPEG-4 format as defined in the DVB standard.
"high" - The primary profile for broadcast and disc storage applications, particularly for high-definition television applications
presets =
"ultrafast"
"superfast"
"veryfast"
"faster"
"fast"
"medium"
"slow"
"slower"
"veryslow"
tunes = (optional)
"film" - use for high quality movie content; lowers deblockin
"animation" - good for cartoons; uses higher deblocking and more reference frames
"grain" - preserves the grain structure in old, grainy film material
"stillimage" - good for slideshow-like content
"fastdecode" - allows faster decoding by disabling certain filters
EOF
exit 0
fi
if [ $# -ne 2 ]
then
echo "ffmpeg-${name} <input> <output>"
exit 1
fi
input=$1
output=$2
if [ ! -f "$input" ]
then
echo "input does not exist $input"
exit 1
fi
2020-01-28 11:43:55 +01:00
# do it stereo
# -ac 2
# do it sample rate 44100
# -ar 44100
2019-10-24 02:20:38 +02:00
exec ${pkgs.ffmpeg}/bin/ffmpeg \
-i "$input" \
2021-02-27 21:48:40 +01:00
-filter:v scale=h='min(${toString height}\,ih)':w='min(${
toString width
}\,iw)' \
2019-10-24 02:20:38 +02:00
-vcodec libx264 \
-preset ${preset} \
-profile:v ${profile} \
${optionalString (tune != null) "-tune ${tune}"} \
-acodec aac \
2020-01-28 11:43:55 +01:00
-ac 2 \
-ar 44100 \
2019-10-24 02:20:38 +02:00
"$output" \
-hide_banner
2019-12-20 05:54:26 +01:00
'';
2019-10-24 02:20:38 +02:00
ffmpegDescriptive = profile: preset:
2019-12-20 05:54:26 +01:00
ffmpegTemplate "${profile}-${preset}-720p" { inherit profile preset; };
2019-10-24 02:20:38 +02:00
2021-02-27 21:48:40 +01:00
ffmpegDescriptive1080p = profile: preset:
ffmpegTemplate "${profile}-${preset}-1080p" {
inherit profile preset;
height = 1080;
width = 1920;
resolution = "1080p";
};
2019-10-24 02:20:38 +02:00
ffmpegDescriptiveTune = profile: preset: tune:
2019-12-20 05:54:26 +01:00
ffmpegTemplate "${profile}-${preset}-${tune}-720p" {
inherit profile preset tune;
};
2019-10-24 02:20:38 +02:00
2021-02-27 21:48:40 +01:00
ffmpegDescriptiveTune1080p = profile: preset: tune:
ffmpegTemplate "${profile}-${preset}-${tune}-720p" {
inherit profile preset tune;
height = 1080;
width = 1920;
resolution = "1080p";
};
2019-10-24 02:20:38 +02:00
# https://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Profiles
profiles = [
"baseline"
"main"
"high"
#"high10"
#"high422"
#"high44"
];
presets = [
#"ultrafast"
#"superfast"
#"veryfast"
#"faster"
"fast"
"medium"
"slow"
#"slower"
"veryslow"
];
2019-12-20 05:54:26 +01:00
tunes = [ "film" "animation" "grain" "stillimage" "fastdecode" ];
2019-10-24 02:20:38 +02:00
2021-02-27 21:48:40 +01:00
ffmpegs = (lib.crossLists ffmpegDescriptive [ profiles presets ])
++ (lib.crossLists ffmpegDescriptive1080p [ profiles presets ]);
2019-10-24 02:20:38 +02:00
2021-02-27 21:48:40 +01:00
ffmpegsTune =
(lib.crossLists ffmpegDescriptiveTune [ profiles presets tunes ])
++ (lib.crossLists ffmpegDescriptiveTune1080p [ profiles presets tunes ]);
2019-10-24 02:20:38 +02:00
in {
options.programs.custom.ffmpeg = {
enable = mkEnableOption "enable programs.custom.ffmpeg";
};
2019-12-20 05:54:26 +01:00
config =
mkIf cfg.enable { environment.systemPackages = ffmpegs ++ ffmpegsTune; };
2019-10-24 02:20:38 +02:00
}