Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use IPropertyStore to access mp3 metadata in Windows with C++?

Given the path of an mp3 file,

how do I create and initialize an IPropertyStore object to operate on the metadata of that file?

Specifically, how do I get from here:

"C:\\Music\\Viva Las Vegas.mp3"

to here:

store->GetValue(PKEY_Music_AlbumArtist, &variant);

Here's some pseudo-ish code to help clarify what I'm trying to do:

#include "stdafx.h"
#include <propsys.h>
#include <propkey.h>

void main ()
{
    // property store must somehow represent the mp3 file
    IPropertyStore* store = "C:\\Music\\Viva Las Vegas.mp3"; // HELP!
    PROPVARIANT variant;

    // get the existing album artist
    store->GetValue(PKEY_Music_AlbumArtist, &variant);
    assert(variant== "Elvis Presley");

    // set it to something else
    variant= "ZZ Top";
    store->SetValue(PKEY_Music_AlbumArtist, variant);
}

BACKGROUND

Perhaps there is a better language for doing this but I want to use C++ (it's a long story).

Originally, after researching mp3 metadata, it seemed like ID3 tags with TagLib was the way to go. So I wrote a utility that worked fine on a couple of fields. But then I discovered that TagLib is limited to a small subset of the many possible fields, and I want access to all of them.

The field I'm most concerned with is Album Artist because Windows Media Player uses it as the default sort order which cannot be changed.

I modified the TagLib source to access the Album artist instead of the Contributing artist (by changing all occurrences of '\251ART' to '\141ART') but it didn't work.

I'm sure there is a way that everything can be done with ID3 tags but I'd rather not rely on extra stuff like TagLib, ZLIB and the CMake facility. I want to use IPropertyStore because it's built in, and it seems like the simplest way if I can just get over this hurdle.

I found a few examples of IPropertyStore on the web, and I've tried to massage them to suit my needs without any luck, I am still mystified.

MSDN "Help" isn't the least bit helpful -- no specs, no examples -- it doesn't even tell me which header file to include. MSDN Help is terrible now compared to what it used to be, or am I missing something? Without Google I'd be screwed. Anyway ...

I hope someone can show me in 3 or 4 lines of code how to create and initialize IPropertyStore for my purpose. Thanks in advance.

like image 488
dog44wgm Avatar asked Feb 22 '23 00:02

dog44wgm


2 Answers

Here's the answer:

#include <shobjidl.h>   // SHGetPropertyStoreFromParsingName, etc
#include <propkey.h>    // PKEY_Music_AlbumArtist
#include <propvarutil.h>// InitPropVariantFromString, needs shlwapi.lib

void main() // error-checking removed
{
    // initialize the COM library
    CoInitialize(NULL);

    // get a property store for the mp3 file
    IPropertyStore* store = NULL;
    SHGetPropertyStoreFromParsingName(L"C:\\Music\\Viva Las Vegas.mp3", 
        NULL, GPS_READWRITE, __uuidof(IPropertyStore), (void**)&store);

    // get the existing album artist ("Elvis Presley")
    PROPVARIANT variant;
    store->GetValue(PKEY_Music_AlbumArtist, &variant);

    // set it to something else
    InitPropVariantFromString(L"ZZ Top", &variant);
    store->SetValue(PKEY_Music_AlbumArtist, variant);
    store->Commit();

    // very important undocumented method
    store->Release();
}

Thanks to Logan Capaldo for pointing me in the right direction. I didn't need those 2 functions when I found

SHGetPropertyStoreFromParsingName()
like image 78
dog44wgm Avatar answered Apr 30 '23 09:04

dog44wgm


Briefly, SHParseDisplayName to get a PIDL you can give to SHGetPropertyStoreFromIDList

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762236(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762196(v=vs.85).aspx

like image 42
Logan Capaldo Avatar answered Apr 30 '23 10:04

Logan Capaldo