-
I'm trying to find the stage height and width of .swf's and pass it to a movie with xml.
I found these two relevant posts on how to grab the height and width of a gif and the theory behind doing it for a swf:
http://board.flashkit.com/board/show...hreadid=285002
http://board.flashkit.com/board/show...hreadid=122862
I'm a perl novice. Can someone post an example script that would parse the correct header? Also, in the second example, it states that the xmax and ymax would be returned. But that's not the stage size. Is there a way to get the stage size?
-
Hi,
since this is probably not exactly novice's reading: you will find the theory behind the code at http://www.openswf.org - check the spec section
Code:
$buffer = 0;
$bufbits = 0;
sub readnbits
{ my $n = shift;
my $ret = $buffer;
if($n == $bufbits)
{ $bufbits = 0;
$buffer = 0;
return $ret;
}
if($n > $bufbits)
{ $n -= $bufbits;
while($n > 8)
{ $ret <<= 8;
read SWF, $buffer, 1; $buffer = ord($buffer);
$ret += $buffer;
$n -= 8;
}
read SWF, $buffer, 1; $buffer = ord($buffer);
if($n > 0)
{ $ret <<= $n;
$bufbits = 8 - $n;
$ret += $buffer >> (8-$n);
$buffer &= (1 << $bufbits)-1;
}
return $ret;
}
$ret = $buffer >> ($bufbits-$n);
$bufbits -= $n;
$buffer &= (1 << $bufbits)-1;
return $ret;
}
sub readnSbits
{ my $n = shift;
my $temp = readnbits($n);
return ($temp - (1 << $n)) if($temp & (1 << ($n-1)));
return $temp;
}
sub swfhdr
{ my $swf = shift;
my $buf, $vers, $fsize, $nbits, $xl, $yl, $xr, $yr, $rect, $rate, $frames;
open SWF, $swf || return undef;
read SWF, $buf, 3;
return undef if($buf ne "FWS");
read SWF, $vers, 1; $vers = ord($vers);
read SWF, $buf, 4; ($fsize) = unpack "V", $buf;
$nbits = readnbits(5);
$xl = readnbits($nbits); $xr = readnbits($nbits);
$yl = readnbits($nbits); $yr = readnbits($nbits);
my $tmplen = (tell SWF) - 8;
seek SWF, 8, 0;
read SWF, $rect, $tmplen;
read SWF, $buf, 4;
($rate, $frames) = unpack("vv", $buf);
return ($vers, $fsize, $xr-$xl, $yr-$yl, $rect, $rate, $frames);
}
sub writeswfheader
{ my ($swf, $vers, $fsize, $rect, $rate, $frames) = @_;
open OSWF, ">$swf" || die "cannot write to $swf\n";
print OSWF "FWS" . chr($vers);
print OSWF pack("V", $fsize) . $rect . pack("vv", $rate, $frames);
}
## testing
($vers, $fsize, $x, $y, $rect, $rate, $frames) = swfhdr ("3554.swf");
printf "movie size %f %f\n", $x/20, $y/20;
printf "rate %f frames %d\n", $rate/256, $frames;
writeswfheader("test3554.swf", $vers, $fsize, $rect, $rate, $frames);
Have fun :)
Musicman
-
Cool. Thanks a lot. Worked like a charm.