Skip to content

Instantly share code, notes, and snippets.

@hjanuschka
Created January 16, 2017 11:14
Show Gist options
  • Save hjanuschka/b2312122c5c81b48ef7783256e42325e to your computer and use it in GitHub Desktop.
Save hjanuschka/b2312122c5c81b48ef7783256e42325e to your computer and use it in GitHub Desktop.
akamai_sprite_generator
module KMM
class SpriteGenerator
attr_accessor :files
attr_accessor :gridsize
attr_accessor :coords
def initialize(path = ".")
@files = Dir["./*.jpg"]
@files.sort!
@gridsize = Math.sqrt(files.length).ceil.to_i
@coords = get_geometry(files.first)
end
def get_geometry(file)
`identify -format "%g" #{file}`.strip
end
def generate_sprite
`montage #{@files.join(" ")} -tile #{@gridsize}x#{@gridsize} -geometry #{@coords} sprite.png`
end
def output
puts "WEBVTT"
puts ""
generate_sprite
tsize = get_thumb_size(files.first)
files.each_with_index do | f, idx |
time_matches = f.scan(/_([0-9]+)h\.([0-9]+)m\.([0-9]+)s\.jpg/)
hours = time_matches.first[0]
minutes = time_matches.first[1]
seconds = time_matches.first[2]
# next one
if idx+1 > files.length-1
next
end
time_matches = files[idx+1].scan(/_([0-9]+)h\.([0-9]+)m\.([0-9]+)s\.jpg/)
nhours = time_matches.first[0]
nminutes = time_matches.first[1]
nseconds = time_matches.first[2]
xywh = get_grid_coordinates(idx+1,@gridsize,tsize[:w],tsize[:h])
puts "#{hours}:#{minutes}:#{seconds}.000 --> #{nhours}:#{nminutes}:#{nseconds}.000 "
puts "sprite.png#xywh=#{xywh}"
puts ""
end
end
def get_thumb_size(f)
i = `identify -format "%g" #{f}`.strip
w,h = i.split("+").first.split("x")
return {
w: w.to_i,
h: h.to_i
}
end
def get_grid_coordinates(image_index,gridsize,w,h)
y = (image_index - 1)/gridsize
x = (image_index -1) - (y * gridsize)
imgx = x * w
imgy =y * h
"#{imgx},#{imgy},#{w},#{h}"
end
end
end
a = KMM::SpriteGenerator.new(".")
a.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment