Skip to content

Instantly share code, notes, and snippets.

@macrat
Forked from ihsoy-s/play_nhk-radio.sh
Last active August 28, 2016 15:21

Revisions

  1. macrat revised this gist Aug 28, 2016. 5 changed files with 6 additions and 374 deletions.
    40 changes: 0 additions & 40 deletions play_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -1,40 +0,0 @@
    #!/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 -
    29 changes: 6 additions & 23 deletions play_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #!/bin/bash

    # https://gist.github.com/matchy2/3956266 を若干修正

    if [ $# -eq 1 ]; then
    channel=$1
    else
    @@ -25,8 +23,8 @@ fi
    #
    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/"
    playerurl=http://radiko.jp/player/swf/player_4.1.0.00.swf
    outdir="${XDG_CACHE_HOME:-/tmp}/radio"
    playerfile="${outdir}/player.swf"
    keyfile="${outdir}/authkey.png"
    auth1_fms_file="${outdir}/auth1_fms_${pid}"
    @@ -91,7 +89,7 @@ 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"
    printf "authtoken: ${authtoken}\noffset: ${offset} length: ${length}\npartialkey: $partialkey\n"

    rm -f ${auth1_fms_file}

    @@ -127,28 +125,13 @@ 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]} \
    -r 'rtmpe://f-radiko.smartstream.ne.jp' \
    --app "${channel}/_definst_" \
    --playpath 'simul-stream.stream' \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live\
    59 changes: 0 additions & 59 deletions rec_mms.sh
    Original file line number Diff line number Diff line change
    @@ -1,59 +0,0 @@
    #!/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
    70 changes: 0 additions & 70 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -1,70 +0,0 @@
    #!/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
    182 changes: 0 additions & 182 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,182 +0,0 @@
    #!/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
  2. @ihsoy-s ihsoy-s revised this gist May 5, 2014. 2 changed files with 28 additions and 32 deletions.
    18 changes: 8 additions & 10 deletions play_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -24,19 +24,17 @@ fi
    #
    # parameter setting
    #
    pid=$$
    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"
    targetapp="live"
    rtmpurl="rtmpe://netradio-${channel}-flash.nhk.jp/live/${playpath}"
    buffer=1000

    #
    # rtmpdump and mplayer
    #
    rtmpdump -v \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    -W $playerurl \
    --live\
    rtmpdump \
    --rtmp "${rtmpurl}" \
    --swfVfy ${playerurl} \
    --live \
    --buffer ${buffer} \
    --flv - \
    | mplayer -
    42 changes: 20 additions & 22 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -6,23 +6,17 @@

    if [ $# -eq 2 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    case $1 in
    r1) playpath='NetRadio_R1_flash@63346' ;;
    r2) playpath='NetRadio_R2_flash@63342' ;;
    fm) playpath='NetRadio_FM_flash@63343' ;;
    *) exit 1 ;;
    esac
    duration=`expr $2 \* 60`
    outputprefix="NHK-${channel}"
    outdir="${XDG_CACHE_HOME}/radio/"
    elif [ $# -eq 3 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    duration=`expr $2 \* 60`
    outputprefix=$3
    outdir="${XDG_CACHE_HOME}/radio/"
    elif [ $# -eq 4 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    duration=`expr $2 \* 60`
    outputprefix=$3
    outdir=$4
    else
    @@ -37,36 +31,40 @@ fi
    #
    # parameter setting
    #
    pid=$$
    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"
    targetapp="live"
    rtmpurl="rtmpe://netradio-${channel}-flash.nhk.jp/live/${playpath}"
    buffer=1000
    mkdir -p ${outdir}


    #
    # rtmpdump
    #
    rtmpdump -v -q \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    -W $playerurl \
    --live\
    --stop ${DURATION} \
    --flv "${outdir}/NHK-${channel}_${date}"
    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"

    # 再エンコしない (AAC) 場合
    # 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}/${channel}_${date}"
    rm -f "${outdir}/NHK-${channel}_${date}"
    fi
  3. @ihsoy-s ihsoy-s revised this gist Apr 27, 2014. 3 changed files with 8 additions and 6 deletions.
    2 changes: 1 addition & 1 deletion rec_mms.sh
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ fi
    #
    # parse asx file
    #
    mmsurl=`gawk '/href=/ {print $0}' ${asxfile} | sed -e 's/.*\(mms\:\/\/[^">]\+\).*/\1/'`
    mmsurl=`nkf -w ${asxfile} | gawk '/href=/ {print $0}' | sed -e 's/.*\(mms\:\/\/[^">]\+\).*/\1/'`

    # 出力ファイル名はasxファイルのファイル名に合わせる
    outputfile="${asxfile%.*}.${mmsurl##*.}"
    7 changes: 4 additions & 3 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ fi
    # parameter setting
    #
    pid=$$
    date=`date '+%Y-%m-%d-%H:%M'`
    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"
    targetapp="live"
    @@ -61,10 +61,11 @@ rtmpdump -v -q \
    #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"
    #ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"

    # 再エンコしない (AAC) 場合
    # ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.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}/${channel}_${date}"
    5 changes: 3 additions & 2 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ fi
    # parameter setting
    #
    pid=$$
    date=`date '+%Y-%m-%d-%H:%M'`
    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"
    @@ -174,7 +174,8 @@ rtmpdump -v -q \
    #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"
    #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}"
  4. @ihsoy-s ihsoy-s revised this gist Apr 2, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ mkdir -p ${outdir}
    #
    # rtmpdump
    #
    rtmpdump -v \
    rtmpdump -v -q \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    @@ -58,13 +58,13 @@ rtmpdump -v \
    --flv "${outdir}/NHK-${channel}_${date}"

    # mp3 encode
    #ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${outputprefix}_${date}.mp3"
    #ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${outputprefix}_${date}.mp3"

    # ogg encode
    ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"
    ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"

    # 再エンコしない (AAC) 場合
    # ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"
    # ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"

    if [ $? = 0 ]; then
    rm -f "${outdir}/${channel}_${date}"
  5. @ihsoy-s ihsoy-s revised this gist Apr 2, 2013. 5 changed files with 327 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions play_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/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
    #
    pid=$$
    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"
    targetapp="live"

    #
    # rtmpdump and mplayer
    #
    rtmpdump -v \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    -W $playerurl \
    --live\
    | mplayer -
    155 changes: 155 additions & 0 deletions play_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,155 @@
    #!/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 -
    59 changes: 59 additions & 0 deletions rec_mms.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/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=`gawk '/href=/ {print $0}' ${asxfile} | sed -e 's/.*\(mms\:\/\/[^">]\+\).*/\1/'`

    # 出力ファイル名はasxファイルのファイル名に合わせる
    outputfile="${asxfile%.*}.${mmsurl##*.}"

    #
    # dump
    #
    mplayer ${mmsurl} -dumpstream -dumpfile ${outputfile}


    if [ $? = 0 ]; then
    rm -f ${asxfile}
    fi
    71 changes: 71 additions & 0 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #!/bin/bash

    # 下記を参考にしつつ作成
    # https://gist.github.com/matchy2/3956266
    # https://gist.github.com/saiten/1185755

    if [ $# -eq 2 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    case $1 in
    r1) playpath='NetRadio_R1_flash@63346' ;;
    r2) playpath='NetRadio_R2_flash@63342' ;;
    fm) playpath='NetRadio_FM_flash@63343' ;;
    *) exit 1 ;;
    esac
    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
    #
    pid=$$
    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"
    targetapp="live"
    mkdir -p ${outdir}


    #
    # rtmpdump
    #
    rtmpdump -v \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    -W $playerurl \
    --live\
    --stop ${DURATION} \
    --flv "${outdir}/NHK-${channel}_${date}"

    # mp3 encode
    #ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${outputprefix}_${date}.mp3"

    # ogg encode
    ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec libvorbis -aq 3 "${outdir}/${outputprefix}_${date}.ogg"

    # 再エンコしない (AAC) 場合
    # ffmpeg -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"

    if [ $? = 0 ]; then
    rm -f "${outdir}/${channel}_${date}"
    fi
    Empty file modified rec_radiko.sh
    100644 → 100755
    Empty file.
  6. @ihsoy-s ihsoy-s revised this gist Apr 2, 2013. 1 changed file with 66 additions and 33 deletions.
    99 changes: 66 additions & 33 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,51 @@
    #!/bin/sh
    #!/bin/bash

    pid=$$
    date=`date '+%Y-%m-%d-%H:%M'`
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile="/tmp/player.swf"
    keyfile="/tmp/authkey.png"
    # https://gist.github.com/matchy2/3956266 を若干修正

    if [ $# -eq 2 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    outdir="."
    outdir="${XDG_CACHE_HOME}/radio/"
    outputprefix=${channel}
    elif [ $# -eq 3 ]; then
    channel=$1
    DURATION=`expr $2 \* 60`
    outdir=$3
    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) [outputdir]"
    exit 1
    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
    #
    @@ -43,8 +70,8 @@ if [ ! -f $keyfile ]; then
    fi
    fi

    if [ -f auth1_fms_${pid} ]; then
    rm -f auth1_fms_${pid}
    if [ -f ${auth1_fms_file} ]; then
    rm -f ${auth1_fms_file}
    fi

    #
    @@ -59,7 +86,7 @@ wget -q \
    --post-data='\r\n' \
    --no-check-certificate \
    --save-headers \
    -O auth1_fms_${pid} \
    -O ${auth1_fms_file} \
    https://radiko.jp/v2/api/auth1_fms

    if [ $? -ne 0 ]; then
    @@ -70,18 +97,18 @@ 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}`
    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_${pid}
    rm -f ${auth1_fms_file}

    if [ -f auth2_fms_${pid} ]; then
    rm -f auth2_fms_${pid}
    if [ -f ${auth2_fms_file} ]; then
    rm -f ${auth2_fms_file}
    fi

    #
    @@ -97,52 +124,58 @@ wget -q \
    --header="X-Radiko-Partialkey: ${partialkey}" \
    --post-data='\r\n' \
    --no-check-certificate \
    -O auth2_fms_${pid} \
    -O ${auth2_fms_file} \
    https://radiko.jp/v2/api/auth2_fms

    if [ $? -ne 0 -o ! -f auth2_fms_${pid} ]; then
    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_${pid}`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
    echo "areaid: $areaid"

    rm -f auth2_fms_${pid}
    rm -f ${auth2_fms_file}

    #
    # get stream-url
    #

    if [ -f ${channel}.xml ]; then
    rm -f ${channel}.xml
    if [ -f ${channel_file} ]; then
    rm -f ${channel_file}
    fi

    wget -q "http://radiko.jp/v2/station/stream/${channel}.xml"
    wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}

    stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel}.xml | tail -2 | head -1`
    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}.xml
    rm -f ${channel_file}

    #
    # rtmpdump
    #
    rtmpdump -v \
    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 "/tmp/${channel}_${date}"
    --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"

    ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${channel}_${date}.mp3"
    # 再エンコしない (AAC) 場合
    # ffmpeg -y -i "/tmp/${channel}_${date}" -acodec copy "${outdir}/${channel}_${date}.aac"
    ffmpeg -loglevel quiet -y -i "${outdir}/${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"

    if [ $? = 0 ]; then
    rm -f "/tmp/${channel}_${date}"
    rm -f "${outdir}/${channel}_${date}"
    fi
  7. @matchy256 matchy256 revised this gist Feb 13, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -141,6 +141,8 @@ rtmpdump -v \
    --flv "/tmp/${channel}_${date}"

    ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${channel}_${date}.mp3"
    # 再エンコしない (AAC) 場合
    # ffmpeg -y -i "/tmp/${channel}_${date}" -acodec copy "${outdir}/${channel}_${date}.aac"
    if [ $? = 0 ]; then
    rm -f "/tmp/${channel}_${date}"
    fi
  8. @matchy256 matchy256 revised this gist Nov 19, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -141,4 +141,6 @@ rtmpdump -v \
    --flv "/tmp/${channel}_${date}"

    ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${channel}_${date}.mp3"
    rm -f "/tmp/${channel}_${date}"
    if [ $? = 0 ]; then
    rm -f "/tmp/${channel}_${date}"
    fi
  9. @matchy256 matchy256 revised this gist Oct 26, 2012. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/bin/sh

    pid=$$
    date=`date '+%Y-%m-%d-%H:%M'`
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile="/tmp/player.swf"
    @@ -42,8 +43,8 @@ if [ ! -f $keyfile ]; then
    fi
    fi

    if [ -f auth1_fms_${channel} ]; then
    rm -f auth1_fms_${channel}
    if [ -f auth1_fms_${pid} ]; then
    rm -f auth1_fms_${pid}
    fi

    #
    @@ -58,7 +59,7 @@ wget -q \
    --post-data='\r\n' \
    --no-check-certificate \
    --save-headers \
    -O auth1_fms_${channel} \
    -O auth1_fms_${pid} \
    https://radiko.jp/v2/api/auth1_fms

    if [ $? -ne 0 ]; then
    @@ -69,18 +70,18 @@ fi
    #
    # get partial key
    #
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_${channel}`
    offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_${channel}`
    length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_${channel}`
    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_${channel}
    rm -f auth1_fms_${pid}

    if [ -f auth2_fms_${channel} ]; then
    rm -f auth2_fms_${channel}
    if [ -f auth2_fms_${pid} ]; then
    rm -f auth2_fms_${pid}
    fi

    #
    @@ -96,20 +97,20 @@ wget -q \
    --header="X-Radiko-Partialkey: ${partialkey}" \
    --post-data='\r\n' \
    --no-check-certificate \
    -O auth2_fms_${channel} \
    -O auth2_fms_${pid} \
    https://radiko.jp/v2/api/auth2_fms

    if [ $? -ne 0 -o ! -f auth2_fms_${channel} ]; then
    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_${channel}`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_${pid}`
    echo "areaid: $areaid"

    rm -f auth2_fms_${channel}
    rm -f auth2_fms_${pid}

    #
    # get stream-url
  10. @matchy256 matchy256 revised this gist Oct 26, 2012. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -42,8 +42,8 @@ if [ ! -f $keyfile ]; then
    fi
    fi

    if [ -f auth1_fms_$channel ]; then
    rm -f auth1_fms_$channel
    if [ -f auth1_fms_${channel} ]; then
    rm -f auth1_fms_${channel}
    fi

    #
    @@ -58,7 +58,7 @@ wget -q \
    --post-data='\r\n' \
    --no-check-certificate \
    --save-headers \
    -O auth1_fms_$channel \
    -O auth1_fms_${channel} \
    https://radiko.jp/v2/api/auth1_fms

    if [ $? -ne 0 ]; then
    @@ -69,18 +69,18 @@ fi
    #
    # get partial key
    #
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_$channel`
    offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_$channel`
    length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_$channel`
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_${channel}`
    offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_${channel}`
    length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_${channel}`

    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_$channel
    rm -f auth1_fms_${channel}

    if [ -f auth2_fms_$channel ]; then
    rm -f auth2_fms_$channel
    if [ -f auth2_fms_${channel} ]; then
    rm -f auth2_fms_${channel}
    fi

    #
    @@ -96,20 +96,20 @@ wget -q \
    --header="X-Radiko-Partialkey: ${partialkey}" \
    --post-data='\r\n' \
    --no-check-certificate \
    -O auth2_fms_$channel \
    -O auth2_fms_${channel} \
    https://radiko.jp/v2/api/auth2_fms

    if [ $? -ne 0 -o ! -f auth2_fms_$channel ]; then
    if [ $? -ne 0 -o ! -f auth2_fms_${channel} ]; then
    echo "failed auth2 process"
    exit 1
    fi

    echo "authentication success"

    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_$channel`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_${channel}`
    echo "areaid: $areaid"

    rm -f auth2_fms_$channel
    rm -f auth2_fms_${channel}

    #
    # get stream-url
    @@ -136,7 +136,7 @@ rtmpdump -v \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --stop $DURATION \
    --stop ${DURATION} \
    --flv "/tmp/${channel}_${date}"

    ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${channel}_${date}.mp3"
  11. @matchy256 matchy256 revised this gist Oct 26, 2012. 1 changed file with 28 additions and 19 deletions.
    47 changes: 28 additions & 19 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,20 @@
    #!/bin/sh

    date=`date '+%Y-%m-%d-%H:%M'`
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile=./player.swf
    keyfile=./authkey.png
    playerfile="/tmp/player.swf"
    keyfile="/tmp/authkey.png"

    if [ $# -eq 1 ]; then
    if [ $# -eq 2 ]; then
    channel=$1
    output=./$1.flv
    elif [ $# -eq 2 ]; then
    DURATION=`expr $2 \* 60`
    outdir="."
    elif [ $# -eq 3 ]; then
    channel=$1
    output=$2
    DURATION=`expr $2 \* 60`
    outdir=$3
    else
    echo "usage : $0 channel_name [outputfile]"
    echo "usage : $0 channel_name duration(minuites) [outputdir]"
    exit 1
    fi

    @@ -39,8 +42,8 @@ if [ ! -f $keyfile ]; then
    fi
    fi

    if [ -f auth1_fms ]; then
    rm -f auth1_fms
    if [ -f auth1_fms_$channel ]; then
    rm -f auth1_fms_$channel
    fi

    #
    @@ -55,6 +58,7 @@ wget -q \
    --post-data='\r\n' \
    --no-check-certificate \
    --save-headers \
    -O auth1_fms_$channel \
    https://radiko.jp/v2/api/auth1_fms

    if [ $? -ne 0 ]; then
    @@ -65,18 +69,18 @@ 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`
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_$channel`
    offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_$channel`
    length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_$channel`

    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
    rm -f auth1_fms_$channel

    if [ -f auth2_fms ]; then
    rm -f auth2_fms
    if [ -f auth2_fms_$channel ]; then
    rm -f auth2_fms_$channel
    fi

    #
    @@ -92,19 +96,20 @@ wget -q \
    --header="X-Radiko-Partialkey: ${partialkey}" \
    --post-data='\r\n' \
    --no-check-certificate \
    -O auth2_fms_$channel \
    https://radiko.jp/v2/api/auth2_fms

    if [ $? -ne 0 -o ! -f auth2_fms ]; then
    if [ $? -ne 0 -o ! -f auth2_fms_$channel ]; then
    echo "failed auth2 process"
    exit 1
    fi

    echo "authentication success"

    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_$channel`
    echo "areaid: $areaid"

    rm -f auth2_fms
    rm -f auth2_fms_$channel

    #
    # get stream-url
    @@ -131,4 +136,8 @@ rtmpdump -v \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --flv $output
    --stop $DURATION \
    --flv "/tmp/${channel}_${date}"

    ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${channel}_${date}.mp3"
    rm -f "/tmp/${channel}_${date}"
  12. @saiten saiten revised this gist Oct 24, 2012. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile=./player.swf
    keyfile=./authkey.png

    @@ -31,7 +31,7 @@ fi
    # get keydata (need swftool)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $playerfile -o $keyfile
    swfextract -b 14 $playerfile -o $keyfile

    if [ ! -f $keyfile ]; then
    echo "failed get keydata"
    @@ -106,13 +106,28 @@ echo "areaid: $areaid"

    rm -f auth2_fms

    #
    # 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 -v \
    -r "rtmpe://radiko.smartstream.ne.jp" \
    --playpath "simul-stream" \
    --app "${channel}/_defInst_" \
    -r ${url_parts[0]} \
    --app ${url_parts[1]} \
    --playpath ${url_parts[2]} \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
  13. @saiten saiten revised this gist Apr 4, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ if [ ! -f $playerfile ]; then
    fi

    #
    # get keydata (need swftools)
    # get keydata (need swftool)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $playerfile -o $keyfile
    @@ -65,9 +65,9 @@ fi
    #
    # get partial key
    #
    authtoken=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)'`
    offset=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)'`
    length=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)'`
    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`

    @@ -101,7 +101,7 @@ fi

    echo "authentication success"

    areaid=`cat auth2_fms | perl -ne 'print $1 if(/^([^,]+),/i)'`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms`
    echo "areaid: $areaid"

    rm -f auth2_fms
  14. @saiten saiten created this gist Mar 18, 2011.
    119 changes: 119 additions & 0 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    #!/bin/sh

    playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf
    playerfile=./player.swf
    keyfile=./authkey.png

    if [ $# -eq 1 ]; then
    channel=$1
    output=./$1.flv
    elif [ $# -eq 2 ]; then
    channel=$1
    output=$2
    else
    echo "usage : $0 channel_name [outputfile]"
    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 swftools)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $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_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 \
    https://radiko.jp/v2/api/auth1_fms

    if [ $? -ne 0 ]; then
    echo "failed auth1 process"
    exit 1
    fi

    #
    # get partial key
    #
    authtoken=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)'`
    offset=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)'`
    length=`cat auth1_fms | 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

    if [ -f auth2_fms ]; then
    rm -f auth2_fms
    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 \
    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=`cat auth2_fms | perl -ne 'print $1 if(/^([^,]+),/i)'`
    echo "areaid: $areaid"

    rm -f auth2_fms

    #
    # rtmpdump
    #
    rtmpdump -v \
    -r "rtmpe://radiko.smartstream.ne.jp" \
    --playpath "simul-stream" \
    --app "${channel}/_defInst_" \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --flv $output