Created
March 7, 2014 23:20
-
-
Save viz3/9422218 to your computer and use it in GitHub Desktop.
split ts and encode.
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 'optparse' | |
require 'fileutils' | |
class Runner | |
def initialize(argv) | |
@recorded_dir = '/chinachu/recorded' | |
@encoded_dir = ENV['HOME'] + '/encoded' | |
@work_dir = ENV['HOME'] + '/tmp' | |
@work_name = 'v' | |
@m2ts_suffix = '.m2ts' | |
@m4v_suffix = '.m4v' | |
@dryrun = true | |
opt = OptionParser.new | |
opt.on('-x') { |v| @dryrun = false } | |
opt.parse!(argv) | |
end | |
def run | |
abort('pid_file exists.') unless lock | |
Dir.glob("#{@recorded_dir}/*#{@m2ts_suffix}").sort!.each do |f| | |
next unless finished?(f) | |
name = f.gsub(/#{@recorded_dir}\//, '').gsub(/#{@m2ts_suffix}/, '') | |
next if File.exists?(efname(name)) | |
File.symlink(f, ifname) | |
Encoder.new(ifname, ofname, @dryrun).run | |
File.rename(ofname, efname(name)) | |
File.delete(ifname) | |
end | |
unlock | |
end | |
private | |
def lock | |
return false if File.exists?(pidfile) | |
File.open(pidfile, 'w') { |f| f.write($$) } | |
end | |
def unlock | |
File.delete(pidfile) | |
end | |
def pidfile | |
"#{@work_dir}/#{@work_name}.pid" | |
end | |
def efname(name) | |
"#{@encoded_dir}/#{name}#{@m4v_suffix}" | |
end | |
def ifname | |
"#{@work_dir}/#{@work_name}#{@m2ts_suffix}" | |
end | |
def ofname | |
"#{@work_dir}/#{@work_name}#{@m4v_suffix}" | |
end | |
def finished?(f) | |
x = File.stat(f) | |
sleep 1 | |
y = File.stat(f) | |
x == y | |
end | |
end | |
class Encoder | |
def initialize(ifname, ofname, dryrun=true) | |
@ifname = ifname | |
@ofname = ofname | |
@dryrun = dryrun | |
@wine_bin = '/usr/local/bin/wine' | |
@ts_splitter_bin = '/usr/local/bin/TsSplitter.exe' | |
@ts_splitter_opt = '-EIT -ECM -EMM -SD -1SEG -WAIT2' | |
@ffmpeg_bin = '/usr/local/bin/ffmpeg' | |
@ffmpeg_opt = '-acodec copy -absf aac_adtstoasc -threads 0 -deinterlace -s hd720 -aspect 16:9 -r 29.97 -vcodec libx264 -g 300 -b 1600000 -f mp4' | |
end | |
def run | |
split | |
encode | |
File.delete(sfname) | |
end | |
private | |
def sfname | |
@ifname =~ /^(.*)\.([^\.]+)$/ | |
"#{$1}_HD.#{$2}" | |
end | |
def split | |
cmd = "#{@wine_bin} #{@ts_splitter_bin} #{@ts_splitter_opt} #{@ifname}" | |
if @dryrun | |
puts cmd | |
FileUtils.touch(sfname) | |
elsif | |
`#{cmd}` | |
end | |
end | |
def encode | |
cmd = "#{@ffmpeg_bin} -i #{sfname} #{@ffmpeg_opt} #{@ofname}" | |
if @dryrun | |
puts cmd | |
FileUtils.touch(@ofname) | |
elsif | |
`#{cmd}` | |
end | |
end | |
end | |
Runner.new(ARGV).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment