video_optimize_check.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. # video_optimize_check.sh <vm> <we_manager dist/linux-unpacked | .deb> [screen] - we_manager's "Optimize video" on a VM, through the
  3. # real app over DevTools (cdp.py): a generated 1920x1080 non-HEVC video in an extra library folder (the entry is a local one, Steam's
  4. # folders are off limits) is converted to H.265, then scaled down in fit and fill mode, a conversion that has nothing to do is
  5. # refused, a discarded result leaves the file as it was, and the engine plays the converted file. The encoder is whatever works
  6. # in the guest (GPU encoders, else libx265); without any encoder the app downloads its ffmpeg build, a download that fails is a
  7. # FAIL (a platform the app has no build for is a SKIP). FFmpeg's own checks (size, codec) use the app's downloaded ffmpeg and
  8. # ffprobe when it has them, else the system's. The app runs in English. Prints one PASS|FAIL|SKIP line per check.
  9. # FAKE_FFMPEG=1 (default, 0 turns it off): the app's ffmpeg download in a guest without an encoder comes from fake_github.py on the host (wem_lib.sh wem_ffmpeg_fake_start), needs the cache in ~/.cache/lwe-release-prep.
  10. # WEM_LAUNCH and WEM_FLAGS are added to the app's start line like in the other we_manager scripts.
  11. HERE=$(cd "$(dirname "$0")" && pwd)
  12. vm=$1 app=$2 screen=${3:-Virtual-1}
  13. sshv() { "$HERE/linuxvm.sh" ssh "$vm" "$@" </dev/null; }
  14. status=0
  15. pass() { echo "PASS $1"; }
  16. fail() { echo "FAIL $1"; status=1; }
  17. skip() { echo "SKIP $1"; }
  18. CDP_TIMEOUT=${CDP_TIMEOUT:-300}
  19. . "$HERE/wem_lib.sh"
  20. ID=900000001
  21. sshv 'mkdir -p ~/wem; pkill -f "[w]e-manager"; pkill -x linux-wallpaper; pkill -x python3; rm -rf ~/lwe-video-lib; true'
  22. wem_wait_gone
  23. wem_install || exit 1
  24. wem_steam_lib
  25. wem_engine_fallback
  26. FAKE_FFMPEG=${FAKE_FFMPEG:-1}
  27. fake_work=
  28. if [ "$FAKE_FFMPEG" = 1 ]; then
  29. fake_work=$(mktemp -d)
  30. if wem_ffmpeg_fake_start "$fake_work"; then
  31. trap 'wem_ffmpeg_fake_stop; rm -rf "$fake_work"' EXIT
  32. wem_start_line="env NODE_EXTRA_CA_CERTS=\$HOME/wem/fake-ca.pem $wem_start_line"
  33. else
  34. echo " no cached ffmpeg or the fake release did not start, the app uses the guest's own or the real download"
  35. rm -rf "$fake_work"; fake_work=
  36. wem_ffmpeg_seed
  37. fi
  38. else
  39. wem_ffmpeg_seed
  40. fi
  41. wem_run || exit 1
  42. HOME_DIR=$(sshv 'echo $HOME')
  43. LIB=$HOME_DIR/lwe-video-lib
  44. stop_app() { cdp 'window.electronAPI.lwe.killAll()' >/dev/null; sshv 'pkill -f "[w]e-manager"; pkill -x python3; rm -rf ~/lwe-video-lib; true'; }
  45. cdp "window.electronAPI.config.setLanguage('en')" >/dev/null
  46. settings() {
  47. cdp "window.electronAPI.videoTools.setSettings({backupOriginal: $1, encoder: 'auto', crf: 26, scaleTo: '$2', scaleMode: 'fit'})" >/dev/null
  48. }
  49. encoder_list() { cdp 'window.electronAPI.videoTools.status().then(s => s.encoders.map(e => e.encoder).join(String.fromCharCode(43)))'; }
  50. probe_info() { cdp "window.electronAPI.videoTools.probe('${1:-$ID}').then(p => p.info.codec + ',' + p.info.width + ',' + p.info.height)"; }
  51. encoders=$(encoder_list)
  52. if ! [[ $encoders =~ ^\"[a-z0-9_+]*\"$ ]]; then
  53. fail "the app does not answer: $encoders $(sshv 'tail -3 ~/wem/app.log')"; stop_app; exit 1
  54. fi
  55. if [ "$encoders" = '""' ]; then
  56. if [ "$(cdp 'window.electronAPI.videoTools.status().then(s => s.canDownload)')" != true ]; then
  57. skip "no H.265 encoder here and the app has no ffmpeg build for this platform"; stop_app; exit 0
  58. fi
  59. if [ -n "$fake_work" ]; then
  60. echo " no encoder in the guest, downloading the app's ffmpeg from the fake release on the host"
  61. else
  62. echo " no encoder in the guest, downloading the app's ffmpeg"
  63. fi
  64. dl=$(CDP_TIMEOUT=900 cdp 'window.electronAPI.videoTools.download()')
  65. if [ -n "$fake_work" ]; then
  66. grep -q 'ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz' "$fake_work/fake.log" && pass "ffmpeg download served by the fake release" \
  67. || fail "the ffmpeg download did not come from the fake release: $(tail -3 "$fake_work/fake.log")"
  68. fi
  69. wem_ffmpeg_keep
  70. encoders=$(encoder_list)
  71. if [ "$encoders" = '""' ] || ! [[ $encoders =~ ^\"[a-z0-9_+]+\"$ ]]; then
  72. fail "no H.265 encoder after the ffmpeg download: $dl $encoders"; stop_app; exit 1
  73. fi
  74. fi
  75. pass "H.265 encoders: $encoders"
  76. read -r FFMPEG FFPROBE < <(sshv '. ~/sessenv; d=${XDG_DATA_HOME:-$HOME/.local/share}/we-manager/ffmpeg; if [ -x $d/ffmpeg ] && [ -x $d/ffprobe ]; then echo "$d/ffmpeg $d/ffprobe"; else echo "$(command -v ffmpeg) $(command -v ffprobe)"; fi')
  77. if [ -z "$FFMPEG" ] || [ -z "$FFPROBE" ]; then fail "no ffmpeg and ffprobe pair in the guest"; stop_app; exit 1; fi
  78. gen_video() {
  79. sshv "mkdir -p $LIB/$1 && cd $LIB/$1 &&
  80. { $FFMPEG -y -v error -f lavfi -i testsrc2=size=1920x1080:rate=30 -t 3 -c:v libx264 -crf 10 -pix_fmt yuv420p vid.mp4 ||
  81. $FFMPEG -y -v error -f lavfi -i testsrc2=size=1920x1080:rate=30 -t 3 -c:v mpeg4 -b:v 8M -pix_fmt yuv420p vid.mp4; } &&
  82. $FFMPEG -y -v error -i vid.mp4 -frames:v 1 preview.jpg &&
  83. echo '{\"title\":\"$2\",\"type\":\"video\",\"file\":\"vid.mp4\",\"preview\":\"preview.jpg\",\"tags\":[]}' > project.json"
  84. }
  85. gen_video "$ID" vidtest || { fail "could not generate the test video"; stop_app; exit 1; }
  86. vprobe_of() { sshv "$FFPROBE -v error -select_streams v:0 -show_entries stream=codec_name,width,height -of csv=p=0 $LIB/${1:-$ID}/vid.mp4"; }
  87. expect_video() {
  88. local got; got=$(vprobe_of "${3:-$ID}")
  89. [ "$got" = "$1" ] && pass "$2 ($got)" || fail "$2: wanted $1, the file is $got"
  90. }
  91. originals() { sshv "ls $LIB/${1:-$ID} | grep -c '(original'"; }
  92. temps() { sshv "ls -A $LIB/${1:-$ID} | grep -c optimizing"; }
  93. optimize() { cdp "window.electronAPI.videoTools.start('$ID', $1)"; }
  94. finish() { cdp "window.electronAPI.videoTools.finish('$ID', $1)" >/dev/null; }
  95. settings true none
  96. cdp "window.electronAPI.config.setExtraLibraries([{path: '$LIB', name: 'vidtest'}])" >/dev/null
  97. cdp 'window.electronAPI.library.scan()' >/dev/null
  98. info=$(probe_info)
  99. case $info in
  100. *Error*|'') fail "the app can't probe the test video: $info"; stop_app; exit 1 ;;
  101. esac
  102. [ "$info" != '"hevc,1920,1080"' ] && [[ $info == *,1920,1080* ]] && pass "source probed: $info" || fail "unexpected source: $info"
  103. out=$(optimize "{encoder: 'auto'}")
  104. if [[ $out == *newSize* ]]; then pass "H.265 conversion ran: $out"; else fail "conversion failed: $out"; fi
  105. finish true
  106. expect_video hevc,1920,1080 "converted file is H.265 at the original size"
  107. tag=$(sshv "$FFPROBE -v error -select_streams v:0 -show_entries stream=codec_tag_string -of csv=p=0 $LIB/$ID/vid.mp4")
  108. [ "$tag" = hvc1 ] || [ "$tag" = hev1 ] && pass "the mp4 carries a usable H.265 tag ($tag)" || fail "unexpected H.265 tag in the mp4: $tag"
  109. [ "$(originals)" -ge 1 ] && pass "a copy of the original was kept next to the video" || fail "no (original) copy after replacing the video"
  110. orig=$(sshv "$FFPROBE -v error -select_streams v:0 -show_entries stream=codec_name,width,height -of csv=p=0 \"$LIB/$ID/vid (original).mp4\"")
  111. [[ $orig == *,1920,1080 ]] && [ "$orig" != hevc,1920,1080 ] && pass "the copy is the untouched source ($orig)" || fail "the (original) copy is not the source: $orig"
  112. [ "$(temps)" = 0 ] && pass "no temp file left behind" || fail "temp file left in the folder"
  113. out=$(optimize "{encoder: 'auto', scale: {width: 1280, height: 720, mode: 'fit'}}")
  114. [[ $out == *newSize* ]] && pass "downscale to 1280x720 (fit) ran" || fail "downscale failed: $out"
  115. finish true
  116. expect_video hevc,1280,720 "fit downscale gives 1280x720 H.265"
  117. out=$(optimize "{encoder: 'auto', scale: {width: 640, height: 640, mode: 'fill'}}")
  118. [[ $out == *newSize* ]] && pass "downscale to 640x640 (fill) ran" || fail "fill downscale failed: $out"
  119. finish true
  120. expect_video hevc,640,640 "fill downscale crops to 640x640 H.265"
  121. out=$(optimize "{encoder: 'auto'}")
  122. if [[ $out == *newSize* ]]; then fail "an H.265 file with nothing to change was converted again"
  123. elif [[ $out == *"Nothing to do"* ]]; then pass "nothing to do on an H.265 file is refused"
  124. else fail "an H.265 file with nothing to change was not refused the expected way: $out"; fi
  125. out=$(optimize "{encoder: 'auto', scale: {width: 320, height: 180, mode: 'fit'}}")
  126. if [[ $out == *newSize* ]] && [ "$(temps)" -ge 1 ]; then
  127. finish false
  128. expect_video hevc,640,640 "a discarded result leaves the file alone"
  129. [ "$(temps)" = 0 ] && pass "discard removes the temp file" || fail "discard left the temp file"
  130. else
  131. fail "the 320x180 conversion to discard did not stage a result: $out"
  132. fi
  133. after=$(probe_info)
  134. [ "$after" = '"hevc,640,640"' ] && pass "the app reads the new file: $after" || fail "the app still sees the old file: $after"
  135. vdur() { sshv "$FFPROBE -v error -show_entries format=duration -of csv=p=0 $LIB/$ID/vid.mp4"; }
  136. dur_near() {
  137. local got; got=$(vdur)
  138. awk -v g="$got" -v w="$1" -v t="$2" 'BEGIN { d = g - w; if (d < 0) d = -d; exit !(d <= t) }' \
  139. && pass "$3 (${got}s)" || fail "$3: wanted ${1}s, the file is ${got}s"
  140. }
  141. dur_near 3 0.3 "the converted file is still about 3 s long"
  142. settings false none
  143. copies=$(originals)
  144. out=$(optimize "{encoder: 'auto', cut: {start: 0.5, end: 2.5}}")
  145. [[ $out == *newSize* ]] && pass "cut 0.5-2.5 s (re-encoded) ran" || fail "cut failed: $out"
  146. finish true
  147. dur_near 2 0.4 "the cut file is about 2 s long"
  148. expect_video hevc,640,640 "the cut keeps codec and size"
  149. out=$(optimize "{encoder: 'auto', cut: {start: 0, end: 1, copy: true}}")
  150. [[ $out == *newSize* ]] && pass "cut 0-1 s (stream copy) ran" || fail "copy cut failed: $out"
  151. finish true
  152. dur_near 1 0.6 "the stream-copy cut is about 1 s long"
  153. expect_video hevc,640,640 "the stream-copy cut keeps codec and size"
  154. [ "$(originals)" = "$copies" ] && pass "no copy of the original with the backup switched off" || fail "a copy was made although the backup is off"
  155. settings true none
  156. out=$(optimize "{encoder: 'auto', force: true}")
  157. [[ $out == *newSize* ]] && pass "encoding an H.265 file again works with force" || fail "force re-encode failed: $out"
  158. finish true
  159. expect_video hevc,640,640 "the forced re-encode keeps codec and size"
  160. ID2=900000002
  161. gen_video "$ID2" quicktest || { fail "could not generate the quick optimize test video"; stop_app; exit 1; }
  162. cdp 'window.electronAPI.library.scan()' >/dev/null
  163. settings true 4k
  164. res=$(cdp "window.electronAPI.videoTools.quick(['$ID2']).then(r => r.optimized + ',' + r.lines.length)")
  165. [ "$res" = '"1,0"' ] && pass "quick optimize converted the video ($res)" || fail "quick optimize: wanted 1 optimized and no lines, got $res"
  166. expect_video hevc,1920,1080 "a 1080p video stays 1080p with a 4K target" "$ID2"
  167. [ "$(originals "$ID2")" -ge 1 ] && pass "quick optimize kept a copy of the original" || fail "quick optimize left no (original) copy"
  168. res=$(cdp "window.electronAPI.videoTools.quick(['$ID2']).then(r => r.optimized + ',' + r.lines.map(l => l.kind + ':' + l.id).join(String.fromCharCode(124)))")
  169. [ "$res" = '"0,skipped:'$ID2'"' ] && pass "an H.265 video is listed as skipped ($res)" || fail "quick optimize: the H.265 video should be skipped with a reason, got $res"
  170. settings false 720p
  171. res=$(cdp "window.electronAPI.videoTools.quick(['$ID2']).then(r => r.optimized + ',' + r.lines.length)")
  172. [ "$res" = '"1,0"' ] && pass "quick optimize scales an H.265 video down ($res)" || fail "quick optimize with a 720p target: got $res"
  173. expect_video hevc,1280,720 "quick optimize scaled to 1280x720" "$ID2"
  174. settings true none
  175. cdp "window.electronAPI.lwe.launch('$LIB/$ID', {screen: '$screen'})" >/dev/null; sleep 15
  176. engine_cmd() { sshv 'pgrep -af "/[l]inux-wallpaperengine(\.bin)? " | head -1'; }
  177. if [[ $(engine_cmd) == *"$LIB/$ID"* ]]; then
  178. sleep 10
  179. [[ $(engine_cmd) == *"$LIB/$ID"* ]] && pass "the engine plays the converted video" || fail "the engine quit on the converted video: $(sshv 'tail -3 ~/wem/app.log')"
  180. else
  181. fail "the engine does not run the converted video: $(sshv 'tail -3 ~/wem/app.log')"
  182. fi
  183. stop_app
  184. exit $status