如何使用IPropertyStore在Windows中使用C ++访问mp3元数据? [英] How do I use IPropertyStore to access mp3 metadata in Windows with C++?

查看:444
本文介绍了如何使用IPropertyStore在Windows中使用C ++访问mp3元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出mp3文件的路径,

Given the path of an mp3 file,

如何创建和初始化IPropertyStore对象以对该文件的元数据进行操作?

具体来说,我怎么从这里得到:

Specifically, how do I get from here:

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

到这里:

store->GetValue(PKEY_Music_AlbumArtist, &variant);

下面是一些伪ish代码,以帮助阐明我要执行的操作:

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);
}

背景

也许有一种更好的语言可以做到这一点,但是我想使用C ++(这是一个很长的故事).

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

最初,在研究了mp3元数据之后,采用TagLib的ID3标签似乎是必经之路.因此,我编写了一个在两个领域都可以正常工作的实用程序.但是后来我发现TagLib局限于许多可能的字段的一小部分,我想访问所有这些字段.

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.

我最关心的字段是专辑艺术家,因为Windows Media Player将其用作无法更改的默认排序顺序.

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.

我修改了TagLib源,以访问专辑艺术家而不是贡献者艺术家(通过将所有出现的'\ 251ART'更改为'\ 141ART'),但是没有用.

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.

我敢肯定,有一种方法可以使用ID3标签完成所有工作,但是我宁愿不依赖于TagLib,ZLIB和CMake工具之类的多余东西.我想使用IPropertyStore,因为它是内置的,这似乎是最简单的方法,如果我能克服这一障碍.

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.

我在网络上找到了一些IPropertyStore的示例,但我一直在努力地按摩它们以适应我的需求,但没有任何运气,我还是很迷惑.

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帮助"一点也不有用-没有规范,没有示例-它甚至没有告诉我要包含哪个头文件.与以前相比,MSDN帮助现在很糟糕,还是我错过了一些东西?没有Google,我会被搞砸.无论如何...

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 ...

我希望有人可以用3或4行代码向我展示如何为我的目的创建和初始化IPropertyStore.预先感谢.

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.

推荐答案

答案如下:

#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()

这篇关于如何使用IPropertyStore在Windows中使用C ++访问mp3元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