Skip to content

Instantly share code, notes, and snippets.

@Mossuru777
Forked from ihsoy-s/play_nhk-radio.sh
Last active December 19, 2015 14:49
Show Gist options
  • Select an option

  • Save Mossuru777/5972196 to your computer and use it in GitHub Desktop.

Select an option

Save Mossuru777/5972196 to your computer and use it in GitHub Desktop.

Revisions

  1. Mossuru777 revised this gist Jul 11, 2013. 5 changed files with 61 additions and 398 deletions.
    42 changes: 0 additions & 42 deletions play_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -1,42 +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
    #
    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 -
    106 changes: 61 additions & 45 deletions play_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,22 @@
    #!/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
    # https://gist.github.com/ihsoy-s/5292735 を参考にしました

    channel="$1"

    #
    # 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/"
    outdir=`mktemp -d`
    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"
    station_list_file="${outdir}/station_list.xml"
    mkdir -p ${outdir}

    #
    @@ -58,10 +43,6 @@ if [ ! -f $keyfile ]; then
    fi
    fi

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

    #
    # access auth1_fms
    #
    @@ -88,17 +69,8 @@ fi
    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
    #
    @@ -119,37 +91,81 @@ if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
    echo "failed auth2 process"
    exit 1
    fi
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`

    echo "authentication success"
    #
    # Fetch Station List
    #
    wget -q -O ${station_list_file} http://radiko.jp/v2/station/list/${areaid}.xml
    areaname="`echo 'cat /stations/@area_name' | xmllint --shell ${station_list_file} | tail -2 | head -1 | sed -e 's@.*\"\(.*\)\"@\1@'`"

    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
    echo "areaid: $areaid"
    _IFS=$IFS
    IFS=$'\n'
    if [ "$LANG" = "ja_JP.UTF-8" ]
    then
    stationname=( `echo -n 'cat /stations/station/name' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' | perl -pe 's/<name>(.*)<\/name>\n/$1\n/; s/ +\n/\n/;'`)
    else
    stationname=( `echo -n 'cat /stations/station/ascii_name' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' | perl -pe 's/<ascii_name>(.*)<\/ascii_name>\n/$1\n/; s/ +\n/\n/;'` )
    fi

    rm -f ${auth2_fms_file}
    stationcd=( `echo -n 'cat /stations/station/id' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' -e 's@<id>\([^ \n]*\)</id>@\1\n@g'` )
    IFS=$_IFS

    #
    # get stream-url
    # Argument Check
    #

    if [ -f ${channel_file} ]; then
    rm -f ${channel_file}
    canlisten=0
    if [ ${channel} ]; then
    for i in `seq 1 ${#stationcd[*]}`
    do
    if [ "${stationcd[${i}]}" = "${channel}" ]; then
    echo " Area: ${areaname} (${areaid})"
    echo "Station: ${stationname[${i-1}]} (${channel})"
    canlisten=1
    break
    fi
    done
    fi
    if [ ${canlisten} = 0 ]; then
    if [ ${channel} ]; then
    echo "invalid station code."
    fi
    echo -e "usage : $0 channel_name\n"
    echo "*** ${areaname} (${areaid}) - channel_name List ***"
    for i in `seq 1 ${#stationcd[*]}`
    do
    printf "%20s : %s\n" "${stationcd[${i}-1]}" "${stationname[${i}-1]}"
    done
    exit 1
    fi

    #
    # get stream-url
    #
    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}
    #
    # Delete Temporary Directory
    #
    rm -rf ${outdir}

    #
    # rtmpdump and mplayer
    #
    rtmpdump -v \
    rtmpdump -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\
    | mplayer -
    | mplayer - >/dev/null 2>&1 &

    #
    # set trap
    #
    MPLAYER_PID=$!
    trap "kill $MPLAYER_PID; exit 0" 1 2 3 15
    wait $MPLAYER_PID
    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=`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: 0 additions & 71 deletions rec_nhk-radio.sh
    Original file line number Diff line number Diff line change
    @@ -1,71 +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`
    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 -q \
    -r "${rtmpurl}" \
    --app "${targetapp}" \
    --playpath "${playpath}" \
    -W $playerurl \
    --live\
    --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) 場合
    # ffmpeg -loglevel quiet -y -i "${outdir}/NHK-${channel}_${date}" -acodec copy "${outdir}/${outputprefix}_${date}.aac"

    if [ $? = 0 ]; then
    rm -f "${outdir}/${channel}_${date}"
    fi
    181 changes: 0 additions & 181 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,181 +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"

    if [ $? = 0 ]; then
    rm -f "${outdir}/${channel}_${date}"
    fi
  2. @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}"
  3. @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.
  4. @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
  5. @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
  6. @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
  7. @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
  8. @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"
  9. @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}"
  10. @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 \
  11. @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
  12. @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