unit VideoDuration;
interface
function GetVideoDuration(FileName:
String): TDateTime;
implementation
uses
DirectShow9,
ActiveX, Dialogs, SysUtils, MPlayer, Forms;
var
hr: HRESULT;
MediaDet: IMediaDet;
procedure ClearDirectShow;
begin
MediaDet :=
nil;
end;
function DurationFromMediaPlayer(FileName:
String): TDateTime;
var
MediaPlayer: TMediaPlayer;
begin
MediaPlayer := TMediaPlayer.Create(
nil);
MediaPlayer.Visible := False;
MediaPlayer.Parent := Application.MainForm;
MediaPlayer.FileName := FileName;
MediaPlayer.TimeFormat := tfMilliseconds;
MediaPlayer.Open;
Result := MediaPlayer.Length / 1000 / (24 * 60 * 60);
MediaPlayer.Close;
FreeAndNil(MediaPlayer);
end;
function GetVideoDuration(FileName:
String): TDateTime;
var
Duration: Double;
wFileName: PWideChar;
begin;
ClearDirectShow;
Result := 0;
wFileName := StringToOleStr(FileName);
try
hr := CoCreateInstance(CLSID_MediaDet,
nil, CLSCTX_INPROC, IMediaDet, MediaDet);
if hr = S_OK
then
hr := MediaDet.put_Filename(wFileName);
if hr = S_OK
then
hr := MediaDet.get_StreamLength(Duration);
if hr = S_OK
then
Result := Duration / (24 * 60 * 60)
else
Result := DurationFromMediaPlayer(FileName);
finally
SysFreeString(wFileName);
end;
ClearDirectShow;
end;
initialization
CoInitialize(
nil);
finalization
CoUnInitialize;
end.