May 14, 2009

How To: Extracting the embedded album art from a MP3 / WMA

I'm using Windows Media Sync Reader in Windows Media Format SDK to get the album art from id3 tag of a MP3, it can also get from a WMA.

Code Snippet:

#include <windows.h>
#include <wmsdk.h>
#include <atlbase.h>

#pragma comment(lib, "wmvcore.lib")

bool ExtractEmbeddedAlbumArt(LPCWSTR wszAudioFile)
  bool bOK = false;

  WM_PICTURE* pPicture = NULL;;

  do {

    CComPtr<IWMSyncReader> pIWMSyncReader;
    if(FAILED(WMCreateSyncReader(NULL, 0, &pIWMSyncReader))) break;

    if(FAILED(pIWMSyncReader->Open(wszAudioFile))) break;

    CComPtr<IWMHeaderInfo3> pIWMHeaderInfo3;
    if(FAILED(pIWMSyncReader->QueryInterface(&pIWMHeaderInfo3))) break;

    WORD wStreamNum = 0;
    WORD wLength = 0;
      &wStreamNum, g_wszWMPicture, &wmtDataType, NULL, &wLength))) break;

    pPicture = (WM_PICTURE*)new BYTE[wLength];

      &wStreamNum, g_wszWMPicture, &wmtDataType, (BYTE*)pPicture, &wLength))) break;

    bOK = true;

  } while(false);

    // TODO: Save the picture or do something with it

    delete [] (BYTE*)pPicture;
    pPicture = NULL;

  return bOK;