Skip to content

Instantly share code, notes, and snippets.

@woowee
Last active December 10, 2015 20:58
Show Gist options
  • Save woowee/4491302 to your computer and use it in GitHub Desktop.
Save woowee/4491302 to your computer and use it in GitHub Desktop.
recording of the radiko.
#!/bin/bash
export LANG="ja_JP.UTF-8" LC_ALL="ja_JP.UTF-8"
datetime=`date '+%Y%m%d_%H%M'`
date=`date '+%Y年%m月%d日'`
year=`date '+%Y'`
radikodir=${HOME}/radiko/ # work ディレクトリ
cachedir=${radikodir}cache/ # アレがあれなトコ
mp3dir=${radikodir}_1recorded/ # レコーディングデータの出力場所
# mid3v2
mid3v2dir=${radikodir}tools/mutagen-1.20/build/scripts-2.7/
playerurl=http://radiko.jp/player/swf/player_3.1.0.00.swf
playerfile="./player.swf"
keyfile="./authkey.png"
suffix="_${STATION}_${NAME}"
tmpfile="./${1}_${datetime}"
# 1->AAC, 2->MP3
filetype=1
if [ ${filetype} -ne 2 ]; then
# aac
extentionis='aac'
else
#MP3
extentionis='mp3'
fi
mp3file="${mp3dir}${2}_${datetime}.${extentionis}"
margintime=1 #min
if [ $# -ge 3 ]; then
STATION=$1
NAME=$2
DURATION=`expr $3 \* 60 + ${margintime} \* 2 \* 60`
else
echo "usage : $0 STATION NAME DURATION(minuites)" 1>&2
exit 1
fi
delaysec=20
cd $cachedir
#
# get player
#
if [ ! -f $playerfile ]; then
/usr/local/bin/wget -q -O $playerfile $playerurl
if [ $? -ne 0 ]; then
echo "failed get player" 1>&2
exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
/usr/local/bin/swfextract -b 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata" 1>&2
exit 1
fi
fi
if [ -f auth1_fms${suffix} ]; then
rm -f auth1_fms${suffix}
fi
#
# access auth1_fms
#
/usr/local/bin/wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--post-data='\r\n' \
--no-check-certificate \
--save-headers \
--tries=5 \
--timeout=6 \
-O auth1_fms${suffix} \
https://radiko.jp/v2/api/auth1_fms
if [ $? -ne 0 ]; then
echo "failed auth1 process" 1>&2
exit 1
fi
#
# get partial key
#
authtoken=`cat auth1_fms${suffix} | perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)'`
offset=`cat auth1_fms${suffix} | perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)'`
length=`cat auth1_fms${suffix} | perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)'`
partialkey=`dd if=$keyfile bs=1 skip=${offset} count=${length} 2> /dev/null | base64`
echo "authtoken: ${authtoken} \noffset: ${offset} length: ${length} \npartialkey:
$partialkey"
rm -f auth1_fms${suffix}
if [ -f auth2_fms${suffix} ]; then
rm -f auth2_fms${suffix}
fi
#
# access auth2_fms
#
/usr/local/bin/wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--header="X-Radiko-Authtoken: ${authtoken}" \
--header="X-Radiko-Partialkey: ${partialkey}" \
--post-data='\r\n' \
--no-check-certificate \
--tries=5 \
--timeout=6 \
-O auth2_fms${suffix} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f auth2_fms${suffix} ]; then
echo "failed auth2 process" 1>&2
exit 1
fi
echo "authentication success"
areaid=`cat auth2_fms${suffix} | perl -ne 'print $1 if(/^([^,]+),/i)'`
echo "areaid: $areaid"
rm -f auth2_fms${suffix}
# offset time
echo "Sleep ${delaysec} sec."
sleep $delaysec
#
# rtmpdump
#
RETRYCOUNT=0
while :
do
/usr/local/bin/rtmpdump -v \
-r "rtmpe://w-radiko.smartstream.ne.jp" \
--playpath "simul-stream.stream" \
--app "${STATION}/_definst_" \
-W $playerurl \
-C S:"" -C S:"" -C S:"" -C S:$authtoken \
--live \
--stop $DURATION \
-o $tmpfile
if [ $? -ne 1 -o `wc -c ${tmpfile} | awk '{print $1}'` -ge 10240 ]; then
break
elif [ ${RETRYCOUNT} -ge 5 ]; then
echo "failed rtmpdump"
exit 1
else
RETRYCOUNT=`expr ${RETRYCOUNT} + 1`
fi
done
if [ ${filetype} -ne 2 ]; then
# acc
/usr/local/bin/ffmpeg -y -i $tmpfile -vn -acodec copy $mp3file
else
#MP3
/usr/local/bin/ffmpeg -y -i $tmpfile -vn -ab 64k $mp3file
fi
rm $tmpfile
@woowee
Copy link
Author

woowee commented Jan 9, 2013

ugegit / Radiko を参考にさせていただきました.
感謝.

@woowee
Copy link
Author

woowee commented Jan 9, 2013

id3 tag の処理について 「radiko録音その4(Mac):んー:So-netブログ」 を,
リトライ処理については「個人的自由帳 » とあるサイトの録音手段 (シェルスクリプト)」を,
参考にさせていただきました.
ありがとうございます.

@woowee
Copy link
Author

woowee commented Feb 3, 2013

id3 tag 処理は切り離しました.
setradiko.sh に.

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