|
| 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}. |
0 commit comments