Create a gist now

Instantly share code, notes, and snippets.

@ybenjo /README.md forked from yagays/agqr.rb
Last active Apr 2, 2017

What would you like to do?
save AGQR radio programs.

agqr.rb

これは何

AGQR の放送を保存するスクリプト.

fork 元との違いは

yagays / agqr.rb には

  • 31日まである月に翌日の指定が失敗する

というバグが存在する.

使い方

適当なフォルダに agqr.rb を配置して crontab で

29,59 * * * sleep 55; ruby agqr.rb

と指定する.

すると, agqr.rb を配置した一つ上のディレクトリに data ディレクトリを作り,

  • data/flv.flv ファイル
  • data/mp3 にエンコード済み .mp3 ファイル

を生成する.rtmpdump/ffmpeg の path は適当に書き換えてもらいたい.

必要なもの

  • Ruby 1.9.x or 2.x
  • rtmpdump
  • ffmpeg
  • cron

config.yaml について

録音したい番組をここで指定する.そのうち 26:00 のような表記に対応したい.

曜日は自分が読みやすいように漢字で指定している.

注意 title 要素にスペース,アンパサンド,クオート,括弧などを挟むと破滅する.

その他

  • fork 先では config.yamlmovie : true を指定しているが,ココロハルカスのように告知なく動画配信を行うラジオがあるため,自分は採用を見送った.
  • 比較的こまめに見なおしているが,コメントが英語日本語混ざっていて良くない.
