vaapi_variants.sh 1.8 KB

1234567891011121314151617181920212223242526272829
  1. #!/bin/bash
  2. # vaapi_variants.sh <video> [render node] [start s] [length s] - encodes a piece of the video with hevc_vaapi the way
  3. # we_manager does (mp4, faststart, audio copied, no hvc1 tag) and with a few variants, then scores every result against
  4. # the source with PSNR. A healthy encode at -qp 28 scores about 35 dB or more; a green/pink or black one is far lower.
  5. # The files are kept in ./vaapi-variants/ so they can be played.
  6. video=$1 dev=${2:-/dev/dri/renderD128} start=${3:-0} len=${4:-60}
  7. [ -f "$video" ] || { echo "usage: $0 <video> [render node] [start s] [length s]"; exit 1; }
  8. out=vaapi-variants
  9. mkdir -p "$out"
  10. rm -f "$out"/*.mp4 "$out"/*.mkv "$out"/*.err
  11. run() { # run <name> <ext> <filter> <encoder options before the output> <muxer options>
  12. local name=$1 ext=$2 vf=$3 enc=$4 mux=$5 file="$out/$1.$2" t0=$SECONDS rc psnr
  13. # shellcheck disable=SC2086
  14. ffmpeg -hide_banner -nostdin -y -loglevel error -vaapi_device "$dev" -ss "$start" -t "$len" -i "$video" \
  15. -map 0:v:0 -map '0:a?' -map_metadata 0 -vf "$vf" -c:v hevc_vaapi -qp 28 $enc -c:a copy $mux "$file" 2>"$out/$name.err"
  16. rc=$?
  17. psnr=$(ffmpeg -hide_banner -nostdin -ss "$start" -t "$len" -i "$video" -i "$file" \
  18. -lavfi "[0:v]format=yuv420p[a];[1:v]format=yuv420p[b];[a][b]psnr" -f null - 2>&1 | grep -o 'average:[0-9.inf]*' | tail -1)
  19. printf '%-24s exit=%s %3ss size=%s %s\n' "$name" "$rc" "$((SECONDS - t0))" "$(du -h "$file" 2>/dev/null | cut -f1)" "${psnr:-no psnr}"
  20. }
  21. F="format=nv12,hwupload"
  22. run app-exact-mp4 mp4 "$F" "" "-movflags +faststart"
  23. run mp4-hvc1-tag mp4 "$F" "-tag:v hvc1" "-movflags +faststart"
  24. run mp4-no-faststart mp4 "$F" "" ""
  25. run mkv mkv "$F" "" ""
  26. run mp4-async_depth-1 mp4 "$F" "-async_depth 1" "-movflags +faststart"
  27. echo "kept in $out/, play the files to look at them"