LukeSmithxyz / voidrice

My dotfiles (deployed by LARBS)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

suggestion: dump radio station streem

voyeg3r opened this issue · comments

I am using your amazing dmenurecord script and I am thinking how to make something similar with radio stream stations. Record the audio and play at the same time.

for now all I have is:

# this is a Brazilian radio station but could be any radio station
station="ceararural"
url="http://stm3.xcast.com.br:8560/stream"
filename="${station}-$(date +%d-%b-%Y-%H-%M).mp3"

recdir="$XDG_MUSIC_DIR/ceararural"
[ ! -d "$recdir" ] && mkdir "$recdir"

(
cd $recdir
echo "Recording $station at $recdir"
wget -O- "$url" | tee -ai "$filename" | mpv -
)

I would like to implement some kind of menu like yours:

$0   ................ start recording
$0 kill ............ stop recording

I have tried putting the recoding command in a subshell, something like this:

(wget -O- "$url" | tee -ai "$filename" | mpv - ) & echo $! /tmp/recradiopid

But, differently of your dmenurecord, here we have at least two processes that we have to manage, wget and mpv.

Thanks for your time!

Based on your dmenurecord I have created a script that dumps a stream to a file and plays it at once:

#!/usr/bin/env zsh
#     Filename: dumpceararural
#          vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab ft=sh:
#  Last Change: Sat, 15 Oct 2022 12:00:21
#        Autor: Sérgio Araújo
#         site: https://dev/to/voyeg3r
#      Licence: GPL (see http://www.gnu.org/licenses/gpl.txt)
#    Reference: https://github.com/LukeSmithxyz/voidrice/blob/master/.local/bin/dmenurecord


# https://meleu.sh/trap-err/
# https://stackoverflow.com/questions/8363519/
# https://askubuntu.com/questions/1033866/
# set -euo pipefail
# trap 'echo "${BASH_SOURCE}:${LINENO}:${FUNCNAME:-}"' ERR

# Dependências: dunst, mpv, wget
#
# Usage:
# $0:        Ask for recording via dmenu
# $0 start:  Record the stream
# $0 stop:   Stop recording

trap 'killrecording' INT

station="ceararural"
url="http://stm3.xcast.com.br:8560/stream"
filename="${station}-$(date +%d-%b-%Y-%H-%M).mp3"

killrecording () {
    dunstify "Parando gravação de áudio"
     if  [ -f "/tmp/dump_pid" ]; then
        pkill -P $(< "/tmp/dump_pid")
        \rm -f "/tmp/dump_pid"
     else
         dunstify "Não há PIDs listados para matar"
         exit 1
     fi
    exit 0
}

askrecording () {
    choice=$(printf "yes\nno" | dmenu -i -p "Record Ceará Rural?")
    case "$choice" in
        yes) record;;
        no)  exit 0;;
         *)  exit 0;;
    esac
}

asktoend() { \
	response=$(printf "No\\nYes" | dmenu -i -p "Recording still active. End recording?") &&
	[ "$response" = "Yes" ] &&  killrecording
    exit 0
	}

record () {
    (
    recdir="$XDG_MUSIC_DIR/ceararural"
    [ ! -d "$recdir" ] && mkdir "$recdir"
    cd "$recdir"
    dunstify "Iniciando gravação em $recdir"
    ( wget -O- "$url" | tee -ai "$filename" | mpv - ) & echo $! > /tmp/dump_pid
    )
}

case "$1" in
    start) record;;
    stop)  killrecording;;
    *) ([ -f "/tmp/dump_pid" ] && asktoend && exit) || askrecording;;
esac

If it's of any use to you, I wrote a similar radio-streaming script using dmenu albeit it lacks recording of stations.

dmenuradio

#!/bin/sh

mkdir -p "${XDG_CACHE_HOME:-~/.cache}/radio"

asktoend() {
	response=$(printf "No\\nYes" | dmenu -i -p "Playing $(cat ${XDG_CACHE_HOME:-~/.cache}/radio/station), stop playing?")
	[ "$response" = "Yes" ] && kill -9 $(cat "${XDG_CACHE_HOME:-~/.cache}/radio/pid") && echo "" > ${XDG_CACHE_HOME:-~/.cache}/radio/station
	kill -46 $(pidof $STATUSBAR)
}

togglemute() {
	state=$(echo '{ "command": ["get_property", "mute"] }' | socat - /tmp/mpvSockets/$(cat ${XDG_CACHE_HOME:-~/.cache}/radio/pid) | jq .data)

	if [ "$state" = "true" ]; then
		echo '{ "command": ["set_property", "mute", false] }' | socat - /tmp/mpvSockets/$(cat ${XDG_CACHE_HOME:-~/.cache}/radio/pid) >/dev/null
	else
		echo '{ "command": ["set_property", "mute", true] }' | socat - /tmp/mpvSockets/$(cat ${XDG_CACHE_HOME:-~/.cache}/radio/pid) >/dev/null
	fi
}

[ "$1" = "togglemute" ] && togglemute && exit

[ "$(cat ${XDG_CACHE_HOME:-~/.cache}/radio/station)" != "" ] && asktoend

chosen=$(cat ${XDG_DATA_HOME:-~/.local/share}/radio_list | cut -d ";" -f 1 | dmenu -p "Radio" -i -c -l 10)

[ -z "$chosen" ] && exit

kill -9 $(cat "${XDG_CACHE_HOME:-~/.cache}/radio/pid") && echo "" > ${XDG_CACHE_HOME:-~/.cache}/radio/station

echo $chosen > ${XDG_CACHE_HOME:-~/.cache}/radio/station

kill -46 $(pidof $STATUSBAR)

mpv > "${XDG_CACHE_HOME:-~/.cache}/radio/current" 2>/dev/null $(grep "$chosen;" ${XDG_DATA_HOME:-~/.local/share}/radio_list | cut -d ";" -f 2) &
echo $! > "${XDG_CACHE_HOME:-~/.cache}/radio/pid"

sb-radio

#!/bin/sh

case $BLOCK_BUTTON in
	1) setsid -f dmenuradio ;;
	2) dmenuradio togglemute ;;
	3) notify-send "📻 Radio module" "\- Shows currently playing radio station.
- Middle click to mute." ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

station="$(cat "${XDG_CACHE_HOME:-~/.cache}/radio/station")"
title="$(grep "icy-title" "${XDG_CACHE_HOME:-~/.cache}/radio/current" | sed -E 's/.icy-title: //g' | tail -n1)"
[ -n "$title" ] && dash=" -"

mutestate=$(echo '{ "command": ["get_property", "mute"] }' | socat - /tmp/mpvSockets/$(cat ${XDG_CACHE_HOME:-~/.cache}/radio/pid) | jq .data)

[ "$mutestate" = "true" ] && muted=" "
[ "$station" != "" ] && echo "📻 $station$dash$title$muted"

It displays the currently playing song in the statusbar, although only if the station is sending out an "icy-title".