Create a gist now

Instantly share code, notes, and snippets.

@nosuz /rec-radio.sh
Last active Feb 21, 2017

What would you like to do?
#!/bin/bash
music_dir="$HOME/Music"
folder="Radio"
logfile="$HOME/log/rec-radio.log"
# Ref.
# https://gist.github.com/riocampos/5656450
# http://wordpress.honobono-life.info/code/bash%E3%81%A7%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%83%9E%E3%83%83%E3%83%81%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E6%9D%A1%E4%BB%B6%E8%A9%95%E4%BE%A1/
if [[ ! -d `dirname $logfile` ]]; then
mkdir -p `dirname $logfile`
fi
#exec &>> $logfile
exec 1>> $logfile 2> /dev/null
date +%Y-%m-%d_%H:%M
echo "$@"
if [[ ! -d $music_dir/$folder ]]; then
mkdir -p $music_dir/$folder
fi
if [[ $# -lt 2 ]]; then
echo `basename $0` channel time[mh] [program_name]
exit
fi
case $1 in
[Ff][Mm] )
echo channel: FM
name="FM"
url="rtmpe://netradio-bkfm-flash.nhk.jp"
path="NetRadio_BKFM_flash@108233" ;;
[Rr]1 )
echo channel: Radio1
name="R1"
url="rtmpe://netradio-bkr1-flash.nhk.jp"
path="NetRadio_BKR1_flash@108232" ;;
[Rr]2 )
echo channel: Radio2
name="R2"
url="rtmpe://netradio-r2-flash.nhk.jp"
path="NetRadio_R2_flash@63342" ;;
* ) echo channel: fm r1 r2;
exit ;;
esac
if [[ $# -gt 2 ]]; then
name=$3
fi
# set record time
if [[ $2 =~ ([[:digit:]]+)$ ]]; then
time=${BASH_REMATCH[1]}
else
time=0
fi
if [[ $2 =~ ([[:digit:]]+)[mM] ]]; then
time=$(( $time + ${BASH_REMATCH[1]} * 60 ))
fi
if [[ $2 =~ ([[:digit:]]+)[hH] ]]; then
time=$(( $time + ${BASH_REMATCH[1]} * 3600 ))
fi
if [[ $time -eq 0 ]]; then
echo Time format erro: $2
exit
else
time = $(( $time + 5 ))
fi
echo record time: $time sec
#file=${name}_$(date +%Y%m%d-%H%M)
file=$(date +%Y%m%d-%H%M)
# encode while recording
rtmpdump --rtmp "$url" \
--playpath "$path" \
--app "live" \
--swfVfy http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf \
--live \
--stop $time \
| ffmpeg -i - -acodec libmp3lame \
-metadata "album"="$name" \
-metadata "artist"="らじる ★ らじる" \
-metadata "title"="$(date +%Y-%m-%d)" \
-metadata "publisher"="NHKラジオ" \
-metadata "date"="$(date +%Y)" \
$music_dir/$folder/$file.mp3
# | ffmpeg -i - -acodec libmp3lame $music_dir/$folder/$file.mp3
# -o $music_dir/$folder/$file.m4a
if [[ -e $music_dir/$folder/$file.mp3 ]]; then
/usr/bin/eyeD3 --no-color $music_dir/$folder/$file.mp3
echo add $folder/$file.mp3 into MPD playlist
retry=20
while [[ retry -gt 0 ]]
do
/usr/bin/mpc ls $folder/$file.mp3
case $? in
0) /usr/bin/mpc add $folder/$file.mp3
echo added to playlist
break
;;
1) :
;;
esac
retry=$(($retry - 1))
#echo wait 1 sec. \($retry\)
sleep 1
done
fi
# set ID3 tag
#if [[ -e $music_dir/$folder/$file.mp3 ]]; then
# /usr/bin/eyeD3 --no-color \
# --set-encoding=utf16-LE \
# -a "らじる ★ らじる" \
# -A "$name" \
# -t $(date +%Y-%m-%d) \
# -Y $(date +%Y) \
# -p "NHKラジオ" \
# $music_dir/$folder/$file.mp3
#fi
@nosuz
Owner
nosuz commented Jan 26, 2017

録音時間を取得するために使っていた[[コマンドの正規表現を一度変数に入れていた。この代入は無駄なので直接埋め込んだ。

@nosuz
Owner
nosuz commented Feb 21, 2017

録音したファイルをMPDのplaylistに登録するように修正した。MPDは常にフォルダーに追加されたファイルがないか監視しているが、発見されるまでに少し時間がかかるのでsleepで発見されるのを待つ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment