filedatedelphiextractavi

Extract correct creation date from avi file


I am using Delphi 10 and Windows 10 Home Edition 64-bit. I have a video file called MVI_0640.AVI. The date shown in the File Explorer is 15/04/04 which corresponds to the Media Created Date in the Properties window.

I am using the following code to extract dates.

procedure TForm1.Button1Click(Sender: TObject);
var
  ADate: TDateTime;
  FlHandle: integer;
  MyData: TWin32FindData;
  FlTime: TFileTime;
  MySysTime: TSystemTime;
begin
  {get date using GetCreationTime}
  ADate := TFile.GetCreationTime(FlName);
  Memo1.Lines.Add('GetCreationTime ' + DateToStr(ADate));

  {get date using FileGetDate}
  FlHandle := FileOpen(FlName,fmOpenRead);
  ADate := FileDateToDateTime(FileGetDate(FlHandle));
  FileClose(FlHandle);
  Memo1.Lines.Add('FileGetDate ' + DateToStr(ADate));

  {get date using FindFirstFile}
  FindFirstFile(PChar(FlName), MyData);

  FlTime := MyData.ftCreationTime;
  FileTimeToSystemTime(FlTime, MySysTime);
  ADate := SystemTimeToDateTime(MySysTime);
  Memo1.Lines.Add('ftCreationTime ' + DateToStr(ADate));

  FlTime := MyData.ftLastAccessTime;
  FileTimeToSystemTime(FlTime, MySysTime);
  ADate := SystemTimeToDateTime(MySysTime);
  Memo1.Lines.Add('ftLastAccessTime ' + DateToStr(ADate));

  FlTime := MyData.ftLastWriteTime;
  FileTimeToSystemTime(FlTime, MySysTime);
  ADate := SystemTimeToDateTime(MySysTime);
  Memo1.Lines.Add('ftLastWriteTime ' + DateToStr(ADate));

end;

The result looks like this:

None of the dates reflect the Media Created Date. How can I extract it?

In answer to Tom Brunberg’s comment I attach an extract of the file taken with a hex editor. enter image description here


Solution

  • I found an answer to my question. Probably not very elegant or save but it does it's job. I tested it on more than 100 files and it worked without a problem. Here is my answer:

    function TForm1.GetAviMediaCreationDate(AFile: string): TDateTime;
    var
      FS: TFileStream;
      NumOfChar: integer;
      i,d: integer;
      ABuffer: array of byte;
      AStr: string;
      DateStr: string;
      sdp: integer; //start date position
      dn,mn,yn: integer; //used to encode date
    begin
      sdp := 0;
      FS := TFileStream.Create(AFile,fmOpenRead);
      NumOfChar := 400;
      SetLength(ABuffer,NumOfChar);
      FS.Read(Pointer(ABuffer)^, NumOfChar);
      {find IDIT}
      for i := 0 to NumOfChar-1 do
      begin
        AStr := Char(ABuffer[i]) +
                Char(ABuffer[i+1]) +
                Char(ABuffer[i+2]) +
                Char(ABuffer[i+3]);
        if AStr = 'IDIT' then sdp := i+7;
      end;
      {extract date}
      for d := 1 to 24 do
      DateStr := DateStr + Char(ABuffer[sdp+d]);
      {assemble TDateTime}
      //123456789 123456789 123456789
      //Sun Jun 28 10:13:39 2015
      dn := StrToInt(Copy(DateStr,9,2));
      mn := IndexText(Copy(DateStr,5,3),ShortMonthNames)+1;
      yn := StrToInt(Copy(DateStr,21,4));
      Result := EncodeDate(yn, mn, dn);
      FS.Free;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      ADate: TDateTime;
    begin
      ADate := GetAviMediaCreationDate(FlName);
      Memo1.Lines.Add(DateToStr(ADate));
    end;