Skip to content

Instantly share code, notes, and snippets.

@s4kr4
Forked from ybenjo/README.md
Last active June 21, 2019 07:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save s4kr4/fcf7d283b9aa641694bed757b7e1b3cf to your computer and use it in GitHub Desktop.
Save s4kr4/fcf7d283b9aa641694bed757b7e1b3cf to your computer and use it in GitHub Desktop.
save AGQR radio programs.

agqr.rb

これは何

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

fork元 ybenjo / README.md

fork 元との違いは

  • 26:00等の表記に対応

使い方

fork元 参照

# -*- 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/bin/rtmpdump'
ffmpeg = '/usr/bin/ffmpeg'
agqr_stream_url = 'rtmp://fms-base2.mitene.ad.jp/agqr/aandg22'
current = File.dirname(File.expand_path(__FILE__))
save_dir = "#{current}/"
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']]
# appropriate wday
h, m = program['time'].split(':').map(&:to_i)
# check whether the hour is next day
if h >= 24
# check next day's wday
# if today.wday is 6 (Sat), next_wday is 0 (Sun)
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
# check today's wday
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%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: it_kakumei
wday: 火
time: '21:00'
length: 30
- title: free_style
wday: 火
time: '21:30'
length: 30
- title: suzakinishi
wday: 火
time: '25:00'
length: 30
- title: kokoro
wday: 土
time: '0:00'
length: 30
- title: alamode
wday: 日
time: '21:30'
length: 30
- title: seisyun
wday: 土
time: '25:30'
length: 30
- title: igaitai
wday: 火
time: '23:30'
length: 30
- title: dream_theater
wday: 月
time: '21:30'
length: 30
- title: daaaa
wday: 水
time: '25:00'
length: 30
- title: tls
wday: 水
time: '25:30'
length: 30
- title: ageage
wday: 水
time: '19:30'
length: 30
- title: dotai
wday: 土
time: '27: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: '26:00'
length: 30
- title: yumeiro
wday: 土
time: '20:00'
length: 30
- title: adlib
wday: 土
time: '25:00'
length: 30
- title: remake
time: '1:30'
wday: 月
length: 30
- title: uchiasa
time: '21:00'
wday: 月
length: 30
- title: note
wday: 水
time: '26:30'
length: 30
- title: you_know_me
wday: 日
time: '21:00'
length: 30
- title: stay_gold
wday: 水
time: '26: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: '26: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: '26:30'
length: 30
- title: delicate_zone
wday: 月
time: '25:00'
length: 30
- title: ss_ch
wday: 火
time: '25: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: '26:00'
length: 30
- title: queenty_sat
wday: 土
time: '10:00'
length: 60
- title: queenty_sun
wday: 日
time: '10:00'
length: 60
- title: kuroshiro
wday: 土
time: '17:00'
length: 30
- title: toshitai
wday: 土
time: '19:30'
length: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment