distro specific fixes
[distro-setup] / ffs
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to change
7 # to a recommended GPL license.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 # ffs = ffmpeg stream
24
25 # potential improvement: it might be nice that we could have a tall terminal but only use
26 # the top half for a 1080p stream, this is how:
27 # https://superuser.com/questions/1106674/how-to-add-blank-lines-above-the-bottom-in-terminal
28
29
30 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
31 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
32 set -eE -o pipefail
33 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
34
35 usage() {
36 cat <<EOF
37 Usage: ${0##*/} [OPTIONS] [sysops|tech|staff|test]
38
39 arg is icecast mountpoint suffix, except staff removes suffix.
40
41 -d debug.
42 -r RESOLUTION_TYPE
43 full: full screen even high resolution.
44 tall (default): half screen.
45 quarter: self evident
46 -l loud/listen. Start unmuted. Usually for testing.
47 -u Undelayed. Removes 5 second video delay, and about 4 second audio delay.
48 -w do not launch watch of stream
49
50 note: args duplicated in ffp
51
52
53 -h|--help Print help and exit.
54
55 Note: Uses util-linux getopt option parsing: spaces between args and
56 options, short options can be combined, options before args.
57 EOF
58 exit $1
59 }
60
61 ##### begin command line parsing ########
62
63 # ensure we can handle args with spaces or empty.
64 ret=0; getopt -T || ret=$?
65 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
66
67 ffp_args=()
68 debug=false
69 delay=true
70 loglevel=fatal
71 watch=true
72 volume=0
73 fullscreen=false
74 tall=true
75 temp=$(getopt -l help hdlr:uw "$@") || usage 1
76 eval set -- "$temp"
77 while true; do
78 case $1 in
79 -d)
80 debug=true
81 loglevel=debug
82 loglevel=info
83 ffp_args+=(-d)
84 ;;
85 -l)
86 volume=1
87 ;;
88 -r)
89 case $2 in
90 tall)
91 fullscreen=false
92 tall=true
93 ;;
94 quarter)
95 fullscreen=false
96 tall=false
97 ;;
98 full)
99 fullscreen=true
100 tall=false
101 ;;
102 esac
103 shift
104 ;;
105 -w)
106 watch=false
107 ;;
108 -u)
109 delay=false
110 ;;
111 -h|--help) usage ;;
112 --) shift; break ;;
113 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
114 esac
115 shift
116 done
117
118 mount_suffix=-sysops
119 if [[ $1 ]]; then
120 case $1 in
121 sysops|tech)
122 mount_suffix=-$1
123 ;;&
124 tech)
125 delay=false
126 ;;
127 staff)
128 mount_suffix=
129 ;;
130 *)
131 echo "error: unexpected \$1: $1" >&2
132 exit 1
133 ;;
134 esac
135 ffp_args+=($1)
136 fi
137
138 if $delay; then
139 # 2500 gets us around a 4 second delay, up from 1.5s.
140 delay_arg=,tpad=start_duration=2500ms
141 fi
142
143
144 ##### end command line parsing ########
145
146 host=live.iankelling.org:8443
147 live_host=$(dig +timeout=1 +short @iankelling.org live.iankelling.org)
148 vps_host=$(dig +timeout=1 +short iankelling.org)
149 if [[ $live_host != "$vps_host" ]] && ip n show 10.2.0.1 | grep . &>/dev/null && \
150 [[ $(dig +timeout=1 +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]]; then
151 host=127.0.0.1:8000
152 if ! pgrep '^icecast2$' >/dev/null; then
153 sudo systemctl start icecast2
154 fi
155 else
156 find_prefix="ssh live.iankelling.org"
157 fi
158
159 if $find_prefix find /var/icecast -type f | grep .; then
160 echo "warning: suggest clearing /var/icecast with icrmr or moving files. sleeping for 4 seconds"
161 sleep 4
162 fi
163
164
165 pass=$(sed -n 's/ *<source-password>\([^<]*\).*/\1/p' /p/c/icecast.xml)
166
167
168 tmpf=$(mktemp)
169 xrandr >$tmpf
170
171 # example xrandr output: 1280x800+0+0
172 primary_res=$(awk '$2 == "connected" && $3 == "primary" { print $4 }' $tmpf | sed 's/+.*//')
173 tmp=$(awk '$2 == "connected" && $3 != "primary" { print $3 }' $tmpf | sed 's/+/ /g')
174 read -r secondary_res x_offset _ <<<"$tmp"
175
176
177 if [[ $secondary_res ]]; then
178 secondary_x=${secondary_res%%x*}
179 secondary_y=${secondary_res##*x}
180 if $fullscreen; then
181 stream_res=$secondary_res
182 elif $tall; then
183 stream_res=$(( secondary_x / 2 ))x$secondary_y
184 else
185 stream_res=$(( secondary_x / 2 ))x$(( secondary_y / 2))
186 fi
187 else
188 x_offset=0
189 stream_res=$primary_res
190 fi
191
192 stream_x=${stream_res%x*}
193 stream_y=${stream_res#*x}
194
195 # leave out our i3 window borders
196 stream_res=$(( stream_x - 4 ))x$(( stream_y - 4))
197
198
199 # if hardware acceleration exists, use it to save power & cpu.
200 if vainfo |& grep -i VAProfileVP9Profile &>/dev/null; then
201 # 1500 seems almost flawless
202 bitrate_1080=1500
203
204 encode_settings=(
205 -c:v vp9_vaapi
206 # these options increase compression based on random internet reference.
207 -bsf:v vp9_raw_reorder,vp9_superframe
208 )
209 # https://trac.ffmpeg.org/wiki/Hardware/VAAPI
210 global_extra_args=(
211 -vaapi_device /dev/dri/renderD128
212 )
213 extra_filter_arg=",format=nv12|vaapi,hwupload"
214 else
215 # 1000 is a bit blury, 1500 is pretty clear, 2000 makes scrolling
216 # adjust much faster, 2500 has marginal improvement on that.
217 #
218 # note https://livekit.io/webrtc/bitrate-guide (our framerate is lower)
219 bitrate_1080=2000
220
221 encode_settings=(
222 -vcodec libvpx
223 -quality realtime
224 -error-resilient 1
225 )
226 fi
227
228 bitrate=$(( ( stream_x * stream_y ) / ( (1920*1080) / bitrate_1080 ) ))
229
230
231 # 8 seems fine. be conservative by going a bit higher.
232 framerate=10
233 keyframe_interval=$((framerate * 2))
234
235 # Monitor of default sink.
236 # eg: alsa_output.usb-Audio-gd_Audio-gd-00.analog-stereo
237 pa_sink=$(pactl get-default-sink).monitor
238
239 # this is for ffmpeg warnings. doesnt seem to affect latency.
240 # 160 was too small. at 300, it occasionally complains,
241 # probably only when we are using delayed output
242 thread_queue_size_arg="-thread_queue_size 500"
243
244 opts=(
245 # global options
246 # be relatively quiet. switch to debug when testing.
247 -v $loglevel
248 -hide_banner
249 -nostats
250
251 ${global_extra_args[@]}
252
253 # tested for decreasing latency: did not help.
254 # -probesize 32
255 # tested for warning "Queue input is backward in time". did not help.
256 #-rtbufsize 500M
257
258 # note: ordering of inputs also affects zmqsend commands.
259
260 ## audio input options
261
262 -f pulse
263 -name ffs
264 # note: duplicated
265 $thread_queue_size_arg
266 -fragment_size 512
267 -i default
268
269 -f pulse
270 $thread_queue_size_arg
271 # pulse knows this name somewhere
272 -name ffsdesktop
273 # This fixes latency. i haven't tried tuning it, but going too low creates
274 # choppy output.
275 -fragment_size 512
276 -i "$pa_sink"
277
278
279 ## video input options
280 -video_size $stream_res
281 $thread_queue_size_arg
282 -f x11grab
283 -framerate $framerate
284 -i :0.0+$x_offset.0
285
286 # Video + audio filter. Note: this has only the things we actually need in it.
287 #
288 # volume=precision=fixed fixes this error:
289 # The following filters could not choose their formats: Parsed_amerge_4.
290 #
291 # Default volume precision is float. Our input is fixed. maybe ffmpeg
292 # thinks the input could change and so can't commit to an output.
293 # The error suggests using aformat, which seems like it would probably
294 # also fix the error.
295 #
296 # man page say zmq url default includes "localhost", but specifying a
297 # localhost url caused an error for me.
298 -filter_complex "[0]azmq,volume=precision=fixed: volume=$volume [vol0];
299 [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1];
300 [vol0][vol1] amerge=inputs=2;
301 [2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}${extra_filter_arg}[out]"
302
303 # An online source says to match a 5 second vid delay, we can do an
304 # audio delay filter: "adelay=5000|5000". However, we already get
305 # a stream delay of about 2 seconds, and having the audio be about
306 # 2 seconds ahead is fine, they do that intentionally in soccer
307 # matches.
308
309 # Based on error message and poking around, it seems ffmpeg is not
310 # smart enough to see that [vol0] and [vol1] are inputs to the amerge
311 # filter, and thus we would not want them as final outputs. So, we
312 # have to identify the amerge output and pass it to -out. This
313 # identifier is called an "output pad" in man ffmpeg-filters, and a
314 # "link label" in man ffmpeg.
315 -map '[out]'
316
317 # video output options
318 -g $keyframe_interval
319 ${encode_settings[@]}
320 -b:v ${bitrate}k
321
322
323 ## audio output options
324 -c:a libvorbis
325 -b:a 128k
326 # afaik, this ensures that the amerge doesn't make 4 channel output if
327 # our output format supported it.
328 -ac 2
329
330 -content_type video/webm
331 -f webm
332 icecast://source:$pass@$host/fsf$mount_suffix.webm
333 )
334
335 rm -f /tmp/iank-ffmpeg-interlude-toggle
336
337 # system mute. disabled, just using application level mute atm.
338 #
339 # pactl set-source-mute @DEFAULT_SOURCE@ true
340
341 if pkill -f ^ffmpeg.\*icecast://source.\*/fsf; then
342 sleep 1
343 fi
344
345 echo executing: ffmpeg ${opts[@]}
346
347 #{ sleep 1; ffp &>/dev/null & }
348
349 if $debug; then
350 ffmpeg "${opts[@]}"
351 exit 0
352 fi
353
354 ##### begin clipboard history checkup ####
355
356 # Avoid streaming with secrets in our clipboard history. We could just
357 # clear the history, but here I truncate it to a max and then show it,
358 # and then I can press super+y if I want to clear it, or close the
359 # window if I want to keep it.
360 copyqcount=$(copyq count)
361 regex='^[1-9][0-9]*$'
362 if [[ $copyqcount =~ $regex ]]; then
363 # i dont want to think about more than this
364 max_rows=40
365 if (( copyqcount >= max_rows )); then
366 rows_arg=()
367 for ((i=max_rows; i<copyqcount; i++)); do
368 rows_arg+=($i)
369 done
370 copyq remove "${rows_arg[@]}"
371 fi
372 copyq show
373 gone=false
374 for (( i=0; i<40; i++ )); do
375 if i3-msg -t get_tree | jq -e '.. | select(.class? == "copyq" and .instance? == "copyq")' &>/dev/null; then
376 sleep .5
377 else
378 gone=true
379 break
380 fi
381 done
382 if ! $gone; then
383 msg="ffs: copyq not gone. aborting. super+y = copyq-restart / clear"
384 if [[ -t 0 ]]; then
385 echo $msg
386 else
387 dunstify -u critical -h string:x-dunst-stack-tag:alert "$msg"
388 fi
389 exit 1
390 fi
391 fi
392 ##### end clipboard history checkup ####
393
394 if [[ $mount_suffix == -sysops ]]; then
395 touch $HOME/.iank-stream-on
396 fi
397
398 echo $volume >$HOME/.iank-stream-muted
399
400 ffmpeg "${opts[@]}" &
401 if $watch; then
402 # watch the stream and end the stream when we stop watching.
403 sleep 2
404 ffp -d "${ffp_args[@]}" ||:
405 kill %%
406 rm -f $HOME/.iank-stream-on
407 fi
408
409 ### begin background/development docs ###
410
411 # zmq vs stdin commands:
412 #
413 # * zmq allows targeting a specific filter when there are duplicates.
414 #
415 # * if you type stdin command too slow, ffmpeg will die because it stops
416 # doing normal work while it is waiting.
417 #
418 # * zmq returns a result to the calling process, rather than printing to
419 # stdout.
420 #
421 # * the only simple zmq tool I found, zmqsend, requires compiling. I
422 # used the latest ffmpeg 7.0.1. Build like so:
423 #
424 # p build-dep ffmpeg/aramo
425 # ./configure --enable-libzmq # i already had libzmq3-dev installed
426 # make alltools
427 # cp tools/zmqsend /a/opt/bin
428 #
429 # * ffmpeg debug output was useful in testing zmq commands.
430 #
431 # * Important documentation for stdin commands is only found by typing
432 # "c" into the stdin of ffmpeg and reading the output.
433 #
434 #
435 #
436 # stdin command docs, before I abandoned it for zmq:
437 # mkfifo -m 0600 /tmp/iank-ffmpeg
438 # # ffmpeg sits and waits until we do this. dunno why.
439 # echo >/tmp/iank-ffmpeg &
440 # ffmpeg ... </tmp/iank-ffmpeg |& while read -r line; do : check results; done
441 # # example of working commands:
442 # echo "cdrawbox -1 t 0" >/tmp/iank-ffmpeg
443 # echo "cdrawbox -1 t fill" >/tmp/iank-ffmpeg
444 # echo "cdrawtext -1 reinit text=''" >/tmp/iank-ffmpeg
445 # echo "cvolume -1 volume=1" >/tmp/iank-ffmpeg
446
447
448 # For testing: to show the number of audio channels in a resulting file
449 # https://stackoverflow.com/questions/47905083/how-to-check-number-of-channels-in-my-audio-wav-file-using-ffmpeg-command
450 #
451 # ffprobe -i /tmp/out.wav -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0
452
453 # for a right/left speaker test:
454 # https://askubuntu.com/questions/148363/which-linux-command-can-i-use-to-test-my-speakers-for-current-talk-radio-output
455 # p install alsa-utils
456 # speaker-test -t wav -c 2 -l 1
457
458 # There are 2 other options for audio, so I wanted to do a little
459 # performance measurement of this method.
460 # 1 is to combine the 2 audio sources in pulse,
461 # https://unix.stackexchange.com/questions/351764/create-combined-source-in-pulseaudio .
462 # 1 is to record mumble and combine in post processing.
463
464 ### benchmark / perf tests: these are pretty inaccurate.
465 # 29 seconds cpu use. video bitrate 1500k, 8 fps, 2x keyframe interval.
466 # * 64k vorbis: 69.7%
467 # * 128k vorbis: 70.1% (used in subsequent tests)
468 # * 1 audio input: 64.3%
469 # * 0 audio inputs: 59.2%
470
471 # how I did perf testing: add -to 00:00:30 to ffmpeg opts to
472 # conveniently exit after measurement. Then run:
473 #
474 # ffmpeg "${opts[@]}" &
475 # pid=$!
476 # sleep 29
477 # ps -p $pid -o %cpu
478 # kill %%
479
480 # filter for only 1 audio input:
481 #-filter_complex "[0]azmq,volume=precision=fixed;[1]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
482 # filter with 0 audio input:
483 # -filter_complex "[0]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
484
485
486 # When things weren't working, I did some checking on newer ffmpeg to
487 # see if that helped. It never did. I compiled the latest ffmpeg release
488 # tarball, 7.0.1, and tried the version in debian bullseye by schrooting
489 # before running ffmpeg. Building was just configure; make, but then I
490 # found some flags that were needed. gpl flags r just because I noticed them.
491 # ./configure --enable-libzmq --enable-libpulse --enable-libvorbis --enable-gpl --enable-version3
492 #
493
494 # note: when playing back, text is going to look aliased unless you
495 # watch it in a window that is exactly as bit or bigger than the
496 # recording: tabbed i3 window shrinks things. or, use: mpv
497 # --video-unscaled
498
499 ### end background/development docs ###