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 # todo: learn to start working in one corner of the screen.
27 # todo: get an icecast on li.b8.nz for when i'm away from home.
29 # potential improvement: it might be nice that we could have a tall terminal bug only use
30 # the top half for a 1080p stream, this is how:
31 # https://superuser.com/questions/1106674/how-to-add-blank-lines-above-the-bottom-in-terminal
33 # potential improvement: sometimes I probably want to stream full height
34 # window. I could add an option for that.
36 # potential improvement: setup a function which automatically mutes after some time, or
37 # after some time of no audio input.
40 ### begin background/development docs ###
42 # zmq vs stdin commands:
44 # * zmq allows targeting a specific filter when there are duplicates.
46 # * if you type stdin command too slow, ffmpeg will die because it stops
47 # doing normal work while it is waiting.
49 # * zmq returns a result to the calling process, rather than printing to
52 # * the only simple zmq tool I found, zmqsend, requires compiling. I
53 # used the latest ffmpeg 7.0.1. Build like so:
55 # p build-dep ffmpeg/aramo
56 # ./configure --enable-libzmq # i already had libzmq3-dev installed
58 # cp tools/zmqsend /a/opt/bin
60 # * ffmpeg debug output was useful in testing zmq commands.
62 # * Important documentation for stdin commands is only found by typing
63 # "c" into the stdin of ffmpeg and reading the output.
67 # stdin command docs, before I abandoned it for zmq:
68 # mkfifo -m 0600 /tmp/iank-ffmpeg
69 # # ffmpeg sits and waits until we do this. dunno why.
70 # echo >/tmp/iank-ffmpeg &
71 # ffmpeg ... </tmp/iank-ffmpeg |& while read -r line; do : check results; done
72 # # example of working commands:
73 # echo "cdrawbox -1 t 0" >/tmp/iank-ffmpeg
74 # echo "cdrawbox -1 t fill" >/tmp/iank-ffmpeg
75 # echo "cdrawtext -1 reinit text=''" >/tmp/iank-ffmpeg
76 # echo "cvolume -1 volume=1" >/tmp/iank-ffmpeg
79 # For testing: to show the number of audio channels in a resulting file
80 # https://stackoverflow.com/questions/47905083/how-to-check-number-of-channels-in-my-audio-wav-file-using-ffmpeg-command
82 # ffprobe -i /tmp/out.wav -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0
84 # for a right/left speaker test:
85 # https://askubuntu.com/questions/148363/which-linux-command-can-i-use-to-test-my-speakers-for-current-talk-radio-output
86 # p install alsa-utils
87 # speaker-test -t wav -c 2 -l 1
89 # There are 2 other options for audio, so I wanted to do a little
90 # performance measurement of this method.
91 # 1 is to combine the 2 audio sources in pulse,
92 # https://unix.stackexchange.com/questions/351764/create-combined-source-in-pulseaudio .
93 # 1 is to record mumble and combine in post processing.
95 ### benchmark / perf tests: these are pretty inaccurate.
96 # 29 seconds cpu use. video bitrate 1500k, 8 fps, 2x keyframe interval.
98 # * 128k vorbis: 70.1% (used in subsequent tests)
99 # * 1 audio input: 64.3%
100 # * 0 audio inputs: 59.2%
102 # how I did perf testing: add -to 00:00:30 to ffmpeg opts to
103 # conveniently exit after measurement. Then run:
105 # ffmpeg "${opts[@]}" &
111 # filter for only 1 audio input:
112 #-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=''"
113 # filter with 0 audio input:
114 # -filter_complex "[0]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
117 # When things weren't working, I did some checking on newer ffmpeg to
118 # see if that helped. It never did. I compiled the latest ffmpeg release
119 # tarball, 7.0.1, and tried the version in debian bullseye by schrooting
120 # before running ffmpeg. Building was just configure; make, but then I
121 # found some flags that were needed. gpl flags r just because I noticed them.
122 # ./configure --enable-libzmq --enable-libpulse --enable-libvorbis --enable-gpl --enable-version3
125 ### end background/development docs ###
128 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
129 shopt -s inherit_errexit
2>/dev
/null ||
: # ignore fail in bash < 4.4
131 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
137 # 3 mountpoints: fsf-sysops (default, public), fsf (all staff), fsf-tech (tech team)
138 # # note: duplicated in ffp
156 host=live.iankelling.org
:8000
157 if [[ $
(dig +short @
10.2.0.1 -x 10.2.0.2 2>&1 ||
:) == kd.b8.nz.
]] \
158 && ip n show
10.2.0.1 |
grep .
&>/dev
/null
; then
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 secondary_res
=$
(awk '$2 == "connected" && $3 != "primary" { print $3 }' $tmpf |
sed 's/+.*//')
172 if [[ $secondary_res ]]; then
173 # assumes secondary is on the right
174 x_offset
=${primary_res%%x*}
175 secondary_x
=${secondary_res%%x*}
176 secondary_y
=${secondary_res##*x}
177 stream_res
=$
(( secondary_x
/ 2 ))x$
(( secondary_y
/ 2))
180 stream_res
=$primary_res
184 keyframe_interval
=$
((framerate
* 2))
186 # Monitor of default sink.
187 # eg: alsa_output.usb-Audio-gd_Audio-gd-00.analog-stereo
188 pa_sink
=$
(pacmd list-sinks |
awk '/\*/ {getline; print $2}' |
sed 's/^<//;s/>$//').monitor
192 # be relatively quiet. switch to debug when testing.
197 # note: ordering of inputs also affects zmqsend commands.
199 ## audio input options
203 # note: duplicated above
204 -thread_queue_size 160
209 # this is for ffmpeg warnings. doesnt seem to affect latency.
210 -thread_queue_size 160
211 # pulse knows this name somewhere
213 # This fixes latency. i haven't tried tuning it, but going too low creates
219 ## video input options
220 -video_size $stream_res
222 -framerate $framerate
225 # Video + audio filter. Note: this has only the things we actually need in it.
227 # volume=precision=fixed fixes this error:
228 # The following filters could not choose their formats: Parsed_amerge_4.
230 # Default volume precision is float. Our input is fixed. maybe ffmpeg
231 # thinks the input could change and so can't commit to an output.
232 # The error suggests using aformat, which seems like it would probably
233 # also fix the error.
235 # man page say zmq url default includes "localhost", but specifying a
236 # localhost url caused an error for me.
237 -filter_complex "[0]azmq,volume=precision=fixed: volume=0 [vol0];
238 [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1];
239 [vol0][vol1] amerge=inputs=2 [out];
240 [2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
242 # Based on error message and poking around, it seems ffmpeg is not
243 # smart enough to see that [vol0] and [vol1] are inputs to the amerge
244 # filter, and thus we would not want them as final outputs. So, we
245 # have to identify the amerge output and pass it to -out. This
246 # identifier is called an "output pad" in man ffmpeg-filters, and a
247 # "link label" in man ffmpeg.
250 # video output options
252 -g $keyframe_interval
254 # for 1080p, default 256k is poor quality. 500 is ok. 1500 is a bit better.
260 ## audio output options
263 # afaik, this ensures that the amerge doesn't make 4 channel output if
264 # our output format supported it.
267 -content_type video
/webm
269 icecast
://source:$pass@
$host/fsf
$mount_suffix.webm
272 rm -f /tmp
/iank-ffmpeg-interlude-toggle
275 pactl set-source-mute @DEFAULT_SOURCE@ true
277 #echo executing: ffmpeg ${opts[@]}
279 #{ sleep 1; ffp &>/dev/null & }
286 # For now, we want to watch the stream and end the stream when we stop watching.
287 ffmpeg
"${opts[@]}" &