//------------------------------------------------------------------------------
        // Name: DeleteAttrib()
        // Desc: Delete the attribute at the specified index.
        //------------------------------------------------------------------------------
        bool DeleteAttrib(string pwszFileName, ushort wStreamNum, ushort wAttribIndex)
        {
            try
            {
                IWMMetadataEditor MetadataEditor;
                IWMHeaderInfo3    HeaderInfo3;

                WMFSDKFunctions.WMCreateEditor(out MetadataEditor);

                MetadataEditor.Open(pwszFileName);

                HeaderInfo3 = ( IWMHeaderInfo3 )MetadataEditor;

                HeaderInfo3.DeleteAttribute(wStreamNum, wAttribIndex);

                MetadataEditor.Flush();

                MetadataEditor.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
        //------------------------------------------------------------------------------
        // Name: ShowAttributes3()
        // Desc: Displays all attributes for the specified stream, with support
        //       for GetAttributeByIndexEx.
        //------------------------------------------------------------------------------
        bool ShowAttributes3(string pwszFileName, ushort wStreamNum)
        {
            try
            {
                IWMMetadataEditor MetadataEditor;
                IWMHeaderInfo3    HeaderInfo3;
                ushort            wAttributeCount = 0;

                WMFSDKFunctions.WMCreateEditor(out MetadataEditor);

                MetadataEditor.Open(pwszFileName);

                HeaderInfo3 = ( IWMHeaderInfo3 )MetadataEditor;

                HeaderInfo3.GetAttributeCountEx(wStreamNum, out wAttributeCount);

                PrintListHeader();

                for (ushort wAttribIndex = 0; wAttribIndex < wAttributeCount; wAttribIndex++)
                {
                    WMT_ATTR_DATATYPE wAttribType;
                    ushort            wLangIndex     = 0;
                    string            pwszAttribName = null;
                    byte[]            pbAttribValue  = null;
                    ushort            wAttribNameLen = 0;
                    uint dwAttribValueLen            = 0;

                    HeaderInfo3.GetAttributeByIndexEx(wStreamNum,
                                                      wAttribIndex,
                                                      pwszAttribName,
                                                      ref wAttribNameLen,
                                                      out wAttribType,
                                                      out wLangIndex,
                                                      pbAttribValue,
                                                      ref dwAttribValueLen);

                    pwszAttribName = new String(( char )0, wAttribNameLen);
                    pbAttribValue  = new byte[dwAttribValueLen];

                    HeaderInfo3.GetAttributeByIndexEx(wStreamNum,
                                                      wAttribIndex,
                                                      pwszAttribName,
                                                      ref wAttribNameLen,
                                                      out wAttribType,
                                                      out wLangIndex,
                                                      pbAttribValue,
                                                      ref dwAttribValueLen);

                    PrintAttribute(wAttribIndex, wStreamNum, pwszAttribName, wAttribType, 0, pbAttribValue, dwAttribValueLen);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        public void GetMetadata()
        {
            try
            {
                String filename   = @"E:\Music\Album Leaf\Vermillion .WMA";
                ushort wStreamNum = 0;

                IWMMetadataEditor MetadataEditor;
                IWMHeaderInfo3    HeaderInfo3;
                ushort            wAttributeCount;

                WMFSDKFunctions.WMCreateEditor(out MetadataEditor);

                MetadataEditor.Open(filename);

                HeaderInfo3 = (IWMHeaderInfo3)MetadataEditor;

                HeaderInfo3.GetAttributeCount(wStreamNum, out wAttributeCount);

                PrintListHeader();

                for (ushort wAttribIndex = 0; wAttribIndex < wAttributeCount; wAttribIndex++)
                {
                    WMT_ATTR_DATATYPE wAttribType;
                    string            pwszAttribName  = null;
                    byte[]            pbAttribValue   = null;
                    ushort            wAttribNameLen  = 0;
                    ushort            wAttribValueLen = 0;

                    HeaderInfo3.GetAttributeByIndex(wAttribIndex,
                                                    ref wStreamNum,
                                                    pwszAttribName,
                                                    ref wAttribNameLen,
                                                    out wAttribType,
                                                    pbAttribValue,
                                                    ref wAttribValueLen);

                    pbAttribValue  = new byte[wAttribValueLen];
                    pwszAttribName = new String((char)0, wAttribNameLen);

                    HeaderInfo3.GetAttributeByIndex(wAttribIndex,
                                                    ref wStreamNum,
                                                    pwszAttribName,
                                                    ref wAttribNameLen,
                                                    out wAttribType,
                                                    pbAttribValue,
                                                    ref wAttribValueLen);

                    PrintAttribute(wAttribIndex, wStreamNum, pwszAttribName, wAttribType, 0, pbAttribValue, wAttribValueLen);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        //------------------------------------------------------------------------------
        // Name: EditorOpenFile()
        // Desc: Creates a metadata editor and opens the file.
        //------------------------------------------------------------------------------
        bool EditorOpenFile(string pwszInFile, out IWMMetadataEditor ppEditor)
        {
            ppEditor = null;

            try
            {
                WMFSDKFunctions.WMCreateEditor(out ppEditor);

                ppEditor.Open(pwszInFile);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
        //------------------------------------------------------------------------------
        // Name: AddAttrib()
        // Desc: Add an attribute with the specifed language index.
        //------------------------------------------------------------------------------
        bool AddAttrib(string pwszFileName, ushort wStreamNum, string pwszAttribName,
                       ushort wAttribType, string pwszAttribValue, ushort wLangIndex)
        {
            try
            {
                IWMMetadataEditor MetadataEditor;
                IWMHeaderInfo3    HeaderInfo3;
                byte[]            pbAttribValue;
                int nAttribValueLen;
                WMT_ATTR_DATATYPE AttribDataType = ( WMT_ATTR_DATATYPE )wAttribType;
                ushort            wAttribIndex   = 0;

                if (!TranslateAttrib(AttribDataType, pwszAttribValue, out pbAttribValue, out nAttribValueLen))
                {
                    return(false);
                }

                WMFSDKFunctions.WMCreateEditor(out MetadataEditor);

                MetadataEditor.Open(pwszFileName);

                HeaderInfo3 = ( IWMHeaderInfo3 )MetadataEditor;

                HeaderInfo3.AddAttribute(wStreamNum,
                                         pwszAttribName,
                                         out wAttribIndex,
                                         AttribDataType,
                                         wLangIndex,
                                         pbAttribValue,
                                         (uint)nAttribValueLen);

                MetadataEditor.Flush();

                MetadataEditor.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 6
0
        public Metadata GetMetadata(object target)
        {
            if (target == null || !(target is string))
            {
                return(null);
            }

            string filename = target.ToString();

            IWMMetadataEditor2 metadataEditor = null;

            IWMSyncReader syncReader = null;

            WMMetadata metadata = new WMMetadata();

            try
            {
                WMFSDKFunctions.WMCreateEditor(out metadataEditor);
                metadataEditor.OpenEx(filename, FILE_ACCESS.GENERIC_READ, FILE_SHARE.FILE_SHARE_NONE);

                IWMHeaderInfo3 header = (IWMHeaderInfo3)metadataEditor;

                ushort attributeCount;
                header.GetAttributeCountEx(StreamNumber, out attributeCount);

                for (int i = 0; i < attributeCount; i++)
                {
                    MetadataField metadataField = GetAttributeByIndex(header, i);

                    metadata.AddMetadataField(metadataField);
                }
            }
            catch (COMException ex)
            {
                // TODO: Logging
            }
            finally
            {
                if (metadataEditor != null)
                {
                    metadataEditor.Close();
                    Marshal.FinalReleaseComObject(metadataEditor);
                    metadataEditor = null;
                }
            }

            try
            {
                WMFSDKFunctions.WMCreateSyncReader(IntPtr.Zero, WMT_RIGHTS.WMT_RIGHT_PLAYBACK, out syncReader);
                syncReader.Open(filename);

                int outputCount;
                syncReader.GetOutputCount(out outputCount);

                IWMOutputMediaProps outputMediaProps = null;

                for (uint i = 0; i < outputCount; i++)
                {
                    IWMOutputMediaProps innerOutputMediaProps;
                    syncReader.GetOutputProps(i, out innerOutputMediaProps);

                    Guid type;
                    innerOutputMediaProps.GetType(out type);

                    if (type == WMFSDKFunctions.WMMEDIATYPE_Video)
                    {
                        outputMediaProps = innerOutputMediaProps;
                        break;
                    }
                }

                if (outputMediaProps != null)
                {
                    int pcbType = 0;
                    outputMediaProps.GetMediaType(IntPtr.Zero, ref pcbType);

                    IntPtr mediaTypeBufferPtr = Marshal.AllocHGlobal(pcbType);

                    outputMediaProps.GetMediaType(mediaTypeBufferPtr, ref pcbType);

                    WM_MEDIA_TYPE     mediaType       = new WM_MEDIA_TYPE();
                    WMVIDEOINFOHEADER videoInfoHeader = new WMVIDEOINFOHEADER();
                    Marshal.PtrToStructure(mediaTypeBufferPtr, mediaType);

                    Marshal.FreeHGlobal(mediaTypeBufferPtr);
                    Marshal.PtrToStructure(mediaType.pbFormat, videoInfoHeader);

                    double frameRate = Math.Round((double)10000000 / videoInfoHeader.AvgTimePerFrame, 2);

                    metadata.AddMetadataField(new MetadataField("FrameRate", frameRate));
                }
            }
            catch (COMException ex)
            {
                // TODO: Logging
            }
            finally
            {
                if (syncReader != null)
                {
                    syncReader.Close();
                    Marshal.FinalReleaseComObject(syncReader);
                    syncReader = null;
                }
            }

            return(metadata);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Open an editor on the given file.
 /// </summary>
 /// <param name="fileName">The path to the file to open</param>
 /// <remarks>This will throw an exception if the format of the file is unknown.</remarks>
 public MetaDataEditor(string fileName)
 {
     WMFSDKFunctions.WMCreateEditor(out this.editor);
     this.headerInfo = (IWMHeaderInfo3)this.editor;
     this.editor.Open(fileName);
 }