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:
|
|
|
|
{ profile, preset, tune ? null }:
|
2019-10-24 02:20:38 +02:00
|
|
|
pkgs.writeShellScriptBin "ffmpeg-${name}" ''
|
|
|
|
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
|
|
|
cat <<EOF
|
|
|
|
ffmpeg-<profile>-<preset>-<tunes>-720p <input> <output>
|
|
|
|
|
|
|
|
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" \
|
|
|
|
-filter:v scale=h='min(720\,ih)':w='min(1280\,iw)' \
|
|
|
|
-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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# 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
|
|
|
|
2019-12-20 05:54:26 +01:00
|
|
|
ffmpegs = lib.crossLists ffmpegDescriptive [ profiles presets ];
|
2019-10-24 02:20:38 +02:00
|
|
|
|
2019-12-20 05:54:26 +01:00
|
|
|
ffmpegsTune = lib.crossLists ffmpegDescriptiveTune [ 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
|
|
|
}
|