private void TestAttr() { short w1, w2, w3; short iIndex; const string v = "ItzaString"; m_head.GetAttributeCountEx(0, out w1); byte[] b = Encoding.Unicode.GetBytes(v); m_head.AddAttribute(0, "asdf", out iIndex, AttrDataType.STRING, 0, b, b.Length); m_head.GetAttributeCountEx(0, out w2); Debug.Assert(w2 == w1 + 1); short pNLen = 0; int pDLen = 0; AttrDataType pType; short pLang; m_head.GetAttributeByIndexEx(0, w1, null, ref pNLen, out pType, out pLang, null, ref pDLen); byte[] bb = new byte[pDLen]; m_head.GetAttributeByIndexEx(0, w1, null, ref pNLen, out pType, out pLang, bb, ref pDLen); string ss = Encoding.Unicode.GetString(bb, 0, pDLen - 2); StringBuilder sbName = new StringBuilder(pNLen); byte[] bb2 = new byte[pDLen]; m_head.GetAttributeByIndexEx(0, w1, sbName, ref pNLen, out pType, out pLang, bb2, ref pDLen); Debug.Assert(sbName.ToString() == "asdf" && pType == AttrDataType.STRING && pLang == 0 && v == Encoding.Unicode.GetString(bb2, 0, v.Length * 2)); byte[] wm3 = BitConverter.GetBytes(4144);; m_head.ModifyAttribute(0, w1, AttrDataType.DWORD, 0, wm3, 4); byte[] bb4 = new byte[pDLen]; m_head.GetAttributeByIndexEx(0, w1, null, ref pNLen, out pType, out pLang, bb4, ref pDLen); Debug.Assert(BitConverter.ToInt32(bb4, 0) == 4144); m_head.DeleteAttribute(0, w1); m_head.GetAttributeCountEx(0, out w3); Debug.Assert(w3 == w1); }
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); }