简体   繁体   中英

C#: Retrieve Video Codec Information from a WMV file

I am having a hard time retrieving the video codec information from a WMV file. I am using .Net 2.0 in C# (I know it is old.. but it is required). I have looked in to using DirectShowLib amoungst other things, although I just can not find where to locate this information.

For Reference, Here is a picture of what I am looking to obtain: http://imgur.com/yNSKo

I would like to avoid using 3rd party dll's, but if I must I will. 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. I added WMP.dll as a reference to attain this information. Once I added the WMP.dll all I did was locate the Hex for the codec and compared off that. 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.

I hope this helps the next person who is trying to find specific codec's with in their WMV's.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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