如何使用 IPropertyStore 通过 C++ 访问 Windows 中的 mp3 元数据?

发布于 2024-12-22 14:33:52 字数 1511 浏览 2 评论 0原文

给定 mp3 文件的路径,

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

具体来说,我如何从这里:

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

到这里:

store->GetValue(PKEY_Music_AlbumArtist, &variant);

这里有一些伪内容代码来帮助澄清我想要做的事情:

#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++(这是一个很长的故事)。

最初,在研究了 mp3 元数据之后,似乎使用 TagLib 的 ID3 标签是正确的选择。因此,我编写了一个在几个领域运行良好的实用程序。但后来我发现 TagLib 仅限于许多可能字段的一小部分,而我想访问所有这些字段。

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

我修改了 TagLib 源以访问专辑艺术家而不是贡献艺术家(通过将所有出现的“\251ART”更改为“\141ART”),但它不起作用。

我确信有一种方法可以使用 ID3 标签完成所有工作,但我不想依赖额外的东西,例如 TagLib、ZLIB 和 CMake 工具。我想使用 IPropertyStore,因为它是内置的,如果我能克服这个障碍,这似乎是最简单的方法。

我在网上找到了一些 IPropertyStore 的示例,我尝试按摩它们以满足我的需要,但没有任何运气,我仍然很困惑。

MSDN“帮助”一点帮助都没有——没有规范,没有示例——它甚至没有告诉我要包含哪个头文件。与以前相比,MSDN 帮助现在很糟糕,或者我错过了什么?没有谷歌我就完蛋了。无论如何......

我希望有人可以用 3 或 4 行代码向我展示如何为我的目的创建和初始化 IPropertyStore。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-12-29 14:33:52

答案如下:

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

感谢 Logan Capaldo 为我指明了正确的方向。当我发现时我不需要这两个功能

SHGetPropertyStoreFromParsingName()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文