How Big Is That Flash?

We interrupt this blog with a quick technical discussion.

As people start using eManagr, we obviously worry that features seeming obvious in design are, in fact, weird and confusing. So, in the walkthrough, we’ll soon add Flash video for each step.

Problem: I have a special hatred of arbitrary numbers in markup, and if you don’t tell your browser about the video, it…apparently picks a height and width out of a hat. Fun, eh?

Searching Google supplied a lot of advice that boiled down to “just put in the numbers.”

Thankfully, it turns out that the SWF format is relatively well-documented. Unfortunately, it’s the sort of bit-twiddling format that was uncool back when MS-DOS roamed the Earth. In hopes of saving somebody else the trouble of finding the video’s size, here’s the Ruby code we’re using. It’s pretty straightforward, so it wouldn’t be hard to hack as C, PHP, or whatever else you need.

def flashsize(filename)

  flash = File.open(filename)

  bytes = flash.read[8,65]          # The length is semi-arbitrary

  flash.close  size = bytes[0] >> 3 # Yes, the number of bits per coordinate is stored in five bits

  rect = bytes[0..size/2]

  rect.reverse!

  carry = 0

  0.upto(rect.length - 1) do |i|    # Worse:  The coordinates aren't byte-aligned!

    k = rect[i] >> 3

    rect[i] = (rect[i] << 5) | carry

    carry = k

  end

  rect.reverse!

size = size / 8                     # Turn the byte-aligned numbers into the size

  xmin = intfrombytes(rect,size*0,size)

  xmax = intfrombytes(rect,size*1,size)

  ymin = intfrombytes(rect,size*2,size)

  ymax = intfrombytes(rect,size*3,size)

  width = xmax - xmin

  height = ymax - ymin

return width, height

end

def intfrombytes(str, offset, length)

  result = 0

  0.upto(length - 1) do |i|

    result = ( result << 8 ) | str[offset + i]

  end

  return result

end

Ta-da!

Yes, I left out the error checks and ignored cases where coordinates are stored in fractional bytes. Mostly because I’d rather correct those errors by beating up the people who cause the error. But as long as nobody’s that stupid, this finds you the height and width. You can then push that into your video’s HTML tag and everybody will be happy.

Tags: ,

One Response to “How Big Is That Flash?”

  1. John Says:

    I probably should have mentioned this in the article, but we’re releasing the code into the public domain. If you’d like to credit eManagr or tell us about your project (so we get the warm fuzzy feelings), feel free, but you can use the code for whatever purpose you like without restriction or feeling beholden to anybody on this end.

Leave a Reply