From 8bfe151a89fb0314ca412e94813e250e03bba714 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Tue, 18 Jun 2024 23:57:06 -0400 Subject: [PATCH] stream improvements --- brc2 | 12 ++++++++++++ ffs | 47 ++++++++++++++++++++++++++++++++++++++--------- pkgs | 1 + 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/brc2 b/brc2 index d6ff1a4..fe5d60f 100644 --- a/brc2 +++ b/brc2 @@ -3291,6 +3291,11 @@ mpvd() { mpva() { mpv --profile=a "$@"; } +# mpv for testing video quality, dont scale. +mpvt() { + mpv --video-unscaled "$@"; + } + # mpv all media files in . or $1 mpvm() { local -a extensions arg @@ -4983,6 +4988,13 @@ ife() { fi } +# decrease filesize without losing any noticeable quality. inspired from +# https://gist.github.com/BlueSwordM/86dfcb6ab38a93a524472a0cbe4c4100 +ffsencode() { + in="$1" + out="$2" + ffmpeg -i "$in" -c:v libsvtav1 -crf 60 -preset 6 -g 60 -svtav1-params tune=0:enable-overlays=1:scd=1:scm=1 -pix_fmt yuv420p10le -c:a copy "$out" +} export BASEFILE_DIR=/a/bin/fai-basefiles diff --git a/ffs b/ffs index 0570787..728b77d 100755 --- a/ffs +++ b/ffs @@ -149,6 +149,9 @@ vps_host=$(dig +timeout=1 +short iankelling.org) if [[ $live_host != "$vps_host" ]] && ip n show 10.2.0.1 | grep . &>/dev/null && \ [[ $(dig +timeout=1 +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]]; then host=127.0.0.1:8000 + if ! pgrep '^icecast2$' >/dev/null; then + sudo systemctl start icecast2 + fi else find_prefix="ssh live.iankelling.org" fi @@ -193,11 +196,37 @@ stream_y=${stream_res#*x} stream_res=$(( stream_x - 4 ))x$(( stream_y - 4)) -# 1000 is a bit blury, 1500 is pretty clear -# note https://livekit.io/webrtc/bitrate-guide (our framerate is lower) +# if hardware acceleration exists, use it to save power & cpu. +if vainfo |& grep -i VAProfileVP9Profile &>/dev/null; then + # 1500 seems almost flawless + bitrate_1080=1500 + + encode_settings=( + -c:v vp9_vaapi + # these options increase compression based on random internet reference. + -bsf:v vp9_raw_reorder,vp9_superframe + ) + # https://trac.ffmpeg.org/wiki/Hardware/VAAPI + global_extra_args=( + -vaapi_device /dev/dri/renderD128 + ) + extra_filter_arg=",format=nv12|vaapi,hwupload" +else + # 1000 is a bit blury, 1500 is pretty clear, 2000 makes scrolling + # adjust much faster, 2500 has marginal improvement on that. + # + # note https://livekit.io/webrtc/bitrate-guide (our framerate is lower) + bitrate_1080=2000 + + encode_settings=( + -vcodec libvpx + -quality realtime + -error-resilient 1 + ) +fi + +bitrate=$(( ( stream_x * stream_y ) / ( (1920*1080) / bitrate_1080 ) )) -# Scale our bitrate to 1500 1080p -bitrate=$(( ( stream_x * stream_y ) / ( (1920*1080) / 1000 ) )) # 8 seems fine. be conservative by going a bit higher. framerate=10 @@ -219,6 +248,8 @@ opts=( -hide_banner -nostats + ${global_extra_args[@]} + # tested for decreasing latency: did not help. # -probesize 32 # tested for warning "Queue input is backward in time". did not help. @@ -267,7 +298,7 @@ opts=( -filter_complex "[0]azmq,volume=precision=fixed: volume=$volume [vol0]; [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1]; [vol0][vol1] amerge=inputs=2; -[2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}[out]" +[2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}${extra_filter_arg}[out]" # An online source says to match a 5 second vid delay, we can do an # audio delay filter: "adelay=5000|5000". However, we already get @@ -284,12 +315,10 @@ opts=( -map '[out]' # video output options - -vcodec libvpx -g $keyframe_interval - -quality realtime + ${encode_settings[@]} -b:v ${bitrate}k - -threads 2 - -error-resilient 1 + ## audio output options -c:a libvorbis diff --git a/pkgs b/pkgs index ae66e34..e2d35b3 100644 --- a/pkgs +++ b/pkgs @@ -324,6 +324,7 @@ p3=( xscreensaver-gl-extra global python3-pygments + vainfo ) # shellcheck disable=SC2034 -- 2.30.2