Created
January 4, 2017 05:03
-
-
Save akahana-1/b728df1543d6537b139754116ed19671 to your computer and use it in GitHub Desktop.
y_benjo氏作成のスクリプトを改良したもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# record AGQR | |
# usage: use with crontab | |
# 29,59 * * * sleep 55; ruby agqr.rb | |
# requirements | |
# crontab, ruby => 2.0, ffmpeg, rtmpdump, mimms | |
require 'yaml' | |
rtmpdump = '/usr/bin/rtmpdump' | |
ffmpeg = '/usr/bin/ffmpeg' | |
mimms = '/usr/bin/mimms' | |
agqr_stream_url = 'rtmp://fms-base1.mitene.ad.jp/agqr/aandg22' | |
simul_url = 'mms://hdv.nkansai.tv/sakudaira' | |
current = File.dirname(File.expand_path(__FILE__)) | |
save_dir = "#{current}/../data" | |
Dir.mkdir(save_dir) if !File.exist?(save_dir) | |
flv_dir = "#{save_dir}/flv" | |
Dir.mkdir(flv_dir) if !File.exist?(flv_dir) | |
mp3_dir = "#{save_dir}/mp3" | |
Dir.mkdir(mp3_dir) if !File.exist?(mp3_dir) | |
schedule = "#{current}/schedule.yaml" | |
if !File.exist?(schedule) | |
puts "Config file (#{schedule}) is not found!" | |
puts "Please make #{schedule}." | |
exit 1 | |
end | |
today = Time.now | |
WDAY = %w(日 月 火 水 木 金 土).zip((0..6).to_a).to_h | |
puts %w(日 月 火 水 木 金 土).zip((0..6).to_a) | |
schedule_yaml = YAML.load_file(schedule) | |
schedule_yaml.each do |program| | |
puts 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 | |
non_video = !program['video'] | |
if is_appropriate_wday && is_appropriate_time | |
title = (program['title'].to_s + '_' + today.strftime('%Y%m%d')).gsub(' ','') | |
flv_path = "#{flv_dir}/#{title}.flv" | |
# record stream | |
if program['channel'] == 'simul' | |
wma_path = "#{mp3_dir}/#{title}.wma" | |
rec_command = "#{mimms} -t #{program['length']} #{simul_url} #{wma_path}" | |
else | |
rec_command = "#{rtmpdump} -r #{agqr_stream_url} --live -B #{length} -o #{flv_path}" | |
end | |
system rec_command | |
if non_video | |
# encode flv -> m4a | |
m4a_path = "#{mp3_dir}/#{title}.m4a" | |
m4a_encode_command = "#{ffmpeg} -y -i #{flv_path} -vn -acodec copy #{m4a_path}" | |
if program['channel'] != 'simul' | |
system m4a_encode_command | |
end | |
# encode m4a -> mp3 | |
mp3_path = "#{mp3_dir}/#{title}.mp3" | |
if program['channel'] == 'simul' | |
mp3_encode_command = "#{ffmpeg} -i #{wma_path} #{mp3_path}" | |
else | |
mp3_encode_command = "#{ffmpeg} -i #{m4a_path} #{mp3_path}" | |
end | |
system mp3_encode_command | |
# delete m4a | |
system "rm -rf #{m4a_path}" | |
system "rm -rf #{wma_path}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment