Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244983
  • 博文数量: 28
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 468
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-03 10:33
文章分类

全部博文(28)

文章存档

2011年(1)

2009年(14)

2008年(13)

我的朋友

分类: LINUX

2009-02-22 11:45:06

收集到的一份关于如何获取flash文件的影片时长,原始尺寸,帧数等信息的代码:

unit uFlash;

interface

uses
    windows, SysUtils, Classes, Zlib;

type
    TBitWidth = Integer;
    TSWFRect = packed record
        Xmin: Integer; // in twips

        Xmax: Integer; // in twips

        Ymin: Integer; // in twips

        Ymax: Integer; // in twips

    end;

    TSWFColor = packed record
        R : Byte;
        G : Byte;
        B : Byte;
        RGB : string;
    end;

    TSWFHeader = packed record
        Signature: array[0..2] of char;
        Version: byte;
        FileLength: cardinal;
        FrameSize: TSWFRect;
        FrameRate: byte;
        FrameRateRemainder: byte;
        FrameCount: cardinal;
        BkColor : TSWFColor;
    end;

    TTagHandle = record
        ID : Integer;
        Length : Integer;
        HandleLength : Integer;
    end;
   
function GetSwfFileHeader(const FileName: string; var Header: TSWFHeader): boolean;

implementation

function ReadNBits(const Buffer; Position: longint; Count: TBitWidth): longint;
var
    I, B: longint;
begin
    Result := 0;
    B := 1 shl (Count - 1);
    for I := Position to Position + Count - 1 do
    begin
        if (PByteArray(@Buffer)^[I div 8] and (128 shr (I mod 8))) <> 0 then
            Result := Result or B;
        B := B shr 1;
    end;
end;

function ReadTagHandle(const Buffer; Position : Longint): TTagHandle;
var
    v : Smallint;
begin
    v := PSmallint(@PByteArray(@Buffer)^[Position])^;
    Result.ID := v shr 6;
    v := v shl 10;
    Result.Length := v shr 10;
    if Result.Length = $FFFFFF then
    begin
        //长tag

        Result.HandleLength := 6;
        Result.Length := PInteger(@PByteArray(@Buffer)^[Position + 2])^;
    end
    else
        Result.HandleLength := 2;
end;

function GetSwfFileHeader(const FileName: string; var Header: TSWFHeader): boolean;
const
    BuffSize = 64;
var
    Buffer: PByteArray;
    NBitsField: byte;
    Poz: longword;
    FileStream: TFileStream;
    MemStream: TMemoryStream;
    ZStream: TDecompressionStream;
    Tag : TTagHandle;
begin
    Result := False;
    if not FileExists(FileName) then
        Exit;
    FileStream := TFileStream.Create(FileName, fmOpenRead);
    try
        FileStream.Position := 0;
        if FileStream.Size > 22 then
        begin
            GetMem(Buffer, BuffSize);
            try
                FileStream.Read(Header, 8);
                if (Header.Signature = 'CWS ') and (Header.Version > = 6) then
                begin
                    Result := True;
                    MemStream := TMemoryStream.Create;
                    try
                        MemStream.CopyFrom(FileStream, FileStream.Size - 8);
                        MemStream.Position := 0;
                        ZStream := TDecompressionStream.Create(MemStream);
                        try
                            ZStream.Read(Buffer^, BuffSize);
                        finally
                            ZStream.Free;
                        end;
                    finally
                        MemStream.Free;
                    end;
                end
                else
                begin
                    FileStream.Read(Buffer^, BuffSize);
                    Result := Header.Signature = 'FWS ';
                end;

                if Result then
                    with Header do
                    begin
                        Poz := 0;
                        NBitsField := TBitWidth(ReadNBits(Buffer^, Poz, 5));
                        Inc(Poz, 5);
                        FrameSize.Xmin := Integer(ReadNBits(Buffer^, Poz, NBitsField));
                        Inc(Poz, NBitsField);
                        FrameSize.Xmax := Integer(ReadNBits(Buffer^, Poz, NBitsField));
                        Inc(Poz, NBitsField);
                        FrameSize.Ymin := Integer(ReadNBits(Buffer^, Poz, NBitsField));
                        Inc(Poz, NBitsField);
                        FrameSize.Ymax := Integer(ReadNBits(Buffer^, Poz, NBitsField));
                        Inc(Poz, NBitsField);
                        NBitsField := Poz mod 8;
                        Poz := Poz div 8;
                        if (NBitsField > 0) then
                            Inc(Poz);
                        FrameRateRemainder := Buffer^[Poz]; // 8.[8]

                        FrameRate := Buffer^[Poz + 1];
                        FrameCount := Buffer^[Poz + 2] or (Buffer^[Poz + 3] shl 8);
                        Inc(Poz, 4);
                        Tag := ReadTagHandle(Buffer^, poz);
                        while Tag.ID <> 9 do
                        begin
                            Inc(Poz, Tag.Length + Tag.HandleLength);
                            Tag := ReadTagHandle(Buffer^, Poz);
                        end;
                        BkColor.R := Buffer^[Poz + 2];
                        BkColor.G := Buffer^[Poz + 3];
                        BkColor.B := Buffer^[Poz + 4];
                        BkColor.RGB := StringReplace(
                                Format( '%2x%2x%2x ', [BkColor.R, BkColor.G, BkColor.B]),
                                ' ', '0 ', [rfReplaceAll]);
                    end;

            finally
                FreeMem(Buffer);
            end;
        end;
    finally
        FileStream.Free;
    end;
end;

end.
  

阅读(2263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~