Last active
September 17, 2017 02:10
-
-
Save poppen/93fe4d09f3fca0113139bf1e8d9d48c3 to your computer and use it in GitHub Desktop.
らじるらじる録音スクリプト forked from https://gist.github.com/poppen/0e054e20947d56756c489f74c7f53d0e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
XML_URL="http://www.nhk.or.jp/radio/config/config_web.xml" | |
XML="/tmp/config_web.xml.$$" | |
trap 'rm $XML; exit 1' 1 2 3 15 | |
date=$(date '+%Y-%m-%d-%H-%M') | |
outdir="." | |
identify_area() | |
{ | |
if [[ "$1" =~ [[:alnum:]]+-[[:alnum:]]+ ]]; then | |
local area=${1%-*} | |
else | |
local area="tokyo" | |
fi | |
echo "$area" | |
} | |
identify_channel() | |
{ | |
if [[ "$1" =~ [[:alnum:]]+-[[:alnum:]]+ ]]; then | |
local channel=${1#*-*} | |
else | |
local channel=$1 | |
fi | |
echo "$channel" | |
} | |
get_playurl() | |
{ | |
local area=$1 channel=$2 | |
local play_url count i | |
count=$(xmllint --xpath 'count(//data)' "$XML") | |
for ((i=1; $i <= "$count"; i=$i+1)); do | |
if [ "$(xmllint --xpath '//data['"$i"']/area/text()' "$XML")" = "$area" ]; then | |
play_url=$(xmllint --nocdata --xpath '//data['"$i"']/'"$channel"'hls/text()' "$XML") | |
echo "$play_url" | |
return 0 | |
fi | |
done | |
echo "Can't find channel url" >&2 | |
exit 1 | |
} | |
#引数が足りないときは、使用方法を表示して戻り値1でバイバイ。 | |
if [ $# -le 1 ]; then | |
echo "usage : $0 channel_name duration(minutes) [outputdir] [prefix]" | |
exit 1 | |
fi | |
#引数が2つ以上あるときは、1個目をCh.名に、2個目を録音時間に指定。 | |
if [ $# -ge 2 ]; then | |
channel=$1 | |
DURATION=$(($2 * 60 + 60)) | |
fi | |
#引数が3つ以上あるときは、3個目を出力先ディレクトリに指定。 | |
if [ $# -ge 3 ]; then | |
outdir=$3 | |
fi | |
#引数が4つあるときは、4個目をファイル名の固定値に指定する。指定しなければ固定値はCh.名。 | |
PREFIX=${channel} | |
if [ $# -ge 4 ]; then | |
PREFIX=$4 | |
fi | |
curl -s -o "$XML" "$XML_URL" | |
channel=$(echo "$channel" | tr '[:upper:]' '[:lower:]') | |
area=$(identify_area "$channel") | |
ch=$(identify_channel "$channel") | |
play_url=$(get_playurl "$area" "$ch") | |
filename="${PREFIX}-${date}.m4a" | |
ffmpeg -y -i "$play_url" -t "$DURATION" -codec copy "/tmp/${filename}" | |
ffmpeg -y -i "/tmp/${filename}" -acodec libmp3lame -ab 64k "${outdir%%/}/${filename%%.*}.mp3" && rm -f "/tmp/${filename}" && rm -f "$XML" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment