1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#!/bin/bash
# desktop-assistant tool: transcribe the speech in a local or online video.
# 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.
#
# video.sh <source> [long]: <source> is an absolute path or an http(s) URL.
# Uploader subtitles (it/en) when a URL has them, else whisper large-v3-turbo
# on the CPU (no VRAM: the loaded model keeps the GPU). Writes
# $ASSISTANT_DIR/<slug>.md, opens it, prints its path and the start of the
# text. Over 30 minutes it only reports the length, unless <long> is 1.
# Check: test-video.sh.
set -uo pipefail
MODEL=${WHISPER_MODEL:-/data/voice-models/whisper/ggml-large-v3-turbo-q5_0.bin}
LONG_S=1800
PREVIEW=1500
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
}
# Cue text of a WebVTT file: no header, cue ids, timings, NOTE/STYLE blocks
# 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 }
skip { next }
/-->/ { next }
/^[0-9]+$/ { next }
{ gsub(/<[^>]*>/, ""); gsub(/&/, "\\&"); gsub(/</, "<"); gsub(/>/, ">")
gsub(/ /, " "); sub(/^[[:space:]]+/, ""); sub(/[[:space:]]+$/, "")
if ($0 != "" && $0 != last) print; last = $0 }
' "$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' \
| tr -c 'a-z0-9' '-' | tr -s '-')
s=${s#-}; s=${s:0:60}; s=${s%-}
printf '%s' "${s:-video}"
}
[[ $# -ge 1 && -n $1 ]] || die "no video given"
src=$1 long=${2:-}
[[ -n ${ASSISTANT_DIR:-} ]] || die "ASSISTANT_DIR is not set"
run=${XDG_RUNTIME_DIR:-/tmp}
# A stop kills this script's process group with SIGKILL, so the trap below
# never runs then: clear what earlier killed runs left behind.
find "$run" -maxdepth 1 -name 'video-*' -type d -mmin +1440 -exec rm -rf {} + 2>/dev/null
tmp=$(mktemp -d "$run/video-XXXXXX") || die "cannot make a temp folder"
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 %(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 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"
dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$src")
[[ $dur =~ ^[0-9.]+$ ]] || die "not a video ffmpeg can read: ${src##*/}"
[[ -n $(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$src") ]] \
|| die "no audio in ${src##*/}"
title=${src##*/}; title=${title%.*}; lang=""
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
# Only subtitles the uploader wrote: auto-generated ones repeat every
# line in rolling cues, and whisper turbo does better.
yt-dlp --no-playlist --no-warnings --skip-download --write-subs --sub-langs 'it.*,en.*' \
--sub-format vtt -o "$tmp/s.%(ext)s" -- "$src" >/dev/null 2>&1
subs=("$tmp"/s.*.vtt)
if [[ -f ${subs[0]} ]]; then
pick=${subs[0]}
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" | paragraphs)
[[ -n $text ]] && how="uploader subtitles"
fi
if [[ -z $text ]]; then
yt-dlp --no-playlist --no-warnings -f 'bestaudio/best' -o "$tmp/a.%(ext)s" -- "$src" \
>/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
fi
if [[ -z $text ]]; then
[[ -r $MODEL ]] || die "whisper model missing: $MODEL"
# 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")"
# turbo repeats words ("themes themes", "Revis Revis Revis" over music):
# a word said twice in a row collapses to one, after the join: a repeat
# often straddles two segments.
# ponytail: a real "very very" goes too; whisper's --vad may fix the cause.
text=$(sed 's/^[[:space:]]*//; /^$/d' "$tmp/t.txt" \
| paragraphs | sed -E "s/\b([[:alpha:]']+)([ ,]+\1\b)+/\1/g")
[[ -n $text ]] || die "no speech found"
how="whisper large-v3-turbo"
fi
mkdir -p "$ASSISTANT_DIR" || die "cannot create $ASSISTANT_DIR"
base=$(slug "$title")
out="$ASSISTANT_DIR/$base.md" n=2
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"
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"
[[ -n ${VIDEO_NO_OPEN:-} ]] || setsid -f xdg-open "$out" >/dev/null 2>&1
words=$(wc -w <<<"$text")
# The path first: the daemon cuts tool output at 2000 characters.
echo "transcript: $out ($words words, $(hms "$dur"), $how)"
if (( ${#text} <= PREVIEW )); then
echo "full text:"
else
echo "start, not the whole text; to summarize or question it, the user attaches the file:"
fi
printf '%s\n' "${text:0:PREVIEW}"
|