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.
9 # Copyright 2024 Ian Kelling
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
15 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
33 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
37 Usage: ${0##*/} [OPTIONS] [sysops|tech|staff|test]
39 arg is icecast mountpoint suffix, except staff removes suffix.
43 full: full screen even high resolution.
44 tall (default): half screen.
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
50 note: args duplicated in ffp
53 -h|--help Print help and exit.
55 Note: Uses util-linux getopt option parsing: spaces between args and
56 options, short options can be combined, options before args.
61 ##### begin command line parsing ########
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; }
75 temp
=$
(getopt
-l help hdlr
:uw
"$@") || usage
1
113 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
131 echo "error: unexpected \$1: $1" >&2
139 # 2500 gets us around a 4 second delay, up from 1.5s.
140 delay_arg
=,tpad
=start_duration
=2500ms
144 ##### end command line parsing ########
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
153 find_prefix
="ssh live.iankelling.org"
156 if $find_prefix find /var
/icecast
-type f |
grep .
; then
157 echo "warning: suggest clearing /var/icecast with icrmr or moving files. sleeping for 4 seconds"
162 pass
=$
(sed -n 's/ *<source-password>\([^<]*\).*/\1/p' /p
/c
/icecast.xml
)
168 # example xrandr output: 1280x800+0+0
169 primary_res
=$
(awk '$2 == "connected" && $3 == "primary" { print $4 }' $tmpf |
sed 's/+.*//')
170 tmp
=$
(awk '$2 == "connected" && $3 != "primary" { print $3 }' $tmpf |
sed 's/+/ /g')
171 read -r secondary_res x_offset _
<<<"$tmp"
174 if [[ $secondary_res ]]; then
175 secondary_x
=${secondary_res%%x*}
176 secondary_y
=${secondary_res##*x}
178 stream_res
=$secondary_res
180 stream_res
=$
(( secondary_x
/ 2 ))x
$secondary_y
182 stream_res
=$
(( secondary_x
/ 2 ))x$
(( secondary_y
/ 2))
186 stream_res
=$primary_res
189 stream_x
=${stream_res%x*}
190 stream_y
=${stream_res#*x}
192 # leave out our i3 window borders
193 stream_res
=$
(( stream_x
- 4 ))x$
(( stream_y
- 4))
196 # 1000 is a bit blury, 1500 is pretty clear
197 # note https://livekit.io/webrtc/bitrate-guide (our framerate is lower)
199 # Scale our bitrate to 1500 1080p
200 bitrate
=$
(( ( stream_x
* stream_y
) / ( (1920*1080) / 1000 ) ))
202 # 8 seems fine. be conservative by going a bit higher.
204 keyframe_interval
=$
((framerate
* 2))
206 # Monitor of default sink.
207 # eg: alsa_output.usb-Audio-gd_Audio-gd-00.analog-stereo
208 pa_sink
=$
(pactl get-default-sink
).monitor
210 # this is for ffmpeg warnings. doesnt seem to affect latency.
211 # 160 was too small. at 300, it occasionally complains,
212 # probably only when we are using delayed output
213 thread_queue_size_arg
="-thread_queue_size 500"
217 # be relatively quiet. switch to debug when testing.
222 # tested for decreasing latency: did not help.
224 # tested for warning "Queue input is backward in time". did not help.
227 # note: ordering of inputs also affects zmqsend commands.
229 ## audio input options
234 $thread_queue_size_arg
239 $thread_queue_size_arg
240 # pulse knows this name somewhere
242 # This fixes latency. i haven't tried tuning it, but going too low creates
248 ## video input options
249 -video_size $stream_res
250 $thread_queue_size_arg
252 -framerate $framerate
255 # Video + audio filter. Note: this has only the things we actually need in it.
257 # volume=precision=fixed fixes this error:
258 # The following filters could not choose their formats: Parsed_amerge_4.
260 # Default volume precision is float. Our input is fixed. maybe ffmpeg
261 # thinks the input could change and so can't commit to an output.
262 # The error suggests using aformat, which seems like it would probably
263 # also fix the error.
265 # man page say zmq url default includes "localhost", but specifying a
266 # localhost url caused an error for me.
267 -filter_complex "[0]azmq,volume=precision=fixed: volume=$volume [vol0];
268 [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1];
269 [vol0][vol1] amerge=inputs=2;
270 [2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}[out]"
272 # An online source says to match a 5 second vid delay, we can do an
273 # audio delay filter: "adelay=5000|5000". However, we already get
274 # a stream delay of about 2 seconds, and having the audio be about
275 # 2 seconds ahead is fine, they do that intentionally in soccer
278 # Based on error message and poking around, it seems ffmpeg is not
279 # smart enough to see that [vol0] and [vol1] are inputs to the amerge
280 # filter, and thus we would not want them as final outputs. So, we
281 # have to identify the amerge output and pass it to -out. This
282 # identifier is called an "output pad" in man ffmpeg-filters, and a
283 # "link label" in man ffmpeg.
286 # video output options
288 -g $keyframe_interval
294 ## audio output options
297 # afaik, this ensures that the amerge doesn't make 4 channel output if
298 # our output format supported it.
301 -content_type video
/webm
303 icecast
://source:$pass@
$host/fsf
$mount_suffix.webm
306 rm -f /tmp
/iank-ffmpeg-interlude-toggle
308 # system mute. disabled, just using application level mute atm.
310 # pactl set-source-mute @DEFAULT_SOURCE@ true
312 if pkill
-f ^ffmpeg.\
*icecast
://source.\
*/fsf
; then
316 echo executing
: ffmpeg
${opts[@]}
318 #{ sleep 1; ffp &>/dev/null & }
325 ##### begin clipboard history checkup ####
327 # Avoid streaming with secrets in our clipboard history. We could just
328 # clear the history, but here I truncate it to a max and then show it,
329 # and then I can press super+y if I want to clear it, or close the
330 # window if I want to keep it.
331 copyqcount
=$
(copyq count
)
332 regex
='^[1-9][0-9]*$'
333 if [[ $copyqcount =~
$regex ]]; then
334 # i dont want to think about more than this
336 if (( copyqcount
>= max_rows
)); then
338 for ((i
=max_rows
; i
<copyqcount
; i
++)); do
341 copyq remove
"${rows_arg[@]}"
345 for (( i
=0; i
<40; i
++ )); do
346 if i3-msg
-t get_tree | jq
-e '.. | select(.class? == "copyq" and .instance? == "copyq")' &>/dev
/null
; then
354 msg
="ffs: copyq not gone. aborting. super+y = copyq-restart / clear"
358 dunstify
-u critical
-h string
:x-dunst-stack-tag
:alert
"$msg"
363 ##### end clipboard history checkup ####
365 if [[ $mount_suffix == -sysops ]]; then
366 touch $HOME/.iank-stream-on
369 echo $volume >$HOME/.iank-stream-muted
371 ffmpeg
"${opts[@]}" &
373 # watch the stream and end the stream when we stop watching.
375 ffp
-d "${ffp_args[@]}" ||
:
377 rm -f $HOME/.iank-stream-on
380 ### begin background/development docs ###
382 # zmq vs stdin commands:
384 # * zmq allows targeting a specific filter when there are duplicates.
386 # * if you type stdin command too slow, ffmpeg will die because it stops
387 # doing normal work while it is waiting.
389 # * zmq returns a result to the calling process, rather than printing to
392 # * the only simple zmq tool I found, zmqsend, requires compiling. I
393 # used the latest ffmpeg 7.0.1. Build like so:
395 # p build-dep ffmpeg/aramo
396 # ./configure --enable-libzmq # i already had libzmq3-dev installed
398 # cp tools/zmqsend /a/opt/bin
400 # * ffmpeg debug output was useful in testing zmq commands.
402 # * Important documentation for stdin commands is only found by typing
403 # "c" into the stdin of ffmpeg and reading the output.
407 # stdin command docs, before I abandoned it for zmq:
408 # mkfifo -m 0600 /tmp/iank-ffmpeg
409 # # ffmpeg sits and waits until we do this. dunno why.
410 # echo >/tmp/iank-ffmpeg &
411 # ffmpeg ... </tmp/iank-ffmpeg |& while read -r line; do : check results; done
412 # # example of working commands:
413 # echo "cdrawbox -1 t 0" >/tmp/iank-ffmpeg
414 # echo "cdrawbox -1 t fill" >/tmp/iank-ffmpeg
415 # echo "cdrawtext -1 reinit text=''" >/tmp/iank-ffmpeg
416 # echo "cvolume -1 volume=1" >/tmp/iank-ffmpeg
419 # For testing: to show the number of audio channels in a resulting file
420 # https://stackoverflow.com/questions/47905083/how-to-check-number-of-channels-in-my-audio-wav-file-using-ffmpeg-command
422 # ffprobe -i /tmp/out.wav -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0
424 # for a right/left speaker test:
425 # https://askubuntu.com/questions/148363/which-linux-command-can-i-use-to-test-my-speakers-for-current-talk-radio-output
426 # p install alsa-utils
427 # speaker-test -t wav -c 2 -l 1
429 # There are 2 other options for audio, so I wanted to do a little
430 # performance measurement of this method.
431 # 1 is to combine the 2 audio sources in pulse,
432 # https://unix.stackexchange.com/questions/351764/create-combined-source-in-pulseaudio .
433 # 1 is to record mumble and combine in post processing.
435 ### benchmark / perf tests: these are pretty inaccurate.
436 # 29 seconds cpu use. video bitrate 1500k, 8 fps, 2x keyframe interval.
437 # * 64k vorbis: 69.7%
438 # * 128k vorbis: 70.1% (used in subsequent tests)
439 # * 1 audio input: 64.3%
440 # * 0 audio inputs: 59.2%
442 # how I did perf testing: add -to 00:00:30 to ffmpeg opts to
443 # conveniently exit after measurement. Then run:
445 # ffmpeg "${opts[@]}" &
451 # filter for only 1 audio input:
452 #-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=''"
453 # filter with 0 audio input:
454 # -filter_complex "[0]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
457 # When things weren't working, I did some checking on newer ffmpeg to
458 # see if that helped. It never did. I compiled the latest ffmpeg release
459 # tarball, 7.0.1, and tried the version in debian bullseye by schrooting
460 # before running ffmpeg. Building was just configure; make, but then I
461 # found some flags that were needed. gpl flags r just because I noticed them.
462 # ./configure --enable-libzmq --enable-libpulse --enable-libvorbis --enable-gpl --enable-version3
465 # note: when playing back, text is going to look aliased unless you
466 # watch it in a window that is exactly as bit or bigger than the
467 # recording: tabbed i3 window shrinks things. or, use: mpv
470 ### end background/development docs ###