Skip to content

Instantly share code, notes, and snippets.

@x86ed
Created March 30, 2012 06:06
Show Gist options
  • Save x86ed/2247251 to your computer and use it in GitHub Desktop.
Save x86ed/2247251 to your computer and use it in GitHub Desktop.
Dynamic video renderer
#!/usr/bin/ruby -w
require 'open-uri'
require 'rubygems'
require 'nokogiri'
require 'RMagick'
require 'fileutils'
require 'net/http'
require 'tagChecker'
include Magick
class LogData
def initialize(logPath)
@filepath = logPath
@contents = File.new(@filepath, "w+")
end
def setPath(name)
@filepath = name
@contents.close
@contents = File.new(@filepath, "w+")
end
def appendText(text)
puts text
@contents.seek(File.size(@filepath) + 1)
@contents.puts(text +"\n")
end
def clearOut
@contents.close
File.delete(@filepath)
@contents = File.new(@filepath, "w+")
end
def dumpOut
@contents.rewind
@contents.each{ |line| puts line}
end
def archiveAs(archivename)
@archive = File.new(archivename, "w")
@contents.rewind
@contents.each{ |line| @archive.puts(line.to_s)}
@archive.close
end
def closeOut
@contents.close
end
end
class XMLSortNClean
attr_reader :url, :sortMethod, :xmlData, :output
def initialize( url, sortMethod, log="renderer", method="get")
if method == "post"
@urlChunks = url.split("?")
@url = Net::HTTP.post_form(URI(@urlChunks[0]) , 'q' => @urlChunks[1]||"" ).body
else
@url = open(url)
end
@sortMethod = sortMethod
@xmlData = Nokogiri::XML(@url)
@log = "/home/robot/scripts/logs/" + log + ".log"
end
def updateQuery
@xmlData = Nokogiri::XML(open(@url))
end
def gimmieMyData
extend @sortMethod
self.sortWithThis(@xmlData)
end
def renderOutput
extend @sortMethod
self.renderOutput
end
end
module MakeTheBracket
def sortWithThis(shitToSort)
@thelog = LogData.new(@log)
@thelog.appendText("XML start:" + Time.now.to_s)
@positions = [] # array of values
@video = shitToSort.xpath("//xmlns:video").map{ |node| node.to_s}
def self.getVideo #what the shit?!
@video
end
@teams = shitToSort.xpath("//xmlns:Team1|//xmlns:Team2").map{ |node| [node.text]}
@positions = @positions.each_with_index.map{ |x,i| x|@teams[i]} #ok that was pretty rad, thanks ruby
@thelog.appendText("XML end:" + Time.now.to_s)
@thelog.closeOut
end
def renderOutput(contentArray)
@contentArray = contentArray
@thelog = LogData.new(@log)
@thelog.appendText("render start:" + Time.now.to_s)
@bracketImage = ImageList.new("/home/robot/assets/brackets_template.png")
@bracketDraw = Draw.new
@contentArray.map{ |x| @bracketDraw = @bracketDraw.annotate(@bracketImage, x[0].to_f, x[1].to_f, x[2].to_f, x[3].to_f, x[4].to_s){
self.font_family = 'Memphis'
self.fill = 'white'
self.stroke = 'transparent'
self.pointsize = 12
self.font_weight = 300
self.gravity = CenterGravity
}}
@bracketImage.write("/home/robot/output/completedBracket.png")
@thelog.appendText("render end:" + Time.now.to_s)
@thelog.closeOut
end
end
module MakeTrackingImages # this module sorts the traking XML from premiere
def makeImages(item,index)
@imageStart = Time.now
@paddedIndex = "%05d" % index
if !@debugChunk
@debugChunk = "whatever"
end
if @debugChunk == ("|rotation:" + (@rotations[index].to_f).to_s + "|Scale:" + (@scales[index][0].to_f/100).to_s + "|Position:" + @positions[index][0].to_s + "x" + @positions[index][0].to_s)
@paddedSibling = "%05d" % (index-1)
FileUtils.cp "/home/robot/output/sequence/sequence_#{@paddedSibling}.png", "/home/robot/output/sequence/sequence_#{@paddedIndex}.png"
else
@thisImage = ImageList.new("/home/robot/output/completedBracket.png")
@backdrop = ImageList.new("/home/robot/assets/backdrop.png")
@thisImage.colorspace=Magick::RGBColorspace
@thisImage.resize!(@scales[index][0].to_f/100)
#@thisImage.rotate!(@rotations[index].to_i)
@backdrop.composite!(@thisImage, CenterGravity,@positions[index][0].to_f - (@dimensions[0].to_f/2) ,@positions[index][1].to_f - (@dimensions[1].to_f/2),OverCompositeOp)
@backdrop.write("/home/robot/output/sequence/sequence_#{@paddedIndex}.png")
GC.start #ugly hack to free rmagick memory
@imageEnd = Time.now
@debugChunk = "|rotation:" + (@rotations[index].to_f).to_s + "|Scale:" + (@scales[index][0].to_f/100).to_s + "|Position:" + @positions[index][0].to_s + "x" + @positions[index][0].to_s
@thelog.appendText(@paddedIndex + @debugChunk + "|Start:" + @imageStart.to_s + "|End:" + @imageEnd.to_s + "|Time:" + (@imageEnd.to_i - @imageStart.to_i).to_s)
end
end
def sortWithThis(shitToSort)
@thelog = LogData.new(@log)
@thelog.appendText("XML start:" + Time.now.to_s)
# picture dimensions
@dimensions = [ shitToSort.xpath("//movie/composition/@width").to_s, shitToSort.xpath("//movie/composition/@height").to_s , shitToSort.xpath("//movie/composition/@bgcolor").to_s]
# anchor points of object
@anchor = shitToSort.xpath("//property[@type='Anchor_Point']/key/@value").map{ |node| node.to_s.split(",")}
puts "@anchor = " + @anchor.length.to_s
# X, Y, Z coordinates of object
@positions = shitToSort.xpath("//property[@type='Position']/key/@value").map{ |node| node.to_s.split(",")}
puts "@positions = " + @positions.length.to_s
# scale of object
@scales = shitToSort.xpath("//property[@type='Scale']/key/@value").map{ |node| node.to_s.split(",")}
puts "@scales = " + @scales.length.to_s
#angle of object
@rotations = shitToSort.xpath("//property[@type='Rotation']/key/@value").to_s
puts "@rotations = " + @rotations.length.to_s
#opacity of object
@opacities = shitToSort.xpath("//property[@type='Opacity']/key/@value").map{ |node| node.to_s.split(",")}
puts "@opacities = " + @opacities.length.to_s
@thelog.appendText("XML end:" + Time.now.to_s)
system("rm -rf /home/robot/output/sequence/*.png")
@positions.each_with_index.map{ |x,i| makeImages(x,i) }
@thelog.closeOut
end
def renderOutput(filename, randomseed,teamclips )
@shotlist = [[21,43,21],[1,10,14],[5,44,12]]
@thelog = LogData.new(@log)
@thelog.appendText("render start:" + Time.now.to_s)
# want to make sure the next calls get done sequentially, so using system calls
system("/home/robot/scripts/ffmpegMov.sh '" + @shotlist.to_s + "'")
@thelog.appendText("render end:" + Time.now.to_s)
@thelog.closeOut
end
end
module TeamModule # module for updating team list & team content
def sortWithThis(shitToSort)
puts shitToSort
@teamId = shitToSort.xpath("//xmlns:TeamId").map{ |node| [node.text]}
puts @teamId
@teamArray = shitToSort.xpath("//xmlns:Name").each_with_index.map{ |x,i| [@teamId[i] , x.text]}
def self.getTeams
@teamArray
end
end
def createTeamVideo(theKingAndI)
@teamId = theKingAndI[0]
@teamname = theKingAndI[1]
system("/home/robot/scripts/maintenence/makeTeams.py #{@teamId} \"#{@teamname}\" ")
end
def oldTeam
"crap"
end
def renderOutput
@currentTeam = self.getTeams
@oldTeam = self.oldTeam
if @currentTeam != @oldTeam
@thelog = LogData.new(@log)
@thelog.appendText("making new videos")
@thelog.appendText("render start:" + Time.now.to_s)
@currentTeam.map{ |x| self.createTeamVideo(x) }
@thelog.appendText("render end:" + Time.now.to_s)
@thelog.closeOut
end
def self.oldTeam
@currentTeam
end
self.oldTeam
end
end
module TestModule # test module for use with XMLSortNClean
def sortWithThis(shitToSort)
puts shitToSort
@team1 = shitToSort.xpath("//xmlns:Team1|//xmlns:Team2").map{ |node| [node.text]}
puts @team1
@video = "test sub method"
def self.getVideo
@video
end
end
def renderOutput(thing)
puts thing
end
end
def encoderLoop
teamList= XMLSortNClean.new('http://#########.com/EncoderService.asmx/GetTeams',TeamModule, "team_Info", "post")
teamList.gimmieMyData
puts teamList.getTeams
teamList.renderOutput
loopChecker = TagChecker.new(encoderLoop())
loopChecker.run
end
encoderLoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment