#!/bin/sh PATH=${PATH}:/usr/local/bin; export PATH LANG=C; export LANG playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf playerfile=/tmp/player.swf keyfile=/tmp/authkey.png timeout=180 outputbase=/tmp/radiko_out outputdateline=$( date -v +1M +%Y%m%d%H%M ) if [ $# -eq 2 ]; then channel=$1 backEndTime=$2 else echo "usage : $0 channel_name back_end" exit 1 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 ]; then rm -f auth1_fms 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 \ 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` offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms` length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms` 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 if [ -f auth2_fms ]; then rm -f auth2_fms 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 \ https://radiko.jp/v2/api/auth2_fms if [ $? -ne 0 -o ! -f auth2_fms ]; then echo "failed auth2 process" exit 1 fi echo "authentication success" areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms` echo "areaid: ${areaid}" rm -f auth2_fms # # rtmpdump # count=0 RET=9 while [ ${RET} -ne 0 ] do count=$(expr ${count} + 1) if [ ${count} -gt 20 ]; then RET=0 break fi output=${outputbase}/${channel}_${outputdateline}_${count}.flv m4a_output=${outputbase}/${channel}_${outputdateline}_${count}.m4a # #get rec time # startUnixTime=$( date +%s ) backEndUnixTime=$( ruby -e "require \"time\";t=Time.parse(\"${backEndTime} JST\"); p t.to_i" ) seconds=$( expr ${backEndUnixTime} - ${startUnixTime} ) if [ ${seconds} -lt 0 ]; then seconds=$( expr 86400 + ${seconds} ) fi rtmpdump -v \ -r "rtmpe://f-radiko.smartstream.ne.jp/" \ --playpath "simul-stream.stream" \ --app "${channel}/_definst_" \ -W ${playerurl} \ -C S:"" -C S:"" -C S:"" -C S:${authtoken} \ --live -B ${seconds} \ --timeout ${timeout} \ --flv ${output} #ffmpeg -loglevel quiet -y -i "${output}" -timestamp now -ab 46k -ar 48k -strict experimental -acodec aac "${m4a_output}" #rm "${output}" RET=$? sleep 6 done ### rm -f ${keyfile} exit ${RET}