简体   繁体   English

C#:从WMV文件检索视频编解码器信息

[英]C#: Retrieve Video Codec Information from a WMV file

I am having a hard time retrieving the video codec information from a WMV file. 我很难从WMV文件中检索视频编解码器信息。 I am using .Net 2.0 in C# (I know it is old.. but it is required). 我在C#中使用.Net 2.0(我知道它很旧。但是它是必需的)。 I have looked in to using DirectShowLib amoungst other things, although I just can not find where to locate this information. 我一直在寻找其他地方使用DirectShowLib的方法,尽管我只是无法找到该信息的位置。

For Reference, Here is a picture of what I am looking to obtain: http://imgur.com/yNSKo 供参考,以下是我想要获得的图像: http : //imgur.com/yNSKo

I would like to avoid using 3rd party dll's, but if I must I will. 我想避免使用第三方dll,但是如果需要的话。 If anyone could help, I would greatly appreciate the information! 如果有人可以提供帮助,我将不胜感激!

Here is some code (although I do not think it is worth its weight in salt): 这是一些代码(尽管我不认为这是值得的):

        WindowsMediaPlayer wmp = new WindowsMediaPlayer();
        IWMPMedia mediaInfo = wmp.newMedia(fileName);
        MessageBox.Show(mediaInfo.getItemInfo("WM/Codec"));

Ok I found the answer. 好的,我找到了答案。 I used: http://www.codeproject.com/Articles/36338/Export-Windows-Media-Player-Music-Metadata-to-XML as a guide. 我使用了: http : //www.codeproject.com/Articles/36338/Export-Windows-Media-Player-Music-Metadata-to-XML作为指南。 I added WMP.dll as a reference to attain this information. 我添加了WMP.dll作为获得此信息的参考。 Once I added the WMP.dll all I did was locate the Hex for the codec and compared off that. 一旦添加了WMP.dll,我所做的就是找到编解码器的十六进制并进行比较。 This will only work if you have the same specific codec that you are looking for every time. 仅当您每次都具有相同的特定编解码器时,这才起作用。 At the very least it will help you pull the hex for the codecs and you can associate the hex value to the video codec from the file properties in WMP. 至少它会帮助您提取编解码器的十六进制,并且可以从WMP中的文件属性将十六进制值与视频编解码器关联。

I hope this helps the next person who is trying to find specific codec's with in their WMV's. 我希望这对下一个正在其WMV中查找特定编解码器的人有所帮助。

    private bool WMPCodecCheck(string fileName)
    {
        try
        {
            WindowsMediaPlayer wmp = new WindowsMediaPlayer();
            wmp.mediaCollection.add(fileName);
            IWMPMedia currentWMV = wmp.newMedia(fileName);
            media = wmp.mediaCollection;

            this.codecType = media.getMediaAtom("FourCC");

            IWMPPlaylist mediaList = null;

            IWMPMedia mediaItem;
            mediaList = media.getByAttribute("MediaType", "Video");

            for (int i = 0; i < mediaList.count; i++)
            {
                mediaItem = mediaList.get_Item(i);
                if (mediaItem.sourceURL.Equals(fileName))
                {
                    if (_hasCodec.Equals(GetCodec(mediaItem)))
                    {
                        //MessageBox.Show("Codec Exists!");
                        wmp.mediaCollection.remove(mediaItem, true);
                        return true;
                    }
                }
            }
            wmp.mediaCollection.remove(currentWMV, true);
            return false;
        }
        catch (Exception e)
        {
            Log.LogToFile("Codec Read Error." + e, LogType.Exception);
        }
        return false;
    }
    private string GetCodec(IWMPMedia mediaItem)
    {
        // Has Codec = 877474375
        // No Codec  = 861293911
        string codec = mediaItem.getItemInfoByAtom(codecType);
        if (string.IsNullOrEmpty(codec))
        {
            codec = mediaItem.getItemInfoByAtom(codecType);
        }
        //MessageBox.Show("Codec Hex Value: " + codec);
        return codec;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM