require 'rubygems'
require 'hpricot'
class Range
def length
to_a.size
end
end
class Fixnum
def pad(n=8)
x = to_s
pad = n - x.length
x.insert(0,"#{'0'*pad}")
x
end
end
class Editor
attr_reader :edl
def initialize(file)
@masters = {}
@edits = []
@edl = []
@doc = Hpricot(open(file))
find_masters()
breakdown()
end
def find_masters
@doc.search("//file/pathurl").each do |elem|
base = File.basename(elem.inner_html).gsub!(/\....$/,'')
path = abspath(elem.inner_html)
folders = `ls
if folders.index(base).nil?
@masters[elem.parent.attributes['id']] = path + "/" + folders.first + "/" + base + "."
else
@masters[elem.parent.attributes['id']] = path + "/" + base + "/"
end
end
end
def breakdown
@edits = @doc.search("//clipitem/file").each do |elem|
cut = []
file_id = elem.attributes['id']
seq_location = @masters[file_id]
places = getpad(seq_location)
cutin = (elem.parent/"in").inner_html.to_i.pad(places)
cutout = (elem.parent/"out").inner_html.to_i.pad(places)
cut.push(Range.new(cutin.succ!,cutout))
cut.push(seq_location)
@edl.push(cut)
end
end
def getpad(path)
if path =~ /\.$/
f = `ls
else
f = `ls
end
return f.match(/(.+\.)?(.+)\..../).to_a.last.length
end
def abspath(path)
path.gsub!(/file:\/\/localhost/,'')
path.gsub!(/%20/,'\ ')
return File.dirname(path)
end
def print_edl
@edl.each_with_index do |cut,i|
puts i.pad(4).succ + "|" + cut[0].first + " -- " + cut[0].last + " ++ " + cut[1]
end
end
end
class Manager
def initialize
@subroll = '00'
@maxslots = 0
@current_directory = ""
@current_frame = "00000001"
refresh_current
end
def refresh_current
@subroll.succ!
@maxslots = FRAMES_PER_ROLL
@current_directory = "#{TARGET}/#{BIGROLL}_#{@subroll}"
`mkdir -p
puts "NEW ROLL! #{@current_directory}"
sleep 5
end
def assemble(cut)
seq, source = cut[0], cut[1]
if seq.length+1 > @maxslots
refresh_current()
end
puts "Copying files to temporary folder..."
seq.each do |frame|
if source =~ /\/$/
`cp
else
`cp
end
end
Dir.chdir(TEMP)
list = Dir.entries(TEMP).delete_if { |x| x =~ /^\./ }
list.map! { |f| File.rename(f,f + "x") ; f + "x" }
puts "Moving files into roll #{@subroll}"
list.each do |f|
`mv -i
Dir.chdir(@current_directory)
`mv -i
puts "Moved #{f}"
Dir.chdir(TEMP)
@current_frame.succ!
@maxslots -= 1
end
end
end
FRAMES_PER_ROLL = 80
BIGROLL = '03'
TEMP = "/Volumes/Offline/Cache/Rubycut"
TARGET = "/Volumes/Offline/REEL3RC2"
FCP_XML = "/Users/anak/Desktop/edl.xml"
editor = Editor.new(FCP_XML)
editor.print_edl
puts "Press ENTER to continue..."
gets
manager = Manager.new
editor.edl.each_with_index do |cut,i|
puts "Assembling #{(i+1).pad(4)} | #{cut[0]} > #{cut[1]}"
manager.assemble(cut)
end
puts "CONFORMED: #{FCP_XML}"