I found a post with a sample showing how to get the duration of an AVI file:
Getting the duration of an AVI file
I modified it for my purposes in my Delphi 6 application and generated the code below. I originally deleted the line that hovered the IAviFile pointer before calling AviFileExit (). But when I did this, I got an access violation when AviFileExit () is called. I restored the string and violated access rights.
Why is it necessary to name the IAviFile link before calling AviFileExit ()? Is it a memory leak? I would think that a normal count of links to an interface would work correctly here, but obviously this is not the case. Is there any other way to fix the error, for example, call AviStreamRelease () or the like?
Here is my code:
function getAviDurationSecs(theAviFilename: string): Extended;
var
aviFileInfo : TAVIFILEINFOW;
intfAviFile : IAVIFILE;
framesPerSecond : Extended;
begin
intfAviFile := nil;
AVIFileInit;
try
if AVIFileOpen(intfAviFile, PChar(theAviFilename), OF_READ, nil) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Error opening the AVI file: ' + theAviFilename);
try
if AVIFileInfoW(intfAviFile, aviFileInfo, sizeof(aviFileInfo)) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Unable to get file information record from the AVI file: ' + theAviFilename);
if aviFileInfo.dwScale < 1 then
raise Exception.Create('(getAviDurationSecs) Invalid dwScale value found in the AVI file information record: ' + theAviFilename);
framesPerSecond := aviFileInfo.dwRate / aviFileInfo.dwScale;
Result := aviFileInfo.dwLength / framesPerSecond;
finally
AVIFileRelease(intfAviFile);
Pointer(intfAviFile) := nil;
end;
finally
AVIFileExit;
end;
end;