Improve video convert script.

This commit is contained in:
Tom Alexander 2025-05-10 23:25:16 -04:00
parent 22f9a0efcd
commit 14c5c7d0fd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -6,6 +6,7 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${VIDEO_BITRATE:="1M"} # Only for encoding modes targeting bitrate : ${VIDEO_BITRATE:="1M"} # Only for encoding modes targeting bitrate
: ${AUDIO_BITRATE:="192k"} : ${AUDIO_BITRATE:="192k"}
: ${SPEED:="1"}
############## Setup ######################### ############## Setup #########################
@ -62,6 +63,8 @@ function copy {
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch" "rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
} }
# benchmark vulkan decode:
# ffmpeg -init_hw_device "vulkan=vk:0" -hwaccel vulkan -hwaccel_output_format vulkan -i INPUT -f null - -benchmark
function convert { function convert {
local args=() local args=()
local acceleration_type="$1" # "software" or "hardware" local acceleration_type="$1" # "software" or "hardware"
@ -99,11 +102,23 @@ function convert {
if [ "$acceleration_type" == "software" ]; then if [ "$acceleration_type" == "software" ]; then
true true
elif [ "$acceleration_type" == "hardware" ]; then elif [ "$acceleration_type" == "hardware" ]; then
args+=(-vaapi_device /dev/dri/renderD128) if [ "$codec" == "h264" ]; then
args+=(-init_hw_device vulkan)
elif [ "$codec" == "av1" ]; then
args+=(-vaapi_device /dev/dri/renderD128)
fi
fi fi
args+=(-i "$file_to_cast") args+=(-i "$file_to_cast")
if [ "$SPEED" != "1" ]; then
local audio_speed video_speed
video_speed=$(bc -l <<< "1/${SPEED}")
audio_speed=$(bc -l <<< "${SPEED}/1")
args+=(-filter:v "setpts=${video_speed}*PTS")
args+=(-filter:a "atempo=${audio_speed}")
fi
if [ "$codec" == "h264" ]; then if [ "$codec" == "h264" ]; then
if [ "$acceleration_type" == "software" ]; then if [ "$acceleration_type" == "software" ]; then
args+=(-c:v h264) args+=(-c:v h264)
@ -111,7 +126,7 @@ function convert {
args+=(-b:v "$VIDEO_BITRATE") args+=(-b:v "$VIDEO_BITRATE")
elif [ "$acceleration_type" == "hardware" ]; then elif [ "$acceleration_type" == "hardware" ]; then
args+=(-vf 'format=nv12|vaapi,hwupload') args+=(-vf 'format=nv12|vaapi,hwupload')
args+=(-c:v h264_vaapi) args+=(-c:v h264_vulkan)
args+=(-profile:v high) args+=(-profile:v high)
args+=(-b:v "$VIDEO_BITRATE") args+=(-b:v "$VIDEO_BITRATE")
fi fi
@ -119,12 +134,14 @@ function convert {
if [ "$acceleration_type" == "software" ]; then if [ "$acceleration_type" == "software" ]; then
args+=(-c:v libsvtav1) args+=(-c:v libsvtav1)
args+=(-preset 4) # [0-13] default 10, lower = higher quality / slower encode args+=(-preset 4) # [0-13] default 10, lower = higher quality / slower encode
args+=(-crf 20) # [0-63] default 35, lower = higher quality / larger file # args+=(-crf 20) # [0-63] default 35, lower = higher quality / larger file
# Parameters: https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/Parameters.md # Parameters: https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/Parameters.md
# fast-decode [0-2] default 0 (off), higher = faster decode # fast-decode [0-2] default 0 (off), higher = faster decode
# tune [0-2] default 1, Specifies whether to use PSNR or VQ as the tuning metric [0 = VQ, 1 = PSNR, 2 = SSIM] # tune [0-2] default 1, Specifies whether to use PSNR or VQ as the tuning metric [0 = VQ, 1 = PSNR, 2 = SSIM]
# film-grain-denoise, setting to 0 uses the original frames instead of denoising the film grain # film-grain-denoise, setting to 0 uses the original frames instead of denoising the film grain
args+=(-svtav1-params "fast-decode=1:film-grain-denoise=0") # rc 1 = vbr 2 = cbr
# tbr = average bitrate
args+=(-svtav1-params "fast-decode=1:film-grain-denoise=0:tbr=${VIDEO_BITRATE}:rc=1:passes=2")
elif [ "$acceleration_type" == "hardware" ]; then elif [ "$acceleration_type" == "hardware" ]; then
# -c:v av1_amf -quality quality # -c:v av1_amf -quality quality
args+=(-vf 'format=nv12|vaapi,hwupload') args+=(-vf 'format=nv12|vaapi,hwupload')
@ -206,32 +223,4 @@ function encode_webcam {
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch" "rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
} }
function speed_up_preprocess_vp8 {
local file_to_cast file_to_save
file_to_cast="$1"
file_to_save="$2"
set -x
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
# -strict -2 :: Enable support for experimental codecs like opus.
# -b:v 2M :: Target 2 megabit/s
# -crf 10 :: Target a quality level and adjust bitrate accordingly. This should be preferred, but ideally both should be used.
# Could also use -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"
</dev/null exec ffmpeg \
-i "$file_to_cast" \
-filter:v "setpts=0.66666666*PTS" \
-filter:a "atempo=1.5" \
-c:v vp8 \
-b:v 2M \
-crf 10 \
-bf 0 \
-c:a opus \
-b:a 320k \
-ar 48000 \
-strict -2 \
"$file_to_save"
}
main "${@}" main "${@}"