You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将WinRT的Audio/VideoEncodingProperties转换为IMFMediaType?

WinRT编码属性转IMFMediaType及IInspectable值检查方案

一、WinRT编码属性转IMFMediaType的正确方法

WinRT的AudioEncodingProperties/VideoEncodingProperties无法直接通过QueryInterface转换为IMFMediaType,必须使用WinRT与Media Foundation的专用互操作API完成转换:

  • 先引入头文件:#include <windows.media.mediaproperties.interop.h>
  • 调用GetIMFMediaTypeFromEncodingProperties函数,该函数专门负责将WinRT编码属性对象转换为IMFMediaType实例。

示例代码:

#include <windows.media.mediaproperties.interop.h>
#include <mfapi.h>
#include <winrt/Windows.Media.MediaProperties.h>

using namespace winrt;
using namespace Windows::Media::MediaProperties;

IMFMediaType* ConvertToIMFMediaType(EncodingProperties const& props)
{
    IMFMediaType* mfType = nullptr;
    HRESULT hr = GetIMFMediaTypeFromEncodingProperties(props, IID_PPV_ARGS(&mfType));
    return SUCCEEDED(hr) ? mfType : nullptr;
}

// 使用示例
auto audioProps = AudioEncodingProperties::CreateAac(48000, 2, 128000);
auto mfAudioType = ConvertToIMFMediaType(audioProps);
// 使用完成后记得释放资源
if (mfAudioType) mfAudioType->Release();

auto videoProps = VideoEncodingProperties::CreateUncompressed(MediaEncodingSubtypes::Bgra8(), 1920, 1080);
auto mfVideoType = ConvertToIMFMediaType(videoProps);
if (mfVideoType) mfVideoType->Release();

二、检查AudioEncodingProperties.Properties中的IInspectable值

AudioEncodingProperties.Properties是IMap<guid, IInspectable>类型,存储的是Media Foundation属性的WinRT包装实例,可通过两种方式检查值:

  1. 转换为具体WinRT数值类型:用try_as尝试将IInspectable转为IReference<uint32_t>、IReference<double>等具体类型,再读取值。
  2. 转为IMFAttributes直接读取:底层属性对应Media Foundation的属性集,可将IInspectable转为IMFAttributes,用MF原生API读取属性。

示例代码:

void InspectAudioProperties(AudioEncodingProperties const& audioProps)
{
    auto propsMap = audioProps.Properties();
    for (auto const& pair : propsMap)
    {
        guid propKey = pair.Key();
        IInspectable propValue = pair.Value();

        // 尝试转为IMFAttributes,直接用MF API读取
        auto mfAttr = propValue.try_as<IMFAttributes>();
        if (mfAttr)
        {
            DWORD uintValue = 0;
            HRESULT hr = mfAttr->GetUINT32(propKey, &uintValue);
            if (SUCCEEDED(hr))
            {
                // 处理读取到的DWORD值
            }
            continue;
        }

        // 尝试转为uint32_t类型
        auto uintRef = propValue.try_as<IReference<uint32_t>>();
        if (uintRef)
        {
            uint32_t value = uintRef.Value();
            // 处理数值
            continue;
        }

        // 尝试转为double类型
        auto doubleRef = propValue.try_as<IReference<double>>();
        if (doubleRef)
        {
            double value = doubleRef.Value();
            // 处理数值
        }

        // 尝试转为字符串类型
        auto strRef = propValue.try_as<IReference<hstring>>();
        if (strRef)
        {
            hstring value = strRef.Value();
            // 处理字符串
        }
    }
}

三、直接QueryInterface失败的原因

AudioEncodingProperties和VideoEncodingProperties是WinRT封装类,其COM实现并未直接暴露IMFMediaType接口,必须通过专用互操作函数完成WinRT到Media Foundation的逻辑映射,这也是官方推荐的转换路径。

内容的提问来源于stack exchange,提问作者Tom Huntington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:45:15