Created
February 14, 2016 20:36
-
-
Save holysugar/63df737eb3dad7f17d50 to your computer and use it in GitHub Desktop.
nicolive ts save
This file contains 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
#!/usr/bin/env ruby | |
require 'shellwords' | |
require 'niconico' | |
token = ENV['NICO_TOKEN'] | |
id = ARGV[0] | |
# monkey patch | |
class Niconico::Live::RtmpdumpFailed < StandardError; end | |
class Nicolive | |
attr_reader :basename, :filenames, :results | |
def initialize(token) | |
@agent = Niconico.new(token) | |
@agent.login | |
end | |
def save(id) | |
l = @agent.live(id) | |
@basename = _filename(l) | |
@results = l.execute_rtmpdump('tmp', true) | |
@filenames = results.map{|x| x[0][3] } | |
results.all?{|x| x[1].success? } | |
end | |
private | |
def _filename(live) | |
date = live.opens_at.strftime('%Y_%m_%d') | |
"#{date}_#{live.title}" | |
end | |
end | |
class FlvFilter | |
def perform(flies) | |
end | |
end | |
class Mp4Converter | |
attr_reader :results, :encoded | |
def initialize(files, base) | |
@files = Array(files) | |
@base = base | |
@encoded = [] | |
end | |
def perform(largest_only = true) | |
if largest_only | |
@results = [ convert(largest_file, "#{@base}.mp4") ] | |
else | |
@results = @files.map do |src| | |
convert(src) | |
end | |
end | |
end | |
private | |
def largest_file | |
@files.max_by{|f| File.size(f) } | |
end | |
def dst_name(src) | |
src.sub('tmp', @base).sub('.flv', '.mp4') | |
end | |
def ffmpeg | |
exit_status, output = shell_exec('hash ffmpeg >/dev/null 2>&1') | |
command = exit_status == 0 ? 'ffmpeg' : 'avconv' | |
end | |
def convert(src, dst = dst_name(src)) | |
result = shell_exec(command(src, dst)) | |
@encoded << dst if result[0].success? | |
result | |
end | |
def shell_exec(command) | |
output = `#{command}` | |
exit_status = $? | |
[exit_status, command, output] | |
end | |
def command(src, dst) | |
command = "#{ffmpeg} -loglevel error -y -i #{Shellwords.escape(src)} -acodec copy -vcodec copy #{Shellwords.escape(dst)} 2>&1" | |
end | |
end | |
# XXX writing | |
nl = Nicolive.new(token) | |
nl.save(id) | |
p nl.filenames | |
p nl.results | |
cv = Mp4Converter.new(nl.filenames, nl.basename) | |
cv.perform | |
p cv.results | |
p cv.encoded | |
# TODO: upload to google drive | |
# TODO: notify in slack | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment