Skip to content

Instantly share code, notes, and snippets.

@g9o7t1s3
Forked from ybenjo/README.md
Last active December 27, 2017 07:07
Show Gist options
  • Select an option

  • Save g9o7t1s3/23d0344324a66a9502f567fd3e8d1c8a to your computer and use it in GitHub Desktop.

Select an option

Save g9o7t1s3/23d0344324a66a9502f567fd3e8d1c8a to your computer and use it in GitHub Desktop.
save AGQR radio programs.

agqr.rb

これは何

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

fork 元との違いは

  • 24時以降の表記に対応
  • 帯番組をひとつのエントリーで書ける

使い方

基本的な使い方はFork元と同じです

必要なもの

  • Ruby
  • rtmpdump
  • ffmpeg
  • cron

schedule.yaml について

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

  • 曜日を複数指定するところで-の前をタブにすると動かないです

その他

  • Ruby超初心者なのでコードがだいぶ汚いです
  • 自分はNASに保存しているのでschedule.yamlにpathtoの項目を追加しています

ちなみに自分の使っている環境は

  • OS : Ubuntu Server 16.04
  • Ruby : v2.3.1
  • rtmpdump: v2.4
  • ffmpeg : v2.8.11
require 'yaml'
require 'date'
##################
# 各パス
##################
$current = File.dirname(File.expand_path(__FILE__))
$schedule_path = "#{$current}/schedule.yaml"
$ffmpeg = '/path/to/ffmpeg'
$agqr_stream_url = 'rtmp://fms-base2.mitene.ad.jp/agqr/aandg22'
$mp4_path = "/path/to/agqr"
$rtmpdump = '/path/to/rtmpdump'
def record_judge(program_title, program_time, program_length, program_wday, program_pathto)
today = Time.now
h, m = program_time.split(':').map(&:to_i)
if h >= 24
if h == 24 && m.zero?
next_wday = (today.wday + 1).modulo(7)
next_day = today + 24 * 60 * 60
is_appropriate_wday = program_wday == next_wday
else
next_wday = today.wday
next_day = today
is_appropriate_wday = (program_wday + 1).modulo(7) == today.wday
end
h -= 24
is_next_day_program = true
else
is_next_day_program = false
if program_wday == today.wday
is_appropriate_wday = true
else
is_appropriate_wday = false
end
end
if is_next_day_program
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') + '_' + today.strftime('%m') + '_' + today.strftime('%d')).gsub(' ','')
flv_path = "#{$current}/flv/#{title}.flv"
rec_command = "#{$rtmpdump} -r #{$agqr_stream_url} --live -B #{length} -o #{flv_path} >/dev/null 2>&1"
system rec_command
# encode flv -> mp4
mp4_title = (program_title.to_s + '_' + today.strftime('%Y') + today.strftime('%m') + today.strftime('%d')).gsub(' ','')
mp4_encode_command = "#{$ffmpeg} -y -loglevel verbose -i #{flv_path} -vcodec copy -acodec copy #{$mp4_path}/#{program_pathto}/#{mp4_title}.mp4 > /mnt/recradio/Ubuntu/log/#{mp4_title}.log"
system mp4_encode_command
# log output
d = DateTime.now
y = (DateTime.now - 1)
if h >= 0 && h <= 5 then
s_time_h = h + "24".to_i
elsif h >= 6 && h <= 9 then
s_time_h = "0".to_s << h.to_s
else
s_time_h = h
end
if m == 0 then
s_time_m = "00"
elsif m >= 0 && m <= 9 then
s_time_m = "0".to_s << m.to_s
else
s_time_m = m
end
if d.strftime("%H").to_i <= "5".to_i then
filename = "#{$current}/log/" + y.strftime("%Y%m%d") + ".log"
else
filename = "#{$current}/log/" + d.strftime("%Y%m%d") + ".log"
end
text = s_time_h.to_s << s_time_m.to_s << "\t" << program_length.to_s << "\t" << program_title
File.open(filename, "a") do |f|
f.puts(text)
end
end
end
if !File.exist?($schedule_path)
puts "Schedule File Not Found!"
exit 1
end
today = Time.now
tmp = { }
%w(日 月 火 水 木 金 土).each_with_index do |v, i|
tmp[v] = i
end
WDAY = tmp
schedule = YAML.load_file($schedule_path)
schedule.each do |program|
program_pathto = program['pathto']
program_title = program['title']
program_length = program['length']
program_time = program['time']
if program['wday'].instance_of?(Array) == true
counts = program['wday'].count
for num in 0..counts - 1 do
program_wday = WDAY[program['wday'][num]]
record_judge(program_title, program_time, program_length, program_wday, program_pathto)
end
else
program_wday = WDAY[program['wday']]
record_judge(program_title, program_time, program_length, program_wday, program_pathto)
end
end
# 複数日
- title : yonayona
wday :
- 月
- 火
- 水
- 木
time : '24:00'
length: 60
pathto: yonayona
# 月曜日
- title : moeshi
wday : 月
time : '17:30'
length: 30
pathto: moeshi
# 火曜日
- title : rietion
wday : 火
time : '23:00'
length: 30
pathto: rietion
- title : toshitai
wday : 火
time : '23:30'
length: 30
pathto: toshitai
- title : kuritakun
wday : 火
time : '27:00'
length: 30
pathto: kuritakun
# 水曜日
- title : qrmp
wday : 水
time : '22:00'
length: 60
pathto: qrmp
# 木曜日
- title : jannne
wday : 木
time : '26:00'
length: 30
pathto: jannne
# 金曜日
- title : TheCatch_Fri
wday : 金
time : '18:00'
length: 60
pathto: 'TheCatch/金'
# 土曜日
- title : agson
wday : 土
time : '21:00'
length: 120
pathto: agson
# 日曜日
- title : aokikuroki
wday : 日
time : '20:30'
length: 30
pathto: aokikuroki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment