2022年10月にラジコプレミアムのログイン仕様が変更された為、
AYTHKeyGet.vbsに変更があります。
こちらの記事をご参照ください
http://hanpen.lsv.jp/blog.hanpen.net/radiko-jp%e3%81%ae%e3%82%bf%e3%82%a4%e3%83%a0%e3%83%95%e3%83%aa%e3%83%bc%e3%82%92%e4%b8%80%e6%8b%ac%e3%83%80%e3%82%a6%e3%83%b3%e3%83%ad%e3%83%bc%e3%83%89%e3%83%90%e3%83%83%e3%83%812022%e5%b9%b410/
今回はradiko.jpのHLS形式ストリーミングをデバイスを指定して再生する記事を書きたい。
最新のmplayer.exeを使用するとm3u8形式のHLS形式ストリーミングを再生できるようだ。mplayer.exeに-http-header-fieldsを指定するとトークンが送信できる。しかし以前のmplayer.exeよりCPU使用率が高いので注意が必要だ。
TBSを再生するには下記の通り
mplayer.exe -http-header-fields "X-Radiko-AuthToken: ここにトークン" "http://c-radiko.smartstream.ne.jp/TBS/_definst_/simul-stream.stream/playlist.m3u8"
-追記-
放送局によってm3u8ファイルのURLが2種類あるようだ。
“http://c-radiko.smartstream.ne.jp/TBS/_definst_/simul-stream.stream/playlist.m3u8″の最初のサーバーの部分が
f-radiko.smartstream.ne.jpとc-radiko.smartstream.ne.jpの2種類だ。
radiko.jpの視聴ページに移動し画面を広げてF12を押してネットワークモニターを見る。
再生するとリストがずらずら出てくるが目的はm3u8ファイルだけなので検索バーにm3u8を入れると目的のURLが出てくるので右クリックしてURLをコピーする。

