stream.sh/client.sh

66 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# USER-CONFIGURABLE VARIABLES
len=60
delay=030
ffmpeg_bin=ffmpeg
# ---
function log() {
echo "[$(date "+%T")] $1"
}
function stream() {
$ffmpeg_bin -re -f alsa -channels 2 -sample_rate 44100 -i hw:Loopback,1,0 -t $1 -f matroska -acodec $fmt -b:a $bitrate - 2>$tmp | nc $ip $port
}
function stream_wave() {
$ffmpeg_bin -re -f alsa -channels 2 -sample_rate 44100 -i hw:Loopback,1,0 -t $1 -f wav - 2>$tmp | nc $ip $port
}
function stream_cont() {
$stream $len & sleep $len.$delay
}
function rolling_rocks() {
while true; do
if [[ "$(cat $tmp | grep -a "Input/output error")" != '' ]]; then
rm $tmp
$stream $((len-1)).9 &
log 'Hit a "Device Busy" error, consider increasing sleep delay!'
fi
sleep 0.1
done
}
if [[ "$1" == '' ]]; then
if [[ "$0" != *"client.sh" ]]; then
echo "usage: $0 client <ip> <port> [fmt] [kbps]"
else
echo "usage: $0 <ip> <port> [fmt] [kbps]"
fi
exit 1
fi
if [[ "$3" != '' ]]; then
fmt="$3"
stream="stream"
log 'Container: matroska'
else
fmt="wav"
stream="stream_wave"
log 'Container: RIFF'
fi
if [[ "$4" != '' ]]; then
bitrate="$4"
else
bitrate="128k"
fi
ip=$1
port=$2
tmp=$(mktemp)
rolling_rocks &
log "Loop length: ${len}s"
log "Post-loop delay: ${delay}ms"
log 'Starting main loop...'
while true; do
stream_cont
done