Skip to content

Instantly share code, notes, and snippets.

@bemxio
Last active August 7, 2024 09:49
Show Gist options
  • Save bemxio/a16184bad962b9950c2d2fae0aeabc5f to your computer and use it in GitHub Desktop.
Save bemxio/a16184bad962b9950c2d2fae0aeabc5f to your computer and use it in GitHub Desktop.

Bash scripts for broadcasting an HLS audio stream, either from files or your desktop output. Uses ffmpeg, http.server from Python and a tunnel from cloudflared. stream_file.sh requires the path to a file or directory as an argument, stream_loopback.sh needs the input device to be specified in the variable at line 4. Make sure to fill up TUNNEL_UUID (line 7) in either script, in case you want to use tunneling.

#!/bin/bash
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
# constants
INPUT_PATH="$1"
OUTPUT_PATH="/tmp/stream"
TUNNEL_UUID=""
SERVER_ADDRESS="localhost"
SERVER_PORT="8080"
# create the output directory
mkdir -p "$OUTPUT_PATH"
# start the server and the tunnel
python -m http.server $SERVER_PORT \
--bind $SERVER_ADDRESS \
--directory "$OUTPUT_PATH" &
cloudflared tunnel --loglevel warn run \
--url http://$SERVER_ADDRESS:$SERVER_PORT \
--credentials-file ~/.cloudflared/$TUNNEL_UUID.json \
--protocol http2 \
$TUNNEL_UUID &
# start the stream
if [ -d "$INPUT_PATH" ]; then
shopt -s nullglob nocaseglob
ffmpeg -threads 0 -re -stream_loop -1 \
-f concat -safe 0 -i <(printf "file %q\n" "$INPUT_PATH"/*.{wav,flac,mp3,m4a,ogg} | sort -Vk1) -map 0:a \
-c:a aac -b:a 160k -ac 2 \
-f hls -hls_list_size 10 -hls_time 3 -hls_flags delete_segments "$OUTPUT_PATH"/stream.m3u8
shopt -u nullglob nocaseglob
else
ffmpeg -threads 0 -re -stream_loop -1 \
-i "$INPUT_PATH" -map 0:a \
-c:a aac -b:a 160k -ac 2 \
-f hls -hls_list_size 10 -hls_time 3 -hls_flags delete_segments "$OUTPUT_PATH"/stream.m3u8
fi
# kill the server and the tunnel
pkill -f python
pkill -f cloudflared
# remove the output directory
rm -rf "$OUTPUT_PATH"
#!/bin/bash
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
# constants
INPUT_DEVICE="alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"
OUTPUT_PATH="/tmp/stream"
TUNNEL_UUID=""
SERVER_ADDRESS="localhost"
SERVER_PORT="8080"
# create the output directory
mkdir -p "$OUTPUT_PATH"
# start the server and the tunnel
python -m http.server $SERVER_PORT \
--bind $SERVER_ADDRESS \
--directory "$OUTPUT_PATH" &
cloudflared tunnel --loglevel warn run \
--url http://$SERVER_ADDRESS:$SERVER_PORT \
--credentials-file ~/.cloudflared/$TUNNEL_UUID.json \
--protocol http2 \
$TUNNEL_UUID &
# start the stream
ffmpeg -threads 0 -re \
-f pulse -i $INPUT_DEVICE \
-c:a aac -b:a 160k -ac 2 \
-f hls -hls_list_size 10 -hls_time 3 -hls_flags delete_segments "$OUTPUT_PATH"/stream.m3u8
# kill the server and the tunnel
pkill -f python
pkill -f cloudflared
# remove the output directory
rm -rf "$OUTPUT_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment