diff options
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/tools/test-video.sh | 40 | ||||
| -rwxr-xr-x | examples/tools/video.sh | 45 |
2 files changed, 61 insertions, 24 deletions
diff --git a/examples/tools/test-video.sh b/examples/tools/test-video.sh index 74f7d72..55a16d3 100755 --- a/examples/tools/test-video.sh +++ b/examples/tools/test-video.sh @@ -76,10 +76,10 @@ 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" +contains "no audio" "$out" "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" +contains "relative" "$out" "not an absolute path or an http(s) URL" +out=$(v "$tmp/missing.mp4"); contains "missing" "$out" "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" @@ -104,31 +104,47 @@ Ciao a tutti & benvenuti 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") +out=$(STUB_META=$'125\tFalse\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.' +contains "subs text" "$md" 'Ciao a tutti & benvenuti Oggi 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 +# CRLF subtitles (PeerTube): the same text, no cue numbers. +sed 's/$/\r/' "$tmp/it.vtt" > "$tmp/crlf.vtt" +out=$(STUB_META=$'125\tFalse\tNA\tCRLF' STUB_VTT="$tmp/crlf.vtt" v "https://example.org/v/9") +md=$(cat "$ASSISTANT_DIR/crlf.md" 2>/dev/null) +contains "crlf text" "$md" 'Ciao a tutti & benvenuti Oggi parliamo di Slackware.' +check "crlf no cue ids" "$(grep -c '^[0-9] \| 2 ' <<<"$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") +out=$(STUB_META=$'5\tFalse\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") +check "fail message" "$out" "Video unavailable" +out=$(STUB_META=$'NA\tTrue\tNA\tLive' v "https://example.org/v/4") +contains "live" "$out" "a live stream" +out=$(STUB_META=$'2000\tFalse\ten\tLong one' v "https://example.org/v/5") contains "long url" "$out" "long: Long one, 33:20." +# No length from the site (Instagram): measured on the download. +out=$(STUB_META=$'NA\tFalse\tNA\tVideo by someone' STUB_AUDIO=$tmp/speech.wav v "https://example.org/v/7") +contains "no length" "$(head -1 <<<"$out")" "$ASSISTANT_DIR/video-by-someone.md (" +cp "$tmp/long.webm" "$tmp/long.wav" # the stub saves it as a.wav; ffprobe reads the content +out=$(STUB_META=$'NA\tFalse\tNA\tLong reel' STUB_AUDIO=$tmp/long.wav v "https://example.org/v/8") +contains "no length, long" "$out" "long: Long reel, 31:00." + # 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") +{ 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\tFalse\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 +para=$(awk 'NR > 5 && NF' "$ASSISTANT_DIR/big-talk.md" | head -1) +check "paragraph joined to a sentence end" "$(( ${#para} > 400 && ${#para} < 440 ))" 1 [ $fail = 0 ] && echo ok diff --git a/examples/tools/video.sh b/examples/tools/video.sh index 842a02f..1c34593 100755 --- a/examples/tools/video.sh +++ b/examples/tools/video.sh @@ -28,9 +28,10 @@ MODEL=${WHISPER_MODEL:-/data/voice-models/whisper/ggml-large-v3-turbo-q5_0.bin} LONG_S=1800 PREVIEW=1500 -die() { echo "error: $*" >&2; exit 1; } +die() { echo "$*" >&2; exit 1; } # the daemon prefixes "error: " hms() { # seconds -> m:ss or h:mm:ss + [[ -n $1 ]] || { printf 'unknown length'; return; } local s=${1%.*} if (( s >= 3600 )); then printf '%d:%02d:%02d' $((s / 3600)) $((s % 3600 / 60)) $((s % 60)) else printf '%d:%02d' $((s / 60)) $((s % 60)); fi @@ -40,6 +41,7 @@ hms() { # seconds -> m:ss or h:mm:ss # or tags, and a line repeated by the next cue only once. vtt_text() { awk ' + { sub(/\r$/, "") } # PeerTube writes CRLF /^WEBVTT/ { skip = 1; next } # header lines (Kind:, Language:) up to a blank /^(NOTE|STYLE|REGION)/ { skip = 1; next } /^[[:space:]]*$/ { skip = 0; next } @@ -52,6 +54,14 @@ vtt_text() { ' "$1" } +# Segments and cues break mid-sentence: join them, and start a paragraph +# at a sentence end once one passes 400 characters. +paragraphs() { + awk '{ p = p (p == "" ? "" : " ") $0 } + length(p) > 400 && p ~ /[.!?]["»”)]?$/ { print p; print ""; p = "" } + END { if (p != "") print p }' +} + slug() { local s s=$(printf '%s' "$1" | iconv -f utf-8 -t ascii//TRANSLIT 2>/dev/null | tr 'A-Z' 'a-z' \ @@ -73,11 +83,15 @@ trap 'rm -rf "$tmp"' EXIT if [[ $src =~ ^https?:// ]]; then # Separated by a tab: a title may hold any other character. - meta=$(yt-dlp --no-playlist --no-warnings --print '%(duration)s %(language)s %(title)s' \ + meta=$(yt-dlp --no-playlist --no-warnings \ + --print '%(duration)s %(is_live)s %(language)s %(title)s' \ -- "$src" 2>"$tmp/err" | head -1) \ || die "$(tail -1 "$tmp/err" | sed 's/^ERROR: //')" - IFS=$'\t' read -r dur lang title <<<"$meta" - [[ $dur =~ ^[0-9.]+$ ]] || die "unknown length: a live stream, or not a video" + IFS=$'\t' read -r dur live lang title <<<"$meta" + [[ $live == True ]] && die "a live stream: nothing to transcribe until it ends" + # Some sites (Instagram) give no length: measured after the download. + [[ $dur =~ ^[0-9.]+$ ]] || dur="" + [[ $lang == NA ]] && lang="" else [[ $src == /* ]] || die "not an absolute path or an http(s) URL: $src" [[ -f $src && -r $src ]] || die "cannot read $src" @@ -88,10 +102,13 @@ else title=${src##*/}; title=${title%.*}; lang="" fi -if (( ${dur%.*} > LONG_S )) && [[ $long != 1 ]]; then - echo "long: $title, $(hms "$dur"). Ask the user before transcribing: it takes about a minute per 10 minutes of video." - exit 0 -fi +too_long() { + if [[ -n $dur ]] && (( ${dur%.*} > LONG_S )) && [[ $long != 1 ]]; then + echo "long: $title, $(hms "$dur"). Ask the user before transcribing: it takes about a minute per 10 minutes of video." + exit 0 + fi +} +too_long text="" how="" if [[ $src =~ ^https?:// ]]; then @@ -105,7 +122,7 @@ if [[ $src =~ ^https?:// ]]; then for s in "${subs[@]}"; do # the spoken language's, when the site says [[ -n $lang && ${s##*/s.} == "$lang"* ]] && pick=$s done - text=$(vtt_text "$pick") + text=$(vtt_text "$pick" | paragraphs) [[ -n $text ]] && how="uploader subtitles" fi if [[ -z $text ]]; then @@ -113,6 +130,11 @@ if [[ $src =~ ^https?:// ]]; then >/dev/null 2>"$tmp/err" || die "$(tail -1 "$tmp/err" | sed 's/^ERROR: //')" audio=$(find "$tmp" -maxdepth 1 -name 'a.*' | head -1) [[ -n $audio ]] || die "the download gave no file" + if [[ -z $dur ]]; then + dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$audio") + [[ $dur =~ ^[0-9.]+$ ]] || dur="" + too_long + fi fi else audio=$src @@ -123,7 +145,7 @@ if [[ -z $text ]]; then # whisper reads any container through ffmpeg. -ng: CPU, 8 threads beat 16. whisper-cli -m "$MODEL" -ng -t 8 -l auto -np -otxt -of "$tmp/t" -f "$audio" \ >/dev/null 2>"$tmp/err" || die "whisper: $(tail -1 "$tmp/err")" - text=$(sed 's/^[[:space:]]*//; /^$/d' "$tmp/t.txt") + text=$(sed 's/^[[:space:]]*//; /^$/d' "$tmp/t.txt" | paragraphs) [[ -n $text ]] || die "no speech found" how="whisper large-v3-turbo" fi @@ -135,8 +157,7 @@ while [[ -e $out ]]; do out="$ASSISTANT_DIR/$base-$n.md"; n=$((n + 1)); done { printf '# %s\n\nSource: %s\nDuration: %s. Transcribed %s with %s.\n\n' \ "$title" "$src" "$(hms "$dur")" "$(date +%F)" "$how" - # One paragraph per segment or cue line. - printf '%s\n' "$text" | sed 's/$/\n/' + printf '%s\n' "$text" } >"$tmp/out.md" || die "cannot write the transcript" # Whole or not at all: a stop before this line leaves no file. mv "$tmp/out.md" "$out" || die "cannot write $out" |