“http://f-radiko.smartstream.ne.jp/RN1/_definst_/simul-stream.stream/playlist.m3u8”
ラジオnikkei第1の場合このようなURLがコピーされる。
こちらの記事で仮想オーディオデバイスを追加したり
http://hanpen.lsv.jp/blog.hanpen.net/%e4%bb%ae%e6%83%b3%e3%82%aa%e3%83%bc%e3%83%87%e3%82%a3%e3%82%aa%e3%83%87%e3%83%90%e3%82%a4%e3%82%b9wsaudiodevices%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b/
録音ソフトを検証したりした
http://hanpen.lsv.jp/blog.hanpen.net/radiko-jp%e3%81%ae%e3%82%bf%e3%82%a4%e3%83%a0%e3%83%95%e3%83%aa%e3%83%bc%e3%81%ae%e9%8c%b2%e9%9f%b3%e3%82%bd%e3%83%95%e3%83%88%e3%82%92%e6%a4%9c%e8%a8%bc%e3%81%99%e3%82%8b/
前回の記事で仮想オーディオデバイスを指定してradiko.jpを指定して再生すれば5局まで同時録音できることを説明したが今回、デバイスを指定して再生する事を記事にしたい。
利用にあたって
mplayer
http://www.mplayerhq.hu/design7/dload.html
wget.exe
https://eternallybored.org/misc/wget/
各種実行ファイルを集めて一つのフォルダに置いてください
フォルダを作成して解凍して同じ場所に配置する
下記のバッチファイルを作成する
set ch=TBSでチャンネル設定。
AYTHKeyGet.vbsでAuthTokenの取得を行う。詳しくは後ほど説明する。
set MPLAYER_OPT=-cache 128 -ao dsound:device=1 の任意の数字でデバイスを指定する。サウンドの設定で番号はコロコロ変わるので注意必要だ
-追記-
放送局によってm3u8ファイルのURLが2種類あるようだ。
“http://c-radiko.smartstream.ne.jp/TBS/_definst_/simul-stream.stream/playlist.m3u8″の最初のサーバーの部分が
f-radiko.smartstream.ne.jpとc-radiko.smartstream.ne.jpの2種類だ。
-2018/03/20追記-
5ちゃんねるのradiko関連ソフト総合スレにて指摘がございました。
listen_radiko_TBS.batの末尾に”)”が混入ました。削除して修正します。申し訳ございません。
listen_radiko_TBS.bat
set AYTHKeyGet=AYTHKeyGet.vbs
set MPLAYER_OPT=-cache 128 -ao dsound:device=1
REM TBS|QRR|LFR|NSB|INT|FMT|FMJ
set ch=TBS
for /f %%i in ('CScript %AYTHKeyGet% %ch%') do SET AUTHTOKEN=%%i
mplayer.exe %MPLAYER_OPT% -http-header-fields "X-Radiko-AuthToken: %AUTHTOKEN%" "http://c-radiko.smartstream.ne.jp/%ch%/_definst_/simul-stream.stream/playlist.m3u8"
AYTHKeyGet.vbsはlisten_radiko_TBS.batと同じ場所においてください
-追記-
HLS形式ストリーミング合わせて承認方式も変更したようだ。
大幅に変更したのでまだバグが残ってるかもしれません。
発見次第随時変更します。
AYTHKeyGet.vbs
Option Explicit
Dim argStation,argDebug,argSilent,cookiefile,mail,pass,checkfile,loginfile
'radiko premium
mail = ""
pass = ""
argStation = "TBS" '引数1(i):radiko.jp内の放送局のid
argDebug = "no" '"yes"なら、動作の途中経過を逐次表示する
argSilent = "yes" '"yes"なら、外部コマンドであるwgetの実行プロンプトを表示しない
Dim i
Dim strCmd
Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim aUnnamed
Set aUnnamed = WScript.Arguments.Unnamed
If aUnnamed.Count > 0 Then
For i = 0 To aUnnamed.Count - 1
Select Case i
Case 0
argStation = aUnnamed.Item(i)
End Select
Next
End If
'シェル起動オブジェクトを生成
Dim WSHShell,gShellStyle
Set WSHShell = WScript.CreateObject("WScript.Shell")
If argSilent = "yes" Then
gShellStyle = 7
Else
gShellStyle = 10
End IF
'ファイルシステム用オブジェクトを生成(ラジコ独自認証でファイルを利用)
Dim WSHFS
Set WSHFS = CreateObject("Scripting.FileSystemObject")
strCmd = GetRadikoCmd()
WScript.Quit(0)
'Radiko.jpの独自認証を行う
Function GetRadikoCmd()
Dim pSwfPlayer,pKeyFile,RtnCD,auth1_fms,auth2_fms
Dim pTmpName
pTmpName = WSHFS.GetTempName()
pSwfPlayer = "player_" & argStation & pTmpName & ".swf"
pKeyFile = "authkey_" & argStation & pTmpName & ".png"
auth1_fms = "auth1_fms_" & argStation & pTmpName
auth2_fms = "auth2_fms_" & argStation & pTmpName
loginfile = "login_" & argStation & pTmpName & ".txt"
cookiefile = "cookie_" & argStation & pTmpName & ".txt"
checkfile = "pre_check_" & argStation & pTmpName & ".txt"
'radiko premium
if not (mail = "") then
RtnCD = WSHShell.run("wget.exe -q --save-cookie=" & cookiefile _
& " --keep-session-cookies" _
& " --post-data=""mail=" & mail & "&pass=" & pass & """" _
& " -O " & loginfile _
& " https://radiko.jp/ap/member/login/login",gShellStyle,True)
If RtnCD <> 0 Then
GetRadikoCmd = "error 認証ステップ0:radiko premiumの取得失敗!"
premiumLogoutCmd()
Exit Function
Else
If argDebug = "yes" Then WScript.Echo "認証ステップ0"
End If
WScript.Sleep 2000
if not objFso.FileExists(cookiefile) then
WScript.Echo "failed login1"
premiumLogoutCmd()
Exit Function
End IF
End IF
'認証ステップ1:Radikoのauth1をダウンロード
RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
& " --header ""X-Radiko-App: pc_html5""" _
& " --header ""X-Radiko-App-Version: 0.0.1""" _
& " --header ""X-Radiko-Device: pc""" _
& " --header ""X-Radiko-User: dummy_user""" _
& " --no-check-certificate " _
& " --load-cookies " & cookiefile _
& " --save-headers " _
& " -O " & checkfile _
& " https://radiko.jp/v2/api/auth1",gShellStyle,True)
If RtnCD <> 0 Then
GetRadikoCmd = "error 認証ステップ1:auth1の取得失敗!"
premiumLogoutCmd()
Exit Function
Else
If argDebug = "yes" Then WScript.Echo "認証ステップ1"
End If
'認証ステップ2:RadikoのplayerCommon.jsをダウンロード
RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
& " -O """ & auth1_fms & """" _
& " http://radiko.jp/apps/js/playerCommon.js",gShellStyle,True)
If RtnCD <> 0 Then
GetRadikoCmd = "error 認証ステップ2:playerCommon.jsの取得失敗!"
premiumLogoutCmd()
Exit Function
Else
If argDebug = "yes" Then WScript.Echo "認証ステップ2"
End If
'認証ステップ3:checkfileから、・X-Radiko-AuthToken・X-Radiko-KeyLength・X-Radiko-KeyOffset を読み取る
Dim pFile
Dim pLine
Dim pAuthtoken,pLength,pOffset
Set pFile = WSHFS.OpenTextFile(checkfile,1)
Do While pFile.AtEndOfStream = False
pLine = pFile.ReadLine
If InStr(LCase(pLine),"x-radiko-authtoken:") <> 0 Then
pAuthtoken = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
ElseIf InStr(LCase(pLine),"x-radiko-keylength:") <> 0 Then
pLength = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
ElseIf InStr(LCase(pLine),"x-radiko-keyoffset:") <> 0 Then
pOffset = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
End If
Loop
pFile.Close
'認証ステップ4:playerCommon.jsから文字列取得
Dim radikojsplayer
Set pFile = WSHFS.OpenTextFile(auth1_fms,1)
Do While pFile.AtEndOfStream = False
pLine = pFile.ReadLine
If InStr(LCase(pLine),"new radikojsplayer") <> 0 Then
radikojsplayer = mid(pLine,65,40)
End If
Loop
pFile.Close
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim outputFile
Set outputFile = fso.OpenTextFile(pKeyFile, 2, True)
outputFile.WriteLine radikojsplayer
outputFile.Close
'認証ステップ5:playerCommon.jsの内容から、base64の値であるpartialkeyを求める
Dim pXmldom,pB64,pStream,partialkey
Set pXmldom = CreateObject("Microsoft.XMLDOM")
Set pB64 = pXmldom.CreateElement("work")
pB64.DataType = "bin.base64"
Set pStream = CreateObject("ADODB.Stream")
pStream.Type = 1
pStream.Open
pStream.LoadFromFile pKeyFile
pStream.position = pOffset
pB64.NodeTypedValue = pStream.Read(pLength)
partialkey = pB64.Text
pStream.Close
Set pStream = Nothing
'認証ステップ6:上記で求めたキーからauthtokenとpartialkeyをradikoに送り認証を成立させる
RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
& " --header ""X-Radiko-Device: pc""" _
& " --header ""X-Radiko-User: dummy_user"""_
& " --header=""X-Radiko-Authtoken: " & pAuthtoken & """" _
& " --header=""X-Radiko-Partialkey: " & partialkey & """" _
& " --load-cookies " & cookiefile _
& " https://radiko.jp/v2/api/auth2" _
& " -O """ & auth2_fms & """" ,gShellStyle,True)
If RtnCD <> 0 Then
GetRadikoCmd = "error 認証ステップ6:authtokenとpartialkeyの失敗!"
premiumLogoutCmd()
Exit Function
Else
If argDebug = "yes" Then WScript.Echo "認証ステップ6"
End If
'テンポラリファイルを消す
If argDebug <> "yes" Then
WSHFS.DeleteFile pKeyFile, True
WSHFS.DeleteFile auth1_fms, True
WSHFS.DeleteFile auth2_fms, True
if not (mail = "") then
WSHFS.DeleteFile loginfile, True
WSHFS.DeleteFile checkfile, True
WSHFS.DeleteFile cookiefile, True
End If
End If
WScript.Echo(pAuthtoken)
End Function
Function premiumLogoutCmd()
Dim RtnCD
'radiko premium Logout
if not (mail = "") then
RtnCD = WSHShell.run("wget -q" _
& " --header=""pragma: no-cache""" _
& " --header=""Cache-Control: no-cache""" _
& " --header=""Expires: Thu, 01 Jan 1970 00:00:00 GMT""" _
& " --header=""Accept-Language: ja-jp""" _
& " --header=""Accept-Encoding: gzip, deflate""" _
& " --header=""Accept: application/json, text/javascript, */*; q=0.01""" _
& " --header=""X-Requested-With: XMLHttpRequest""" _
& " --no-check-certificate "_
& " --load-cookies cookiefile "_
& " --save-headers "_
& " -O logoutfile" _
& " https://radiko.jp/ap/member/webapi/member/logout",gShellStyle,True)
If RtnCD <> 0 Then
GetRadikoCmd = "error radiko premiumのLogout失敗!"
Exit Function
Else
If argDebug = "yes" Then WScript.Echo "radiko premiumのLogout取得成功"
End If
if objFso.FileExists(cookiefile) then
WSHFS.DeleteFile cookiefile, True
End IF
End IF
End Function
これらのファイルを用意してlisten_radiko_TBS.batを実行すると仮想オーディオデバイスにTBSラジオが再生される。もちろん地域によって聞けるチャンネルは違うので任意のチャンネルを指定してください。
このスプリクトは試験的なもので要望など、いかなるサポートも行いません。 録音失敗や何らかの、いかなる損害が発生しても、苦情などを一切受け付けません。全て自己責任で利用してください。



1 Comment
さらみ
2018年11月15日参考にさせていただきました。
ついでに他の所にあるエリアフリー化に対応したスクリプトを作成しています。