Created
August 5, 2016 09:11
-
-
Save takuya/3ccbb52cb135adae72b4b1c1b3e3b019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding:utf-8 | |
from pprint import pprint | |
pp = pprint | |
import os | |
import datetime | |
import sys | |
import urllib2 | |
import subprocess | |
import base64 | |
from IPython import embed | |
debugger = embed | |
import argparse | |
channel_csv = u""" | |
fm, 63343,NHK-FM | |
r2, 63342,NKH-第2 | |
r1, 63346,NHK-第1 | |
bkfm, 108233,大阪FM | |
bkr1, 108232,大阪第1 | |
ckfm, 108235,名古屋FM | |
ckr1, 108234,名古屋第1 | |
hkr1, 108442,仙台第1 | |
hkfm, 108237,仙台FM | |
"""[1:-1] | |
channel_name_list = {} | |
channel_num_list = {} | |
channels = {} | |
channel_name_help = u'チャンネル' | |
lines = channel_csv.split('\n') | |
for line in lines : | |
row = line.split(',') | |
row = [x.strip() for x in row ] | |
channels[ row[0] ] = [ row[0], row[1] ] | |
key = row[2] | |
channel_name_list[key] = row[0] | |
channel_num_list[key] = row[1] | |
channel_name_help = channel_name_help + "\n" + u'%s : %s' % ( key+' '*( 10-len(key)*2), row[0] ) | |
# print channel_name_help | |
# オプションのパース | |
parser = argparse.ArgumentParser( | |
description=u'NHKらじる★らじるの再生と保存', | |
usage='%(prog)s [options]', | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
epilog=channel_name_help) | |
parser.add_argument( '-c ', '--channel', action="store", help=u'チャンネル') | |
parser.add_argument( '-t ', '--duration', action="store", help=u'録音時間(デフォルト1800)',default=1800) | |
parser.add_argument( '-o ', '--output', action="store", help=u'出力ファイル名(省略時はmplayerで再生)') | |
args = parser.parse_args() | |
args = vars(args) | |
if args['channel'] is None : | |
parser.parse_args(['-h']) | |
exit(1) | |
channel = args['channel'] | |
duration = args['duration'] | |
output = args['output'] | |
# コマンドの実行 | |
# | |
# rtmpdump | |
# | |
param = { | |
'channel' : channel, | |
'channel_num' : channels[channel][1], | |
'channel_upcase' : channel.upper(), | |
'time_length' : duration, | |
} | |
cmd = 'rtmpdump --rtmp rtmpe://netradio-{channel}-flash.nhk.jp '\ | |
' --playpath NetRadio_{channel_upcase}_flash@{channel_num}' \ | |
' --app live' \ | |
' -W http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf' \ | |
' --live '\ | |
' -- stop {time_length}' | |
cmd = cmd.format( **param ) | |
cmd = cmd.split(' ') | |
cmd = [ x for x in cmd if x ] | |
if output is None : | |
print( ' '.join( cmd ) ) | |
p1 = subprocess.Popen( cmd, stdout=subprocess.PIPE ) | |
p2 = subprocess.Popen( ['mplayer', '-'], stdin=p1.stdout ) | |
p1.stdout.close() | |
output = p2.communicate()[0] | |
else : | |
cmd.insert(3, '-o') | |
cmd.insert(4, output) | |
cmd = ' '.join( cmd ) | |
print( cmd ) | |
subprocess.call(cmd , shell=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'-- stop {time_length}'
の部分で-- と stop のあいだにスペースがあるので、録音が自動終了しません。