# -*- coding: utf-8 -*-
# record AGQR
# usage: use with crontab
# 29,59 * * * sleep 55; ruby agqr.rb
# requirements
# crontab, ruby >~ 1.9, ffmpeg, rtmpdump
require 'yaml'
rtmpdump = '/usr/local/bin/rtmpdump'
ffmpeg = '/usr/local/bin/ffmpeg'
agqr_stream_url = 'rtmp://fms-base1.mitene.ad.jp/agqr/aandg22'
current = File.dirname(File.expand_path(__FILE__))
save_dir = "#{current}/../data/"
Dir.mkdir(save_dir) if !File.exist?(save_dir)
%w(mp3 flv).each do |dir|
path = save_dir + 'dir'
Dir.mkdir(path) if !File.exist?(path)
end
schedule_yaml = "#{current}/schedule.yaml"
if !File.exist?(schedule_yaml)
puts "Config file (#{schedule_yaml}) is not found!"
puts "Please make #{schedule_yaml}."
exit 1
end
today = Time.now
# ruby 1.9.x でも動くために汚いけど書き換える
# WDAY = %w(日 月 火 水 木 金 土).zip((0..6).to_a).to_h
tmp = { }
%w(日 月 火 水 木 金 土).each_with_index do |v, i|
tmp[v] = i
end
WDAY = tmp
schedule = YAML.load_file(schedule_yaml)
schedule.each do |program|
program_wday = WDAY[program['wday']]
is_next_day_program = false
# appropriate wday
h, m = program['time'].split(':').map(&:to_i)
if h.zero? && m.zero?
# check next day's wday
# if today.wday is 6 (Sat), next_wday is 0 (Sun)
next_wday = (today.wday + 1).modulo(7)
is_appropriate_wday = program_wday == next_wday
is_next_day_program = true
else
# check today's wday
is_appropriate_wday = program_wday == today.wday
end
# appropriate time
if is_next_day_program
# 日付を跨ぐので録音開始の日付が1日ずれる
next_day = today + 60 * 60 * 24
# today.day + 1 してたら 31 を超えるとTimeがエラー吐く
program_start = Time.new(next_day.year, next_day.month, next_day.day, h, m, 0)
else
program_start = Time.new(today.year, today.month, today.day, h, m, 0)
end
is_appropriate_time = (program_start - today).abs < 120
length = program['length'] * 60 + 10
if is_appropriate_wday && is_appropriate_time
title = (program['title'].to_s + '_' + today.strftime('%Y%m%d')).gsub(' ','')
flv_path = "#{save_dir}/flv/#{title}.flv"
# record stream
rec_command = "#{rtmpdump} -r #{agqr_stream_url} --live -B #{length} -o #{flv_path} >/dev/null 2>&1"
system rec_command
# encode flv -> m4a
m4a_path = "#{save_dir}/mp3/#{title}.m4a"
m4a_encode_command = "#{ffmpeg} -y -i #{flv_path} -vn -acodec copy #{m4a_path} >/dev/null 2>&1"
system m4a_encode_command
# encode m4a -> mp3
mp3_path = "#{save_dir}/mp3/#{title}.mp3"
mp3_encode_command = "#{ffmpeg} -i #{m4a_path} #{mp3_path} >/dev/null 2>&1"
system mp3_encode_command
# delete m4a
system "rm -rf #{m4a_path}"
end
end
- title: mu_n
wday:
time: '22:00'
length: 60
- title: fgo
wday:
time: '21:00'
length: 30
- title: free_style
wday:
time: '19:30'
length: 30
- title: suzakinishi
wday:
time: '1:00'
length: 30
- title: kokoro
wday:
time: '0:00'
length: 30
- title: alamode
wday:
time: '21:30'
length: 30
- title: high
wday:
time: '1:30'
length: 30
- title: igaitai
wday:
time: '1:30'
length: 30
- title: dream_theater
wday:
time: '21:30'
length: 30
- title: daaaa
wday:
time: '1:00'
length: 30
- title: ageage
wday:
time: '19:30'
length: 30
- title: dotai
wday:
time: '3:00'
length: 30
- title: tryangle
wday:
time: '19:30'
length: 30
- title: hitokana
wday:
time: '23:00'
length: 30
- title: asumin
wday:
time: '23:00'
length: 60
- title: okaeri
wday:
time: '22:00'
length: 60
- title: ayahi
wday:
time: '2:00'
length: 30
- title: adlib
wday:
time: '1:00'
length: 30
- title: uchiasa
time: '21:30'
wday:
length: 30
- title: 375
wday:
time: '0:30'
length: 30
- title: note
wday:
time: '2:30'
length: 30
- title: stay_gold
wday:
time: '2:30'
length: 30
- title: doudemo_ii
wday:
time: '19:30'
length: 30
- title: five_stars_mon
wday:
time: '20:00'
length: 60
- title: five_stars_tue
wday:
time: '20:00'
length: 60
- title: five_stars_wed
wday:
time: '20:00'
length: 60
- title: five_stars_thu
wday:
time: '20:00'
length: 60
- title: five_stars_fri
wday:
time: '20:00'
length: 60
- title: sachika
wday:
time: '2:00'
length: 30
- title: ozanari
wday:
time: '19:00'
length: 30
- title: a_rie
wday:
time: '23:00'
length: 30
- title: agson
wday:
time: '21:00'
length: 120
- title: homerun
wday:
time: '22:00'
length: 60
- title: ohanashi
wday:
time: '20:30'
length: 30
- title: animage
wday:
time: '2:30'
length: 30
- title: delicate_zone
wday:
time: '1:00'
length: 30
- title: ss_ch
wday:
time: '1:30'
length: 30
- title: mikassho
wday:
time: '19:30'
length: 30
- title: pit_inn
wday:
time: '18:30'
length: 30
- title: himitsu_kichi
wday:
time: '2:00'
length: 30
- title: queenty
wday: 月曜
time: '17:30'
length: 30
- title: kuroshiro
wday:
time: '17:00'
length: 30
- title: toshitai
wday:
time: '23:30'
length: 30
- title: torima
wday:
time: '19:30'
length: 30
- title: it_kakumei
wday:
time: '21:00'
length: 30
- title: ayaradi
wday:
time: '19:00'
length: 30
- title: light
wday:
time: '17:30'
length: 30
- title: chika
wday:
time: '21:00'
length: 30

southrop commented Apr 9, 2016

突然失礼致します。 __dir__ は Ruby 2.0.0 からのバリアブルなので、Ruby 1.9.x は使えないと思います。

Owner

ybenjo commented Apr 27, 2016

ご指摘ありがとうございます. File.dirname(File.expand_path(__FILE__)) でいかがでしょうか?

goriggg commented Mar 8, 2017

ruby実行するとerrがでますね。
/root/radio/agqr.rb:65:in initialize': wrong number of arguments (6 for 0) (ArgumentError) from /root/radio/agqr.rb:65:innew'
from /root/radio/agqr.rb:65
from /root/radio/agqr.rb:39:in `each'
from /root/radio/agqr.rb:39

Owner

ybenjo commented Apr 2, 2017

program_start = Time.new(next_day.year, next_day.month, next_day.day, h, m, 0) でエラーが出ているということで確認します.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment