ffmpegでNHK語学講座を録音した
その1. ストリームURLと録音時間を指定してcrontabに登録したその2. 聞き逃し番組をダウンロードした
ストリームURLと録音時間を指定してcrontabに登録した
(経緯)radi.shで録音していたが、NHKラジオのストリーミングのURLが変更されたため、ラジオ第2をffmpegで直接録音することにした。
2025年9月25日までのURL
https://www.nhk.or.jp/radio/config/config_v5.8.0_radiru_and.xml
R2 https://radio-stream.nhk.jp/hls/live/2023501/nhkradiruakr2/master.m3u8
2025年9月26日からのURL
https://www.nhk.or.jp/radio/config/config_web.xml
R2 https://simul.drdi.st.nhk/live/4/joined/master.m3u8
# 実行ファイル
nhkradio.sh(NHKラジオ第2専用)
# 引数 $1は録音時間(遅延で最後が切れるので、1分長く)
00:16:00なら16分
# 引数 $2は出力先
/home/hoge/以下
/usr/local/bin/nhkradio.sh
#!/bin/bash
# $1 00:00:00
# $2 output
/usr/bin/ffmpeg -fflags +discardcorrupt -i https://simul.drdi.st.nhk/live/4/joined/master.m3u8 -acodec copy -vn -bsf:a aac_adtstoasc -t $1 $2 >&/dev/null
(参考)#!/bin/bash
# $1 00:00:00
# $2 output
/usr/bin/ffmpeg -fflags +discardcorrupt -i https://simul.drdi.st.nhk/live/4/joined/master.m3u8 -acodec copy -vn -bsf:a aac_adtstoasc -t $1 $2 >&/dev/null
これだと、ライブストリームが途切れたのか短い録音になってた。
/usr/bin/ffmpeg -fflags +discardcorrupt -i https://simul.drdi.st.nhk/live/4/joined/master.m3u8 -c copy -bsf:a aac_adtstoasc -http_persistent 0 -t $1 $2 >&/dev/null
crontab -l
# ラジオ英会話 45 6 * * 1-5 /usr/local/bin/nhkradio.sh 00:16:00 /home/hoge/nhk_kaiwa_$(date +\%Y\%m\%d-\%a).m4a # ビジネス英語 15 9 * * 1-5 /usr/local/bin/nhkradio.sh 00:16:00 /home/hoge/nhk_business_$(date +\%Y\%m\%d-\%a).m4a # 英会話タイムトライアル 30 8 * * 1-5 /usr/local/bin/nhkradio.sh 00:11:00 /home/hoge/nhk_timetrial_$(date +\%Y\%m\%d-\%a).m4a # エンジョイシンプルイングリッシュ 0 6 * * 1-5 /usr/local/bin/nhkradio.sh 00:06:00 /home/hoge/nhk_enjoy_$(date +\%Y\%m\%d-\%a).m4a # ニュースで学ぶ現代英語 30 09 * * 1-5 /usr/local/bin/nhkradio.sh 00:16:00 /home/hoge/nhk_news_$(date +\%Y\%m\%d-\%a).m4a
参考
放送時間の変更はなかった。(2025年9月と10月で)
参考
URL変更で繋がらなくなったときのradi.shのエラーメッセージ
ffmpegのソースになるストリームが存在しなくなった。
Output file #0 does not contain any stream
Record failed
Record failed
聞き逃し番組をダウンロードした
(経緯)2025年9月26日~9月30日の録音予約が失敗していたため、聞き逃し番組をダウンロードした。公開元 https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/corners/new_arrivals
ここから聞き逃し番組の「series_site_id」を探す。
ラジオ英会話 PMMJ59J6N2
ビジネス英語 368315KKP8
英会話タイムトライアル 8Z6XJ6J415
エンジョイシンプルイングリッシュ BR8Z3NX7XM
聞き逃した番組の公開中の「stream_url」(m3u8ファイル)を表示する。
例「series_site_id: "PMMJ59J6N2"」を「series?site_id=PMMJ59J6N2」に指定する。
https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=PMMJ59J6N2&corner_site_id=01
https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=PMMJ59J6N2&corner_site_id=01
0 id 4272566 program_title "ラジオ英会話 ハートでつかめ!英語の極意 (118)" onair_date "9月24日(水)午前6:45放送" closed_at "2025年10月1日(水)午前7:00配信終了" stream_url "https://vod-stream.nhk.jp/radioondemand/r/PMMJ59J6N2/s/stream_PMMJ59J6N2_05b8d6bf79521304a55dd26307accd14/index.m3u8" aa_contents_id "[radio]vod;ラジオ英会話 ハートでつかめ!英語の極意 (118);r2,130;2025092473898;2025-09-24T06:45:00+09:00_2025-09-24T07:00:03+09:00" annotation_title "" annotation_url "" program_sub_title "【講師】東洋学園大学教授…大西泰斗,【出演】デイビット・エバンス,秋乃ろーざ" 1 id 4272771 program_title "ラジオ英会話 ハートでつかめ!英語の極意 (119)" onair_date "9月25日(木)午前6:45放送" closed_at "2025年10月2日(木)午前7:00配信終了" stream_url "https://vod-stream.nhk.jp/radioondemand/r/PMMJ59J6N2/s/stream_PMMJ59J6N2_42bd140d835338daffffb246a84d8dfd/index.m3u8" aa_contents_id "[radio]vod;ラジオ英会話 ハートでつかめ!英語の極意 (119);r2,130;2025092574145;2025-09-25T06:45:00+09:00_2025-09-25T07:00:03+09:00" annotation_title "" annotation_url "" program_sub_title "【講師】東洋学園大学教授…大西泰斗,【出演】デイビット・エバンス,秋乃ろーざ" 2 id 4272949 program_title "ラジオ英会話 ハートでつかめ!英語の極意 (120)" onair_date "9月26日(金)午前6:45放送" closed_at "2025年10月3日(金)午前7:00配信終了" stream_url "https://vod-stream.nhk.jp/radioondemand/r/PMMJ59J6N2/s/stream_PMMJ59J6N2_0503526630c5601b90107b5b29eb17d8/index.m3u8" aa_contents_id "[radio]vod;ラジオ英会話 ハートでつかめ!英語の極意 (120);r2,130;2025092674392;2025-09-26T06:45:00+09:00_2025-09-26T07:00:03+09:00" annotation_title "" annotation_url "" program_sub_title "【講師】東洋学園大学教授…大西泰斗,【出演】デイビット・エバンス,秋乃ろーざ" 3 ...以下略
ffmpegでダウンロードする。(9月26日放送分のラジオ英会話)
ffmpeg -http_seekable 0 -i https://vod-stream.nhk.jp/radioondemand/r/PMMJ59J6N2/s/stream_PMMJ59J6N2_0503526630c5601b90107b5b29eb17d8/index.m3u8 nhk_kaiwa_20250926.mp3
聞き逃し番組のjsonからmp3のタグを取得してffmpegで録音した
ffmpegのコマンドラインをphpのページに表示させて、コンソールから実行した。json_decodeで抽出した番組名(share_text_title)、タイトル(program_title)、放送日(onair_date)を自分好みに編集して、mp3のタグのアルバム名とタイトル、ファイル名に指定してffmpegでダウンロードする。
nhk_kaiwa.php
サンプル
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<?php
$pref = "nhk_kaiwa_2025";
$url = "https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=PMMJ59J6N2&corner_site_id=01";
$json_string = file_get_contents($url);
$arr = json_decode($json_string, true);
$episodes = $arr["episodes"];
echo $arr["share_text_title"]."<br>\n";
$i = 0;
while ($i < count($episodes)) {
$title = $episodes[$i]["program_title"]." ".preg_replace("/午.*/", "", $episodes[$i]["onair_date"]);
$filename = $pref."_".preg_replace("/午.*/", "", $episodes[$i]["onair_date"]).".mp3";
$filename = preg_replace("/\(/", "\\(", $filename);
$filename = preg_replace("/\)/", "\\)", $filename);
echo "ffmpeg -http_seekable 0 -i ".$episodes[$i]["stream_url"]." -metadata title='".$title."' -metadata album='".rtrim($arr["share_text_title"])."' ".$filename."<br>\n";
$i++;
}
?>
nhk_kaiwa.php