Radikoを予約録音してGoogleMusicに自動的にアップロード
2018/03/10
Radikoを録音するサーバーを作って、楽しく聞いていたのだけど、いちいちサーバーからスマホにデータを移すのがめんどうくさい。
録音したら勝手にスマホからアクセスできるところに移すようにしたいなーと思い、その先としてGoogleMusicを使うことを企てた。
gmusicapi-scriptsをインストール
python3.4以上が必要です。
$ pip3 install gmusicapi-scripts
初回起動時には認証が行われる。
$ gmupload
...
Visit the following url:
https://accounts.google.com/.../
Follow the prompts, then paste the auth code here and hit enter:
こんな風にURLが表示されるのでアクセスしてログインする。 するとアクセストークンが表示されるので画面に入力。
認証が終了したので、試しに音楽をアップロードしてみました。
$ gmupload abc.mp3
MusicManagerWrapper authentication succeeded.
Loading local songs...
Excluded 0 local songs
Filtered 0 local songs
Loaded 1 local songs
Uploading 1 song(s) to Google Music
(1/1) Successfully uploaded -- xxx.mp3 (...)
All done!
確認するとちゃんとアップロードされてましたー。設定は上手くいったみたいです。
Radiko録音スクリプトを修正
録音は先人の方たちがスクリプトを共有してくださっています。
これを自動アップロードに対応させるためにちょっと修正。以下のようになりました。
This file contains hidden or 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 | |
LANG=ja_JP.utf8 | |
pid=$$ | |
date=`date '+%Y-%m-%d-%H_%M'` | |
playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf | |
playerfile="/tmp/player.swf" | |
keyfile="/tmp/authkey.png" | |
if [ $# -le 1 ]; then | |
echo "usage : $0 channel_name duration(minuites) [prefix]" | |
exit 1 | |
fi | |
if [ $# -ge 2 ]; then | |
channel=$1 | |
DURATION=`expr $2 \* 60` | |
fi | |
PREFIX=${channel} | |
if [ $# -ge 3 ]; then | |
PREFIX=$3 | |
fi | |
# | |
# get player | |
# | |
if [ ! -f $playerfile ]; then | |
wget -q -O $playerfile $playerurl | |
if [ $? -ne 0 ]; then | |
echo "failed get player" | |
exit 1 | |
fi | |
fi | |
# | |
# get keydata (need swftool) | |
# | |
if [ ! -f $keyfile ]; then | |
swfextract -b 12 $playerfile -o $keyfile | |
if [ ! -f $keyfile ]; then | |
echo "failed get keydata" | |
exit 1 | |
fi | |
fi | |
if [ -f auth1_fms_${pid} ]; then | |
rm -f auth1_fms_${pid} | |
fi | |
# | |
# access auth1_fms | |
# | |
wget -q \ | |
--header="pragma: no-cache" \ | |
--header="X-Radiko-App: pc_ts" \ | |
--header="X-Radiko-App-Version: 4.0.0" \ | |
--header="X-Radiko-User: test-stream" \ | |
--header="X-Radiko-Device: pc" \ | |
--post-data='\r\n' \ | |
--no-check-certificate \ | |
--save-headers \ | |
-O auth1_fms_${pid} \ | |
https://radiko.jp/v2/api/auth1_fms | |
if [ $? -ne 0 ]; then | |
echo "failed auth1 process" | |
exit 1 | |
fi | |
# | |
# get partial key | |
# | |
authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_${pid}` | |
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_${pid}` | |
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_${pid}` | |
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_${pid} | |
if [ -f auth2_fms_${pid} ]; then | |
rm -f auth2_fms_${pid} | |
fi | |
# | |
# access auth2_fms | |
# | |
wget -q \ | |
--header="pragma: no-cache" \ | |
--header="X-Radiko-App: pc_ts" \ | |
--header="X-Radiko-App-Version: 4.0.0" \ | |
--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 \ | |
-O auth2_fms_${pid} \ | |
https://radiko.jp/v2/api/auth2_fms | |
if [ $? -ne 0 -o ! -f auth2_fms_${pid} ]; then | |
echo "failed auth2 process" | |
exit 1 | |
fi | |
#echo "authentication success" | |
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_${pid}` | |
#echo "areaid: $areaid" | |
rm -f auth2_fms_${pid} | |
# | |
# get stream-url | |
# | |
if [ -f ${channel}.xml ]; then | |
rm -f ${channel}.xml | |
fi | |
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" | |
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel}.xml | tail -2 | head -1` | |
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`) | |
rm -f ${channel}.xml | |
# | |
# rtmpdump | |
# | |
#rtmpdump -q \ | |
rtmpdump \ | |
-r ${url_parts[0]} \ | |
--app ${url_parts[1]} \ | |
--playpath ${url_parts[2]} \ | |
-W $playerurl \ | |
-C S:"" -C S:"" -C S:"" -C S:$authtoken \ | |
--live \ | |
--stop ${DURATION} \ | |
--flv "/tmp/${channel}_${date}" | |
ffmpeg -loglevel quiet -y -i "/tmp/${channel}_${date}" -metadata title="${PREFIX}_${date}" -metadata album="${PREFIX}" -acodec libmp3lame -ab 128k "/tmp/${channel}_${date}.mp3" | |
~/.local/bin/gmupload /tmp/${channel}_${date}.mp3 | |
if [ $? = 0 ]; then | |
rm -f "/tmp/${channel}_${date}" | |
rm -f "/tmp/${channel}_${date}.mp3" | |
fi |
ffmpeg
で変換するときにタグを付けるようにしています。付けとかないとGoogleMusicでは「不明なアルバム」とか表示されて分かりにくくなってしまいます。
cronに登録
これで下準備はできたので、指定した時間にスクリプトが実行されるようcronに登録します。
crontab -e
で例えばこんな感じに。
59 0 * * 3 ~/rec_radiko.sh TBS 120 "爆笑問題カーボーイ"
59 0 * * 0 ~/rec_radiko.sh LFR 120 "オードリーのオールナイトニッポン"
録音は1分前に開始するようにしてます。
もちろん番組は皆さんが好きなように変えましょう。