使用 Windows Media Foundation 创建 NV12 编码的视频 [英] Creating NV12 encoded video using windows media foundation

查看:58
本文介绍了使用 Windows Media Foundation 创建 NV12 编码的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用本教程使用 Windows Media Foundation 创建带有原始固定彩色图像的视频文件 http://blogs.msdn.com/b/eternalcoding/archive/2013/03/06/developing-a-winrt-component-to-create-a-video-file-using-media-foundation.aspx

I was learning to create video files with raw fixed colored images using Windows Media Foundation using this tutorial http://blogs.msdn.com/b/eternalcoding/archive/2013/03/06/developing-a-winrt-component-to-create-a-video-file-using-media-foundation.aspx

我能够创建上述链接中显示的 RGB 编码的 WMV 视频.但是我需要从图像创建 NV12 格式的 YUV 编码的 MP4 视频.由于 NV12 为每帧使用 (W x H x 3/2) 字节的内存缓冲区,我为图像缓冲区创建了 (W x H x 3/2) 字节的缓冲区(教程中的变量目标")并设置了一些固定的值作为图像数据.然后为 MFCopyImage() 设置图像步幅W"(ARGB 使用 Wx4 作为步幅),并将高度设置为H".但这似乎不起作用.MF_MT_SUBTYPE 设置为 MFVideoFormat_NV12.

I was able to create RGB encoded WMV video shown in the above link. But i need to create YUV encoded MP4 video in NV12 format from image. Since NV12 use (W x H x 3/2) bytes of memory buffer for each frame, i created (W x H x 3/2) bytes of buffer for image buffer(variable 'target' in the tutorial) and set some fixed values to it as image data. Then set image stride 'W' (ARGB uses Wx4 as stride) for the MFCopyImage() and height is set to just 'H'. But that doesn't seem to work. MF_MT_SUBTYPE is set to MFVideoFormat_NV12.

创建 NV12 编码的 mp4 视频文件的正确方法是什么?

What is the correct way to create a NV12 encoded mp4 video file?

推荐答案

我怀疑你做了太多的工作(希望这是个好消息).首先请记住,该示例不会将输出编码为 RGB.RGB 只是接收器的输入类型,您可以随意保留这种方式.

I suspect you are doing too much of the work (which hopefully is good news). First keep in mind that the example does not encode the output as RGB. RGB was simply the input type to the sink, and you can feel free to leave it that way.

容器是 MFTranscodeContainerType_ASF(稍后会详细介绍),子类型是 MFVideoFormat_WMV3,结果是一个压缩良好的 windows 媒体视频文件.

The container is MFTranscodeContainerType_ASF (more on that in a sec), and the subtypte is MFVideoFormat_WMV3, and the result is a well compressed windows media video file.

在创建接收器编写器时,在您的情况下,容器会自动创建,因为未指定属性.实际上,添加的唯一属性是以下几行的 MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS:

When the sink writer is created, in your case the container is automatically created, since the attribute wasn't specified. In fact, the only attribute added is MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS on the following lines:

MFCreateAttributes(&spAttr, 10);
spAttr->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true);
hr = MFCreateSinkWriterFromURL(L".wmv", 
                               spByteStream.Get(), 
                               spAttr.Get(), 
                               &sinkWriter);

这就引出了一个问题——为什么要分配 10 个属性?"...(耸肩)哦,没关系.

Which begs the question - 'why are 10 attributes allocated?' ... (shrug) oh well, doesn't matter.

关键是,您将 RGB 帧发送到接收器,并且正在进行转换以完成对您的规范的编码.容器可以在这里找到:

The point being, you send RGB frames to the sink, and transforms are being done to complete the encoding to your specification. Containers may be found here:

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

在创建接收器之前,您需要指定一个类似于以下内容的给定容器:

and prior to creating the sink, you would specify a given container similar to the following:

spAttr->SetGUID(MF_TRANSCODE_CONTAINERTYPE, MFTranscodeContainerType_ASF);
// or MFTranscodeContainerType_MPEG4, MFTranscodeContainerType_AVI, etc

您之前使用以下行设置了子类型:

You previously set the subtype using the following line:

hr = mediaTypeOut->SetGUID(MF_MT_SUBTYPE, encodingFormat);
// where encodingFormat was hardcoded to MFVideoFormat_WMV3 in the ctor

但是请记住,某些格式需要自下而上的图像.尝试将文件扩展名更改为.mp4",将容器更改为 MFTranscodeContainerType_AVI,将子类型更改为 MFVideoFormat_NV12,您将看到相同的视频颠倒.与 wmv 相比,您的文件会很大,因为所有 YUV 值都放在文件中.

Keep in mind however, some formats expect bottom-up images. Try changing the file extension to ".mp4", container to MFTranscodeContainerType_AVI, and the subtype to MFVideoFormat_NV12 and you will see your same video upside-down. And your file will be huge comparing to the wmv since all of the YUV values are placed into the file.

正确处理容器和格式细节需要非常小心,有些细节很难研究.

Getting the container and format details correct requires a good deal of care, and some details are difficult to research.

希望这会有所帮助.

这篇关于使用 Windows Media Foundation 创建 NV12 编码的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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