diff options
Diffstat (limited to 'examples/tools/test-video.sh')
| -rwxr-xr-x | examples/tools/test-video.sh | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/examples/tools/test-video.sh b/examples/tools/test-video.sh new file mode 100755 index 0000000..74f7d72 --- /dev/null +++ b/examples/tools/test-video.sh @@ -0,0 +1,134 @@ +#!/bin/bash +# desktop-assistant tool check: video.sh. +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# test-video.sh: local clips made with espeak-ng and ffmpeg go through the +# real whisper (CPU, ~10 s each); yt-dlp is a stub driven by STUB_* variables, +# so URL cases never touch the network. Output and temp folders are +# throwaway. Prints "ok" or one FAIL line per broken case. + +set -u +here=$(cd "$(dirname "$0")" && pwd) +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +export ASSISTANT_DIR=$tmp/out XDG_RUNTIME_DIR=$tmp/run VIDEO_NO_OPEN=1 +mkdir -p "$tmp/bin" "$XDG_RUNTIME_DIR" + +cat > "$tmp/bin/yt-dlp" <<'EOF' +#!/bin/bash +[ -n "${STUB_FAIL:-}" ] && { echo "ERROR: $STUB_FAIL" >&2; exit 1; } +out=""; prev="" +for a in "$@"; do [ "$prev" = -o ] && out=$a; prev=$a; done +case " $* " in + *" --print "*) printf '%s\n' "$STUB_META" ;; + *" --write-subs "*) for v in ${STUB_VTT:-}; do cp "$v" "$(dirname "$out")/s.$(basename "$v")"; done ;; + *" -f "*) cp "$STUB_AUDIO" "$(dirname "$out")/a.wav" ;; +esac +EOF +chmod +x "$tmp/bin/yt-dlp" +export PATH=$tmp/bin:$PATH + +fail=0 +check() { [ "$2" = "$3" ] || { echo "FAIL $1: got '$2', want '$3'"; fail=1; }; } +contains() { case "$2" in *"$3"*) ;; *) echo "FAIL $1: '${2:0:300}' lacks '$3'"; fail=1 ;; esac; } +v() { "$here/video.sh" "$@" 2>&1; } + +espeak-ng -v en -w "$tmp/speech.wav" "Hello. This is a short test of the video transcription tool." +ffmpeg -loglevel error -f lavfi -i color=black:s=160x120:d=6 -i "$tmp/speech.wav" -af apad \ + -shortest -c:a libopus "$tmp/Hello Clip.mp4" # padded: whisper drops a word cut at the end +ffmpeg -loglevel error -f lavfi -i color=black:s=160x120:d=2 "$tmp/silent.mkv" +ffmpeg -loglevel error -f lavfi -i anullsrc=r=8000:cl=mono -t 1860 -c:a libopus "$tmp/long.webm" + +# Stale temp folders from killed runs go; a fresh one stays. +mkdir "$XDG_RUNTIME_DIR/video-old" "$XDG_RUNTIME_DIR/video-new" +touch -d '2 days ago' "$XDG_RUNTIME_DIR/video-old" + +# Local video: whisper, the path line first, the Markdown file. +out=$(v "$tmp/Hello Clip.mp4") +check "local path line" "$(head -1 <<<"$out" | cut -d' ' -f1-2)" "transcript: $ASSISTANT_DIR/hello-clip.md" +contains "local how" "$out" "whisper large-v3-turbo" +contains "local text" "$(tr 'A-Z' 'a-z' <<<"$out")" "short test of the video" +md=$(cat "$ASSISTANT_DIR/hello-clip.md" 2>/dev/null) +contains "md title" "$md" "# Hello Clip" +contains "md source" "$md" "Source: $tmp/Hello Clip.mp4" +contains "md duration" "$md" "Duration: 0:0" +check "stale tmp removed" "$(test -e "$XDG_RUNTIME_DIR/video-old" && echo yes || echo no)" no +check "fresh tmp kept" "$(test -e "$XDG_RUNTIME_DIR/video-new" && echo yes || echo no)" yes +check "own tmp removed" "$(command ls -d "$XDG_RUNTIME_DIR"/video-* | wc -l)" 1 + +# Same title again: a free name, nothing overwritten. +out=$(v "$tmp/Hello Clip.mp4") +contains "collision" "$(head -1 <<<"$out")" "$ASSISTANT_DIR/hello-clip-2.md " + +# Refusals: one error line, exit 1, nothing written. +before=$(command ls "$ASSISTANT_DIR" | wc -l) +out=$(v "$tmp/silent.mkv"); check "no audio rc" $? 1 +contains "no audio" "$out" "error: no audio in silent.mkv" +out=$(v "relative.mp4"); check "relative rc" $? 1 +contains "relative" "$out" "error: not an absolute path or an http(s) URL" +out=$(v "$tmp/missing.mp4"); contains "missing" "$out" "error: cannot read" +out=$(v "$tmp/long.webm"); check "long rc" $? 0 +contains "long" "$out" "long: long, 31:00. Ask the user" +check "nothing written" "$(command ls "$ASSISTANT_DIR" | wc -l)" "$before" + +# URL with uploader subtitles in both languages: the spoken one, cleaned. +cat > "$tmp/it.vtt" <<'EOF' +WEBVTT +Kind: captions + +NOTE a comment +that spans lines + +1 +00:00:00.000 --> 00:00:02.000 +<c>Ciao a tutti</c> & benvenuti + +2 +00:00:02.000 --> 00:00:04.000 align:start +Ciao a tutti & benvenuti + +00:00:04.000 --> 00:00:06.000 +Oggi parliamo di Slackware. +EOF +printf 'WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nHello everyone\n' > "$tmp/en.vtt" +out=$(STUB_META=$'125\tit\tUn Titolo: prova!' STUB_VTT="$tmp/en.vtt $tmp/it.vtt" v "https://example.org/v/1") +contains "subs path" "$(head -1 <<<"$out")" "$ASSISTANT_DIR/un-titolo-prova.md (9 words, 2:05, uploader subtitles)" +md=$(cat "$ASSISTANT_DIR/un-titolo-prova.md" 2>/dev/null) +contains "subs text" "$md" $'Ciao a tutti & benvenuti\n\nOggi parliamo di Slackware.' +check "subs dedup" "$(grep -c 'Ciao a tutti' <<<"$md")" 1 +check "subs no english" "$(grep -c 'Hello everyone' <<<"$md")" 0 +check "subs no note" "$(grep -c 'comment\|spans' <<<"$md")" 0 + +# URL without subtitles: the audio download goes through whisper. +out=$(STUB_META=$'5\tNA\tNo Subs' STUB_AUDIO=$tmp/speech.wav v "https://example.org/v/2") +contains "audio path" "$(head -1 <<<"$out")" "$ASSISTANT_DIR/no-subs.md" +contains "audio how" "$out" "whisper large-v3-turbo" + +# URL failures. +out=$(STUB_FAIL="Video unavailable" v "https://example.org/v/3"); check "fail rc" $? 1 +check "fail message" "$out" "error: Video unavailable" +out=$(STUB_META=$'NA\tNA\tLive' v "https://example.org/v/4") +contains "live" "$out" "error: unknown length" +out=$(STUB_META=$'2000\ten\tLong one' v "https://example.org/v/5") +contains "long url" "$out" "long: Long one, 33:20." + +# A long transcript: the path line survives, the output fits the cap. +{ printf 'WEBVTT\n\n'; for i in $(seq 400); do printf '00:00:%02d.000 --> 00:00:%02d.500\nline number %d of the talk\n\n' $((i % 60)) $((i % 60)) "$i"; done; } > "$tmp/big.vtt" +out=$(STUB_META=$'600\ten\tBig Talk' STUB_VTT="$tmp/big.vtt" v "https://example.org/v/6") +contains "big path" "$(head -1 <<<"$out")" "big-talk.md (2400 words" +check "big under cap" "$(( ${#out} < 2000 ))" 1 + +[ $fail = 0 ] && echo ok |
