|
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 |