Skip to content

Commit 655d54a

Browse files
committed
Initial import.
0 parents  commit 655d54a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
===================
2+
SWF metadata parser
3+
===================
4+
5+
This is an Erlang port of the `hexagonit.swfheader`_ package which is
6+
a SWF metadata parser for Python.
7+
8+
_`http://pypi.python.org/pypi/hexagonit.swfheader/`
9+
10+
Usage
11+
=====
12+
13+
The module exposes two functions. ``parse/1`` parses the the given SWF
14+
file and returns the metadata in a tuple. ``print_header/1`` pretty
15+
prints the metadata in stdout.
16+
17+
You can use the ``parse/1`` function in your own code in the following
18+
way::
19+
20+
{version, Version,
21+
compression, Compression,
22+
width, Width,
23+
height, Height,
24+
bbox, {xmin, Xmin, xmax, Xmax, ymin, Ymin, ymax, Ymax},
25+
frames, Frames,
26+
fps, Fps} = swfheader:parse("/path/to/the/file.swf").
27+
28+
All items have numeric values except ``compression`` which is either
29+
the atom ``compressed`` or ``uncompressed``.
30+
31+
The ``print_header/1`` function can be used directly from the command
32+
line to quickly inspect the metadata in an SWF file, e.g.::
33+
34+
$ erl -pa -s swfheader print_header /path/to/the/file.swf -s init stop -noshell
35+
36+
This will print the following to stdout::
37+
38+
Version: 5
39+
Compression: uncompressed
40+
Dimensions: 550.0 x 400.0
41+
Bounding box: (0.0, 550.0, 0.0, 400.0)
42+
Frames: 3072
43+
FPS: 1
44+
45+
The exact values will naturally depend on the particular SWF file.

swfheader.erl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-module(swfheader).
2+
-export([parse/1, print_header/1, test/0]).
3+
4+
test() ->
5+
{version, 5,
6+
compression, uncompressed,
7+
width, 550.0,
8+
height, 400.0,
9+
bbox, {xmin, 0.0, xmax, 550.0, ymin, 0.0, ymax, 400.0},
10+
frames, 3072,
11+
fps, 1} = parse("test.swf").
12+
13+
print_header(Filename) ->
14+
{version, Version,
15+
compression, Compression,
16+
width, Width,
17+
height, Height,
18+
bbox, {xmin, Xmin, xmax, Xmax, ymin, Ymin, ymax, Ymax},
19+
frames, Frames,
20+
fps, Fps} = parse(Filename),
21+
io:format("Version: ~p~n", [Version]),
22+
io:format("Compression: ~p~n", [Compression]),
23+
io:format("Dimensions: ~p x ~p~n", [Width, Height]),
24+
io:format("Bounding box: (~p, ~p, ~p, ~p)~n", [Xmin, Xmax, Ymin, Ymax]),
25+
io:format("Frames: ~p~n", [Frames]),
26+
io:format("FPS: ~p~n", [Fps]).
27+
28+
parse(Filename) ->
29+
{ok, Swf} = file:read_file(Filename),
30+
{Compression, Version, _, Payload} = parse_signature(Swf),
31+
{Xmin, Xmax, Ymin, Ymax, Frames, Fps} = parse_payload(Payload),
32+
{version, Version,
33+
compression, Compression,
34+
width, Xmax-Xmin,
35+
height, Ymax-Ymin,
36+
bbox, {xmin, Xmin, xmax, Xmax, ymin, Ymin, ymax, Ymax},
37+
frames, Frames,
38+
fps, Fps}.
39+
40+
parse_signature(<<"FWS", Version:8, Size:32/little, Data/binary>>) ->
41+
{uncompressed, Version, Size, Data};
42+
parse_signature(<<"CWS", Version:8, Size:32/little, Data/binary>>) ->
43+
{compressed, Version, Size, zlib:gunzip(Data)}.
44+
45+
parse_payload(Payload) ->
46+
% Extract the RECT field size
47+
<<Nbits:5/little, _:3, _/binary>> = Payload,
48+
% Calculate the byte alignment
49+
ByteAlign = (8 - ((5 + 4 * Nbits) rem 8)) rem 8,
50+
% Pattern match the RECT element
51+
<<_:5,
52+
Xmin:Nbits/big-signed,
53+
Xmax:Nbits/big-signed,
54+
Ymin:Nbits/big-signed,
55+
Ymax:Nbits/big-signed,
56+
_:ByteAlign,
57+
Frames:16/little-unsigned,
58+
Fps:16/little-unsigned,
59+
_/binary>> = Payload,
60+
% Convert the RECT values from Twips to pixels
61+
{Xmin / 20, Xmax / 20, Ymin / 20, Ymax / 20, Frames, Fps}.

test.swf

153 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)