From c93f1c1a05a49353b989d2f99f8e4893027af43a Mon Sep 17 00:00:00 2001 From: awkawb Date: Wed, 20 Dec 2023 17:08:11 -0500 Subject: [PATCH] initial commit --- LICENSE | 10 +++++++++ README.md | 1 + default.nix | 21 +++++++++++++++++ transcode-animated-video.sh | 45 +++++++++++++++++++++++++++++++++++++ transcode-organic-video.sh | 44 ++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 default.nix create mode 100644 transcode-animated-video.sh create mode 100644 transcode-organic-video.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..aa38025 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..1e1ef5f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# transcode-video diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..99ea912 --- /dev/null +++ b/default.nix @@ -0,0 +1,21 @@ +{ stdenv, pkgs, ... }: + +let + transcode-animated-video = with pkgs; writeShellApplication { + name = "transcode-animated-video.sh"; + runtimeInputs = [ ffmpeg ]; + text = builtins.readFile ./transcode-animated-video.sh; + }; + + transcode-organic-video = with pkgs; writeShellApplication { + name = "transcode-organic-video.sh"; + runtimeInputs = [ ffmpeg ]; + text = builtins.readFile ./transcode-organic-video.sh; + }; + +in { + environment.systemPackages = [ + transcode-animated-video + transcode-organic-video + ]; +} diff --git a/transcode-animated-video.sh b/transcode-animated-video.sh new file mode 100644 index 0000000..02a8018 --- /dev/null +++ b/transcode-animated-video.sh @@ -0,0 +1,45 @@ +# CLI arguments +input_video=$1 +output_video=$2 + +# Get video pixel format +video_pix_fmt=$(ffprobe -v error \ + -select_streams v:0 \ + -show_entries stream=pix_fmt \ + -of flat "$input_video" \ + | sed 's/\"//g' \ + | awk -F= '{ print $2 }') + +# Set transcode profile based on pixel format +case $video_pix_fmt in + yuv420p10le) + video_profile="main444-10" + ;; + + yuv420p12le) + video_profile="main444-12" + ;; + + yuvj420p) + video_profile="main444-8" + ;; + + *) + video_profile="main444-8" + ;; + +esac + +# Transcode video +ffmpeg -v info -hide_banner -i "$input_video" \ + -progress - \ + -nostats \ + -preset slow \ + -tune animation \ + -c:s copy \ + -c:a copy \ + -c:v libx265 \ + -profile:v $video_profile \ + -crf 36 \ + "$output_video" + diff --git a/transcode-organic-video.sh b/transcode-organic-video.sh new file mode 100644 index 0000000..914f811 --- /dev/null +++ b/transcode-organic-video.sh @@ -0,0 +1,44 @@ +# CLI arguments +input_video=$1 +output_video=$2 + +# Get video pixel format +video_pix_fmt=$(ffprobe -v error \ + -select_streams v:0 \ + -show_entries stream=pix_fmt \ + -of flat "$input_video" \ + | sed 's/\"//g' \ + | awk -F= '{ print $2 }') + +# Set transcode profile based on pixel format +case $video_pix_fmt in + yuv420p10le) + video_profile="main444-10" + ;; + + yuv420p12le) + video_profile="main444-12" + ;; + + yuvj420p) + video_profile="main444-8" + ;; + + *) + video_profile="main444-8" + ;; + +esac + +# Transcode video +ffmpeg -v info -hide_banner -i "$input_video" \ + -progress - \ + -nostats \ + -preset medium \ + -c:s copy \ + -c:a copy \ + -c:v libx265 \ + -profile:v $video_profile \ + -crf 27 \ + "$output_video" +