i3 improvements wip
[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 # todo: learn to start working in one corner of the screen.
26
27 # todo: get an icecast on li.b8.nz for when i'm away from home.
28
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
32 #
33 # potential improvement: sometimes I probably want to stream full height
34 # window. I could add an option for that.
35
36 # potential improvement: setup a function which automatically mutes after some time, or
37 # after some time of no audio input.
38
39
40 ### begin background/development docs ###
41
42 # zmq vs stdin commands:
43 #
44 # * zmq allows targeting a specific filter when there are duplicates.
45 #
46 # * if you type stdin command too slow, ffmpeg will die because it stops
47 # doing normal work while it is waiting.
48 #
49 # * zmq returns a result to the calling process, rather than printing to
50 # stdout.
51 #
52 # * the only simple zmq tool I found, zmqsend, requires compiling. I
53 # used the latest ffmpeg 7.0.1. Build like so:
54 #
55 # p build-dep ffmpeg/aramo
56 # ./configure --enable-libzmq # i already had libzmq3-dev installed
57 # make alltools
58 # cp tools/zmqsend /a/opt/bin
59 #
60 # * ffmpeg debug output was useful in testing zmq commands.
61 #
62 # * Important documentation for stdin commands is only found by typing
63 # "c" into the stdin of ffmpeg and reading the output.
64 #
65 #
66 #
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
77
78
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
81 #
82 # ffprobe -i /tmp/out.wav -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0
83
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
88
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.
94
95 ### benchmark / perf tests: these are pretty inaccurate.
96 # 29 seconds cpu use. video bitrate 1500k, 8 fps, 2x keyframe interval.
97 # * 64k vorbis: 69.7%
98 # * 128k vorbis: 70.1% (used in subsequent tests)
99 # * 1 audio input: 64.3%
100 # * 0 audio inputs: 59.2%
101
102 # how I did perf testing: add -to 00:00:30 to ffmpeg opts to
103 # conveniently exit after measurement. Then run:
104 #
105 # ffmpeg "${opts[@]}" &
106 # pid=$!
107 # sleep 29
108 # ps -p $pid -o %cpu
109 # kill %%
110
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=''"
115
116
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
123 #
124
125 ### end background/development docs ###
126
127
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
130 set -eE -o pipefail
131 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
132
133
134 debug=false
135 loglevel=fatal
136
137 # 3 mountpoints: fsf-sysops (default, public), fsf (all staff), fsf-tech (tech team)
138 # # note: duplicated in ffp
139 mount_suffix=-sysops
140 while [[ $1 ]]; do
141 case $1 in
142 sysops|tech)
143 mount_suffix=-$1
144 ;;
145 staff)
146 mount_suffix=
147 ;;
148 -d)
149 debug=true
150 loglevel=debug
151 ;;
152 esac
153 shift
154 done
155
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
159 host=127.0.0.1:8000
160 fi
161
162 pass=$(sed -n 's/ *<source-password>\([^<]*\).*/\1/p' /p/c/icecast.xml)
163
164
165 tmpf=$(mktemp)
166 xrandr >$tmpf
167
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/+.*//')
171
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))
178 else
179 x_offset=0
180 stream_res=$primary_res
181 fi
182
183 framerate=8
184 keyframe_interval=$((framerate * 2))
185
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
189
190 opts=(
191 # global options
192 # be relatively quiet. switch to debug when testing.
193 -v $loglevel
194 -hide_banner
195 -nostats
196
197 # note: ordering of inputs also affects zmqsend commands.
198
199 ## audio input options
200
201 -f pulse
202 -name ffs
203 # note: duplicated above
204 -thread_queue_size 160
205 -fragment_size 512
206 -i default
207
208 -f pulse
209 # this is for ffmpeg warnings. doesnt seem to affect latency.
210 -thread_queue_size 160
211 # pulse knows this name somewhere
212 -name ffsdesktop
213 # This fixes latency. i haven't tried tuning it, but going too low creates
214 # choppy output.
215 -fragment_size 512
216 -i "$pa_sink"
217
218
219 ## video input options
220 -video_size $stream_res
221 -f x11grab
222 -framerate $framerate
223 -i :0.0+$x_offset.0
224
225 # Video + audio filter. Note: this has only the things we actually need in it.
226 #
227 # volume=precision=fixed fixes this error:
228 # The following filters could not choose their formats: Parsed_amerge_4.
229 #
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.
234 #
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=''"
241
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.
248 -map '[out]'
249
250 # video output options
251 -vcodec libvpx
252 -g $keyframe_interval
253 -quality realtime
254 # for 1080p, default 256k is poor quality. 500 is ok. 1500 is a bit better.
255 -b:v 1500k
256 -threads 2
257 -buffer_duration 10
258 -error-resilient 1
259
260 ## audio output options
261 -c:a libvorbis
262 -b:a 128k
263 # afaik, this ensures that the amerge doesn't make 4 channel output if
264 # our output format supported it.
265 -ac 2
266
267 -content_type video/webm
268 -f webm
269 icecast://source:$pass@$host/fsf$mount_suffix.webm
270 )
271
272 rm -f /tmp/iank-ffmpeg-interlude-toggle
273
274 # start muted
275 pactl set-source-mute @DEFAULT_SOURCE@ true
276
277 #echo executing: ffmpeg ${opts[@]}
278
279 #{ sleep 1; ffp &>/dev/null & }
280
281 if $debug; then
282 ffmpeg "${opts[@]}"
283 exit 0
284 fi
285
286 # For now, we want to watch the stream and end the stream when we stop watching.
287 ffmpeg "${opts[@]}" &
288 sleep 2
289 ffp ||:
290 kill %%