Skip to content

Instantly share code, notes, and snippets.

@ihsoy-s
Forked from matchy256/rec_radiko.sh
Last active January 11, 2019 15:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ihsoy-s/5292735 to your computer and use it in GitHub Desktop.
Save ihsoy-s/5292735 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 下記を参考にしつつ作成
# https://gist.github.com/matchy2/3956266
# https://gist.github.com/saiten/1185755
if [ $# -eq 1 ]; then
channel=$1
case $1 in
r1) playpath='NetRadio_R1_flash@63346' ;;
r2) playpath='NetRadio_R2_flash@63342' ;;
fm) playpath='NetRadio_FM_flash@63343' ;;
*) exit 1 ;;
esac
else
echo "usage : $0 channel_name"
echo " channel_name list"
echo " NHK Radio #1: r1"
echo " NHK Radio #2: r2"
echo " NHK-FM: fm"
exit 1
fi
#
# parameter setting
#
playerurl="http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf"
rtmpurl="rtmpe://netradio-${channel}-flash.nhk.jp/live/${playpath}"
buffer=1000
#
# rtmpdump and mplayer
#
rtmpdump \
--rtmp "${rtmpurl}" \
--swfVfy ${playerurl} \
--live \
--buffer ${buffer} \
--flv - \
| mplayer -
#!/bin/bash
# https://gist.github.com/matchy2/3956266 を若干修正
if [ $# -eq 1 ]; then
channel=$1
else
echo "usage : $0 channel_name"
echo " channel_name list"
echo " TBS Radio: TBS"
echo " Nippon Cultural Broadcasting: QRR"
echo " Nippon Broadcasting: LFR"
echo " Radio Nippon: JORF"
echo " Inter FM: INT"
echo " Tokyo FM: FMT"
echo " J-WAVE: FMJ"
echo " bayfm 78.0MHz: BAYFM78"
echo " NACK5: NACK5"
echo " FM yokohama 84.7: YFM"
exit 1
fi
#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
outdir="${XDG_CACHE_HOME}/radio/"
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
mkdir -p ${outdir}
#
# get player
#
if [ ! -f $playerfile ]; then
wget -q -O $playerfile $playerurl
if [ $? -ne 0 ]; then
echo "failed to get player"
exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
swfextract -b 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed to get keydata"
exit 1
fi
fi
if [ -f ${auth1_fms_file} ]; then
rm -f ${auth1_fms_file}
fi
#
# access auth1_fms
#
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 \
-O ${auth1_fms_file} \
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_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`
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_file}
if [ -f ${auth2_fms_file} ]; then
rm -f ${auth2_fms_file}
fi
#
# access auth2_fms
#
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 \
-O ${auth2_fms_file} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
echo "failed auth2 process"
exit 1
fi
echo "authentication success"
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
echo "areaid: $areaid"
rm -f ${auth2_fms_file}
#
# get stream-url
#
if [ -f ${channel_file} ]; then
rm -f ${channel_file}
fi
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
rm -f ${channel_file}
#
# rtmpdump and mplayer
#
rtmpdump -v \
-r ${url_parts[0]} \
--app ${url_parts[1]} \
--playpath ${url_parts[2]} \
-W $playerurl \
-C S:"" -C S:"" -C S:"" -C S:$authtoken \
--live\
| mplayer -
#!/bin/bash
#
# mmsなストリーミングのasxファイルを読み込み、ストリームをdumpする
# 1. asxファイルをダウンロード
# 2. asxファイルのなかからURL(mms://~)を取得
# 3. mplayerでdump
#
if [ $# -eq 1 ]; then
url=$1
outdir="${XDG_CACHE_HOME}/radio/"
elif [ $# -eq 2 ]; then
url=$1
outdir=$2
else
echo "usage : $0 url [outputdir]"
exit 1
fi
#
# parameter setting
#
pid=$$
tmpdir="${XDG_CACHE_HOME}/radio/"
asxfile="${tmpdir}/${url##*/}"
mkdir -p ${tmpdir}
mkdir -p ${outdir}
#
# get asx file
#
if [ ! -f ${asxfile} ]; then
wget -q -O ${asxfile} ${url}
if [ $? -ne 0 ]; then
echo "failed to get ${url}"
exit 1
fi
fi
#
# parse asx file
#
mmsurl=`nkf -w ${asxfile} | gawk '/href=/ {print $0}' | sed -e 's/.*\(mms\:\/\/[^">]\+\).*/\1/'`
# 出力ファイル名はasxファイルのファイル名に合わせる
outputfile="${asxfile%.*}.${mmsurl##*.}"
#
# dump
#
mplayer ${mmsurl} -dumpstream -dumpfile ${outputfile}
if [ $? = 0 ]; then
rm -f ${asxfile}
fi
#!/bin/bash
# 下記を参考にしつつ作成
# https://gist.github.com/matchy2/3956266
# https://gist.github.com/saiten/1185755
if [ $# -eq 2 ]; then
channel=$1
duration=`expr $2 \* 60`
outputprefix="NHK-${channel}"
outdir="${XDG_CACHE_HOME}/radio/"
elif [ $# -eq 3 ]; then
channel=$1
duration=`expr $2 \* 60`
outputprefix=$3
outdir="${XDG_CACHE_HOME}/radio/"
elif [ $# -eq 4 ]; then
channel=$1
duration=`expr $2 \* 60`
outputprefix=$3
outdir=$4
else
echo "usage : $0 channel_name duration(minuites) [filename_prefix [output_dir]]"
echo " channel_name list"
echo " NHK Radio #1: r1"
echo " NHK Radio #2: r2"
echo " NHK-FM: fm"
exit 1
fi
#
# parameter setting
#
case ${channel} in
r1) playpath='NetRadio_R1_flash@63346' ;;
r2) playpath='NetRadio_R2_flash@63342' ;;
fm) playpath='NetRadio_FM_flash@63343' ;;
*) exit 1 ;;
esac
date=`date '+%Y-%m-%d-%H%M'`
playerurl="http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf"
rtmpurl="rtmpe://netradio-${channel}-flash.nhk.jp/live/${playpath}"
buffer=1000
mkdir -p ${outdir}
#
# rtmpdump
#
rtmpdump --quiet \
--rtmp "${rtmpurl}" \
--swfVfy ${playerurl} \
--live \
--buffer ${buffer} \
--stop ${duration} \
--flv - > "${outdir}/NHK-${channel}_${date}"
# mp3 encode
#ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${outputprefix}_${date}.mp3"
# ogg encode
#ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"
# dump stream (AAC)
#ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"
avconv -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.m4a"
if [ $? = 0 ]; then
rm -f "${outdir}/NHK-${channel}_${date}"
fi
#!/bin/bash
# https://gist.github.com/matchy2/3956266 を若干修正
if [ $# -eq 2 ]; then
channel=$1
DURATION=`expr $2 \* 60`
outdir="${XDG_CACHE_HOME}/radio/"
outputprefix=${channel}
elif [ $# -eq 3 ]; then
channel=$1
DURATION=`expr $2 \* 60`
outputprefix=$3
outdir="${XDG_CACHE_HOME}/radio/"
elif [ $# -eq 4 ]; then
channel=$1
DURATION=`expr $2 \* 60`
outputprefix=$3
outdir=$4
else
echo "usage : $0 channel_name duration(minutes) [filename_prefix [output_dir]]"
echo " channel_name list"
echo " TBS Radio: TBS"
echo " Nippon Cultural Broadcasting: QRR"
echo " Nippon Broadcasting: LFR"
echo " Radio Nippon: JORF"
echo " Inter FM: INT"
echo " Tokyo FM: FMT"
echo " J-WAVE: FMJ"
echo " bayfm 78.0MHz: BAYFM78"
echo " NACK5: NACK5"
echo " FM yokohama 84.7: YFM"
exit 1
fi
#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H%M'`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
mkdir -p ${outdir}
#
# 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 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata"
exit 1
fi
fi
if [ -f ${auth1_fms_file} ]; then
rm -f ${auth1_fms_file}
fi
#
# access auth1_fms
#
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 \
-O ${auth1_fms_file} \
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_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`
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_file}
if [ -f ${auth2_fms_file} ]; then
rm -f ${auth2_fms_file}
fi
#
# access auth2_fms
#
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 \
-O ${auth2_fms_file} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
echo "failed auth2 process"
exit 1
fi
echo "authentication success"
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
echo "areaid: $areaid"
rm -f ${auth2_fms_file}
#
# get stream-url
#
if [ -f ${channel_file} ]; then
rm -f ${channel_file}
fi
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
rm -f ${channel_file}
#
# rtmpdump
#
rtmpdump -v -q \
-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 "${outdir}//${channel}_${date}"
# mp3 encode
#ffmpeg -loglevel quiet -y -i "${outdir}/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${outputprefix}_${date}.mp3"
# ogg encode
#ffmpeg -loglevel quiet -y -i "${outdir}/${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"
# 再エンコしない (AAC) 場合
#ffmpeg -loglevel quiet -y -i "${outdir}/${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"
avconv -loglevel quiet -y -i "${outdir}/${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.m4a"
if [ $? = 0 ]; then
rm -f "${outdir}/${channel}_${date}"
fi
@always19
Copy link

always19 commented Apr 8, 2014

Useful gist!
One bug on rec_nhk-radio.sh.
playpath should have been set for the case of $# are more than 2
https://gist.github.com/ihsoy-s/5292735#file-rec_nhk-radio-sh-L10

@Ttatata
Copy link

Ttatata commented Sep 24, 2017

すみません。教えて頂きたいのですが、
ソースコード"play_nhk-radio.sh"をダウンロードさせて頂き、
実行権限を追加して、実行したのですが、
以下のようなログが出力されるのですが、
スピーカの方へラジオが再生されませんでした。

pi@raspberrypi:~/radio $ ./play_nhk-radio.sh r1
RTMPDump v2.4
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
Connecting ...
WARNING: HandShake: Type mismatch: client sent 6, server answered 8
INFO: Connected...
Starting Live Stream
MPlayer 1.3.0 (Debian), built with gcc-6.2.1 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing -.
Reading from stdin...
libavformat version 57.56.101 (external)
Mismatching header version 57.56.100
ERROR: RTMP_ReadPacket, failed to read RTMP packet header
-0.001 kB / 0.00 sec
Download may be incomplete (downloaded about 0.00%), try resuming

Exiting... (End of file)

他のいろいろなサイトも参考にさせて頂き
試したのですが、どこで躓いているか分からない
状態です。
教えて頂ければ幸いです。

[環境]
・Raspberry Pi 3 Model B_Single
・OS: Distributor ID: Raspbian
    Description: Raspbian GNU/Linux 9.1 (stretch)
Release: 9.1
Codename: stretch
・インストール済: rtmpdump swftools libxml2-utils libav-tools mplayer など

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