How to get the Media Length Use DirectShow

zhaozj2021-02-16  90

Hello, Guys!

Glad to see you again!

Now, I will tell you how to get the media length by driectShow!

First, You Should Install DirectX SDK!

When you installing The DirectX SDK, We can Step the next!

Let's Go Into How TO:

(1) Initialized DirectShow, Defined As Global, The Code Looks Like:

#include

#pragma comment (Lib, "Strmiids.lib")

IGRAPHBUILDER * PGB; IMEDIAPSITION * PMP;

#include

REFTIME M_TOTAL = 0;

#define jif (x) IF (failed (hr = (x))) / {returnh HR;}

The First Line Show That We Well Use DirectShow Interface, Next Line Shows The Destination Library.

Before We Use the DirectShow Interface, WE Must Build The Fillet

M_Total IS Used to Storage The Media Length, And Reftime Is Defined in Strmif.h File, So Included this Head file.

Next, the imediaposition interface used to get the destination medium!

WHEN WE Query The Destination Interface of The DirectShow, Sometimes It Maybe Fail,

SO i add a micro, use to estimate failure or not:

#define jif (x) IF (failed (hr = (x))) / {returnh HR;}

(2) WHEN WE HAD INITIALIZED, WE WILL GET The Media Length, Code Looks Like:

HRESULT INITDIRECTSHOW (CSTRING STRFILENAME)

{

Wchar wfile [max_path]; HRESULT HR; Coinitialize (NULL);

#ifndef UNICODE MultiByteToWideChar (CP_ACP, 0, strFileName, -1, wFile, MAX_PATH); # else wcscpy (wFile, strFileName); # endif JIF (CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **) & pGB)) JIF (PGB-> Renderfile (WFILE, NULL)); if (succeededed (hr)) {jif (pgb-> queryinterface (iid_imediaposition, (void **) & pmp);} else {PGB-> Release (); Couninitialize (); return hr;} Sleep (1000); JIF (PMP-> Get_Duration);

PMP-> Release (); pgb-> release ();

Couninitialize (); return hr;

}

Before USE DirectShow, We Must Initialized CORARY First, SO Coinitialize (NULL);

Because The CoM Interface Support Unicode, So We Must Transform The File Name from Ansi To Unicode, Use the lausted code:

#ifndef unicode multibytetowideChar (CP_ACP, 0, StrfileName, -1, WFile, Max_path); # else wcscpy (wfile, strfilename); # ENDIF

Next, We Must Initialize Filtergraph Interface And Return PGB, in ORDER TO Use To Query The iMediaPosition, Use it to get the media length.

.

THEN, We can get the Media File's Play Time by get_duration of the imediaposition interface pointer pgb.the getting time of the media file in second.

WHEN We query the interface, the count will add auto, sowe used complebo, we shop release the count, in order to release the com object safely!

The Last, We Should Release The COBRARY USE The CODE CounInitialize ();

(3) HOW to Call:

TCHAR BIGBUFF [MAX_PATH] / * = _t ("") * /; // maximum common dialog buffer size tchar szfilter [] = _T ("Video files (* .avi; * .dat; * .mpg; * .mpeg) | * .avi; * .dat; * .mpg; * .mpeg | audio files (* .wav; * .mp3) | * .wav; * .mp3 || "); // all files (*. *) . | * * | ZeroMemory (bigBuff, sizeof (bigBuff)); CFileDialog dlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilter); // Modify OPENFILENAME members directly to point to bigBuff dlg.m_ofn.lpstrFile = bigBuff; dlg .m_ofn.nMaxFile = sizeof (bigBuff); if (IDOK == dlg.DoModal ()) {CString strFileName; strFileName = dlg.GetPathName (); if (S_OK == InitDirectShow (strFileName)) {CString strMediaLen; strMediaLen.Format ("" The Destination Media Length:%. 2F S "), M_Total; AFXMessageBox (StrMedialen); Return;} else {afxMessageBox (_T (" Get Media Length Error! "));}}

Very Easy, isn't it?

DirectXSDK9.0 VC6.0 VSP6.0 WinXP!

Good luck!

转载请注明原文地址:https://www.9cbs.com/read-13139.html

New Post(0)