Scripting inspiration

Yesterday, too late in the evening, I stumbled upon a need, to save an album available for streaming for offline use, to be able to stream it conveniently via my Sonos system (yes there are streaming services to stream directly from as well but that is not as future proof let’s say). If you live in a legislation where it is allowed to make a private copy, this is fine from a regulatory viewpoint.

I asked google what solutions/products other people had come up with. Nothing obvious, a bunch of crap where scammers want you to pay for some shitty Windows applications. No thanks.

Next thought. How difficult could that be in a bash script since you are able to combine a bunch of well tested software components. This type of scripting is more like lego than software development.

I think I had something working in less that 15 minutes since I had already used parec to record sound output produced by PulseAudio. My thought is that there are probably a bunch of problems and cases where people would have a lot of use of some basic scripting abilities, if that is PowerShell in Windows or Bash in Ubuntu does not matter, in order to get things done without buying/installing software.

If you are already onboard, good for you, otherwise a primer in scripting might be a well invested evening. First this one, and then the classic Advanced Bash-Scripting Guide.

!/bin/bash
#Dependencies: script that moves sound output to a named sink (moveSinks.sh), mp3splt
NOW=$(date '+%Y-%m-%d')
mkdir ~/Music/$NOW
echo "Start playing the album"
sleep 2
DEFAULT_OUTPUT_NAME=$(pacmd list-sinks | grep -A1 "* index" | grep -oP "<\K[^ >]+")
pactl load-module module-combine-sink sink_name=record-n-play slaves=$DEFAULT_OUTPUT_NAME sink_properties=device.description="Record-and-Play"
${HOME}/scripts/moveSinks.sh record-n-play
/usr/bin/parec -d record-n-play.monitor | /usr/bin/lame -r -V0 - "${HOME}/Music/${NOW}/${NOW}.mp3" &
while true; do
  sleep 15
  number_sinks=$(pacmd list-sink-inputs | grep available. | cut -c1-1)
  echo "Found this no. of pulse sinks: $number_sinks"
  if [[ $number_sinks -le 1 ]]; then
    # Stop recording kill -9 $!
    break
  fi
  sleep 45
done
#Split into separate tracks
cd ${HOME}/Music/${NOW}/
mp3splt -s ${NOW}.mp3

This entry was posted in datorer, linux, programmering, webbprojekt and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *