HanDs
管理员

[Delphi文章] 取Flash影片时长、原始大小、帧数等信息 



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Zlib, OleCtrls, ShockwaveFlashObjects_TLB, StdCtrls;

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;


type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button1: TButton;
    ShockwaveFlash1: TShockwaveFlash;
    OpenDialog1: TOpenDialog;
    Edit5: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

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;

 

procedure TForm1.Button1Click(Sender: TObject);
var
  TemSWF:TSWFHeader;
begin
  if OpenDialog1.Execute then
  begin
    if GetSwfFileHeader(OpenDialog1.FileName,TemSWF) then
    begin
      Edit1.Text:=IntToStr(TemSWF.FrameSize.Xmin);
      Edit2.Text:=IntToStr(TemSWF.FrameSize.Xmax);
      Edit3.Text:=IntToStr(TemSWF.FrameSize.Ymin);
      Edit4.Text:=IntToStr(TemSWF.FrameSize.Ymax);

      ShockwaveFlash1.Width:=TemSWF.FrameSize.Xmax div 20;
      ShockwaveFlash1.Height:=TemSWF.FrameSize.Ymax div 20;
      ShockwaveFlash1.Movie:=OpenDialog1.FileName;
      PostMessage(ShockwaveFlash1.Handle,WM_LBUTTONDOWN,0,0);

    end;
  end;
end;

end.


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
Fl as h 影片 时长
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组
快速回复