DELPHI는 플래시 파일의 동영상 길이, 원래 크기, 프레임 수 등 정보를 가져옵니다.

//전재, 저작권은 원작자 소유
DELPHI는 플래시 파일의 동영상 길이, 원래 크기, 프레임 수 등 정보를 가져옵니다.
'부드러운 칼잡이'의 노력에 감사드립니다. 괜찮은 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.


=====================
실행 인스턴스:
procedure TForm1.Button4Click(Sender: TObject);
var
    Head: TSWFHeader;
    Xmin, Ymin, XMax,YMax:integer;
begin
    M.Clear;
    if not FIleExists(Edit1.Text) then exit;
    if not GetSwfFileHeader(Edit1.Text, Head)
        then SHowmessage('    ');

    //     div 20           
    //XMin:=Head.FrameSize.Xmin div 20;
    XMax:=Head.FrameSize.Xmax div 20;
    //YMin:=Head.FrameSize.Ymin div 20;
    YMax:=Head.FrameSize.Ymax div 20;
    //M.Lines.Add(inttostr(XMin));

    M.Lines.Add('       :'+inttostr(XMax));
    //M.Lines.Add(inttostr(YMin));
    M.Lines.Add('       :'+inttostr(YMax));
end;

 

좋은 웹페이지 즐겨찾기