7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

らじるを録音(シェルスクリプト)

Last updated at Posted at 2016-09-02

こんにちは。
らじるを録音しようとしてコケた話(認証が必要だった)」という記事を見つけたので、それのシェルスクリプトをほぼそっくり動かしてみました。NHK ラジオ番組の聴取・録音の話題です。

必要なコマンドは事前にインストールしておいてください(下記はhomebrew利用の例です)。

$ brew install wget rtmpdump mplayer
$ brew install --with-tools ffmpeg
$ ./radiru.sh -h

Name: radiru.sh

Usage: radiru.sh [options]

Options:
  --help
  --channel R1|R2|FM        (default R1)
  --branch  AK|BK|CK|HK     (default AK)
  --stop duration_time
  --output outputfile.mp4   or playing lively using mplayer if not specified
radiru.sh
#!/bin/bash

# default values
ch="R1"
br="AK"
area="tokyo"
volume=4

usage_exit() {
  PROGNAME=$(basename $0)
  cat << EOS

Name: $PROGNAME

Usage: $PROGNAME [options]

Options:
  --help
  --channel R1|R2|FM        (default $ch)
  --branch  AK|BK|CK|HK     (default $br)
  --stop duration_time
  --output outputfile.mp4   or playing lively using mplayer if not specified

EOS
  exit 1
}

# analysis of arguments
for OPT in "$@"
do
  case "$OPT" in
    '-h'|'--help' ) usage_exit;;
    '-c'|'--channel' ) ch="$2"; shift 2;;
    '-b'|'--branch' ) br="$2"; shift 2;;
    '-s'|'--stop' ) dur="$2"; shift 2;;
    '-o'|'--output' ) ofile="$2"; shift 2;;
  esac
done

ch=`tr '[a-z]' '[A-Z]' <<< ${ch}`
br=`tr '[a-z]' '[A-Z]' <<< ${br}`
case "$ch" in
  'R2' ) case "$br" in 'AK' ) id=63342;; esac;;
  'R1' ) case "$br" in 'AK' ) id=63346;;  'BK' ) id=108232;;
                       'CK' ) id=108234;; 'HK' ) id=108442;; esac;;
  'FM' ) case "$br" in 'AK' ) id=63343;;  'BK' ) id=108233;;
                       'CK' ) id=108235;; 'HK' ) id=108237;; esac;;
esac
[ -z "$id" ] && usage_exit
[ -z "$dur" ] && [ -n "$ofile" ] && usage_exit
[ "$br" = "AK" ] && br=""
playpath="NetRadio_${br}${ch}_flash@${id}"
pageurl="http://www3.nhk.or.jp/netradio/player/index.html?ch=${br}${ch}&area=${area}"

if [[ -z "$tempdir" ]]; then tempdir=$(mktemp -d "${TMPDIR:-/tmp}"/foo.XXXXXXXXX); fi
swf=${tempdir}/rtmpe_ver2015.swf
uswf=${tempdir}/rtmpe_ver2015_u.swf
swfurl="http://www3.nhk.or.jp/netradio/files/swf/rtmpe_ver2015.swf?tm="`date +"%s%3N"`
wget -nv -O ${swf} ${swfurl}
cws2fws ${swf} ${uswf}
size=`wc -c ${uswf}`
hash=`openssl sha -sha256 -hmac "Genuine Adobe Flash Player 001" ${uswf} | cut -d ' ' -f 2`

if [ -n "$dur" ]; then stopdur="--stop ${dur}"; fi
if [ -z "$ofile" ]; then
  pipecommand="mplayer - -volume ${volume}"
else
  flv="${tempdir}/${ofile}.flv"
  output="--flv ${flv}"
  pipecommand="cat"
fi

rtmpdump --rtmp "rtmpe://netradio-${ch}-flash.nhk.jp/live" \
         --pageUrl "${pageurl}" --playpath "${playpath}" \
         --swfhash "${hash}" --swfsize "${size}" --app "live" --live \
         ${stopdur} ${output} | ${pipecommand}

if [ -n "$ofile" ]; then ffmpeg -v 0 -i ${flv} -c:v copy -acodec copy ${ofile}; fi
if [[ -n "$tempdir" ]]; then rm -rf $tempdir; fi
exit $?
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?