private void TestInput() { int iCount, iFCount; IWMInputMediaProps pMP, pMP2; m_Writer.GetInputCount(out iCount); m_Writer.GetInputProps(0, out pMP); m_Writer.GetInputFormatCount(0, out iFCount); m_Writer.GetInputFormat(0, 0, out pMP2); m_Writer.SetInputProps(0, pMP); }
/// <summary> /// Create the writer indicating Metadata information /// </summary> /// <param name="output"><see cref="System.IO.Stream"/> Where resulting WMA string will be written</param> /// <param name="format">PCM format of input data received in <see cref="WmaWriter.Write"/> method</param> /// <param name="profile">IWMProfile that describe the resulting compressed stream</param> /// <param name="MetadataAttributes">Array of <see cref="Yeti.WMFSdk.WM_Attr"/> structures describing the metadata information that will be in the result stream</param> public WmaWriter(Stream output, WaveFormat format, IWMProfile profile, WM_Attr[] MetadataAttributes) : base(output, format) { m_Writer = WM.CreateWriter(); IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer; wa.AddSink((IWMWriterSink)this); m_Writer.SetProfile(profile); uint inputs; m_Writer.GetInputCount(out inputs); if (inputs == 1) { IWMInputMediaProps InpProps; Guid type; m_Writer.GetInputProps(0, out InpProps); InpProps.GetType(out type); if (type == MediaTypes.WMMEDIATYPE_Audio) { WM_MEDIA_TYPE mt; mt.majortype = MediaTypes.WMMEDIATYPE_Audio; mt.subtype = MediaTypes.WMMEDIASUBTYPE_PCM; mt.bFixedSizeSamples = true; mt.bTemporalCompression = false; mt.lSampleSize = (uint)m_InputDataFormat.nBlockAlign; mt.formattype = MediaTypes.WMFORMAT_WaveFormatEx; mt.pUnk = IntPtr.Zero; mt.cbFormat = (uint)Marshal.SizeOf(m_InputDataFormat); GCHandle h = GCHandle.Alloc(m_InputDataFormat, GCHandleType.Pinned); try { mt.pbFormat = h.AddrOfPinnedObject(); InpProps.SetMediaType(ref mt); } finally { h.Free(); } m_Writer.SetInputProps(0, InpProps); if ( MetadataAttributes != null ) { WMHeaderInfo info = new WMHeaderInfo((IWMHeaderInfo)m_Writer); foreach(WM_Attr attr in MetadataAttributes) { info.SetAttribute(attr); } info = null; } m_Writer.BeginWriting(); m_Profile = profile; } else { throw new ArgumentException("Invalid profile", "profile"); } } else { throw new ArgumentException("Invalid profile", "profile"); } }
/// <summary> /// Create the writer indicating Metadata information /// </summary> /// <param name="output"><see cref="System.IO.Stream"/> Where resulting WMA string will be written</param> /// <param name="format">PCM format of input data received in <see cref="WmaWriter.Write"/> method</param> /// <param name="profile">IWMProfile that describe the resulting compressed stream</param> /// <param name="MetadataAttributes">Array of <see cref="Yeti.WMFSdk.WM_Attr"/> structures describing the metadata information that will be in the result stream</param> public WmaWriter(Stream output, WaveFormat format, IWMProfile profile, WM_Attr[] MetadataAttributes) : base(output, format) { m_Writer = WM.CreateWriter(); IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer; wa.AddSink((IWMWriterSink)this); m_Writer.SetProfile(profile); uint inputs; m_Writer.GetInputCount(out inputs); if (inputs == 1) { IWMInputMediaProps InpProps; Guid type; m_Writer.GetInputProps(0, out InpProps); InpProps.GetType(out type); if (type == MediaTypes.WMMEDIATYPE_Audio) { WM_MEDIA_TYPE mt; mt.majortype = MediaTypes.WMMEDIATYPE_Audio; mt.subtype = MediaTypes.WMMEDIASUBTYPE_PCM; mt.bFixedSizeSamples = true; mt.bTemporalCompression = false; mt.lSampleSize = (uint)m_InputDataFormat.nBlockAlign; mt.formattype = MediaTypes.WMFORMAT_WaveFormatEx; mt.pUnk = IntPtr.Zero; mt.cbFormat = (uint)Marshal.SizeOf(m_InputDataFormat); GCHandle h = GCHandle.Alloc(m_InputDataFormat, GCHandleType.Pinned); try { mt.pbFormat = h.AddrOfPinnedObject(); InpProps.SetMediaType(ref mt); } finally { h.Free(); } m_Writer.SetInputProps(0, InpProps); if (MetadataAttributes != null) { WMHeaderInfo info = new WMHeaderInfo((IWMHeaderInfo)m_Writer); foreach (WM_Attr attr in MetadataAttributes) { info.SetAttribute(attr); } info = null; } m_Writer.BeginWriting(); m_Profile = profile; } else { throw new ArgumentException("Invalid profile", "profile"); } } else { throw new ArgumentException("Invalid profile", "profile"); } }
public WMAWriter(string path, WMAWriterSettings settings) { this.m_settings = settings; this.outputPath = path; try { m_pWriter = settings.GetWriter(); int cInputs; m_pWriter.GetInputCount(out cInputs); if (cInputs < 1) { throw new InvalidOperationException(); } IWMInputMediaProps pInput; m_pWriter.GetInputProps(0, out pInput); try { int cbType = 0; AMMediaType pMediaType = null; pInput.GetMediaType(pMediaType, ref cbType); pMediaType = new AMMediaType(); pMediaType.formatSize = cbType - Marshal.SizeOf(typeof(AMMediaType)); pInput.GetMediaType(pMediaType, ref cbType); try { var wfe = new WaveFormatExtensible(m_settings.PCM); Marshal.FreeCoTaskMem(pMediaType.formatPtr); pMediaType.formatPtr = IntPtr.Zero; pMediaType.formatSize = 0; pMediaType.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(wfe)); pMediaType.formatSize = Marshal.SizeOf(wfe); Marshal.StructureToPtr(wfe, pMediaType.formatPtr, false); pInput.SetMediaType(pMediaType); m_pWriter.SetInputProps(0, pInput); } finally { WMUtils.FreeWMMediaType(pMediaType); } } finally { Marshal.ReleaseComObject(pInput); } } catch (Exception ex) { if (m_pWriter != null) { Marshal.ReleaseComObject(m_pWriter); m_pWriter = null; } throw ex; } }
/// <summary> /// Configure audio media type. /// </summary> public bool ConfigAudio(_WMMediaType mt) { if (audioProps == null) { Debug.WriteLine("Failed to configure audio: properties is null."); return(false); } try { audioProps.SetMediaType(ref mt); writer.SetInputProps(audioInput, audioProps); } catch (Exception e) { eventLog.WriteEntry("Failed to set audio properties: " + e.ToString(), EventLogEntryType.Error, 1000); Debug.WriteLine("Failed to set audio properties: " + e.ToString()); return(false); } return(true); }
//------------------------------------------------------------------------------ // Name: CWMVCopy::CreateWriter() // Desc: Creates a writer and sets the profile and output of this writer. //------------------------------------------------------------------------------ protected void CreateWriter(string pwszOutputFile) { int dwInputCount = 0; Console.WriteLine("Creating the Writer..."); // // Create a writer // WMUtils.WMCreateWriter(IntPtr.Zero, out m_pWriter); // // Get the IWMWriterAdvanced interface of the writer // m_pWriterAdvanced = m_pWriter as IWMWriterAdvanced; // // Get the IWMHeaderInfo interface of the writer // m_pWriterHeaderInfo = m_pWriter as IWMHeaderInfo; // // Set the profile of the writer to the reader's profile // m_pWriter.SetProfile(m_pReaderProfile); m_pWriter.GetInputCount(out dwInputCount); // // Set the input property to null so the SDK knows we're going to // send compressed samples to the inputs of the writer. // for (int i = 0; i < dwInputCount; i++) { m_pWriter.SetInputProps(i, null); } // // Set the output file of the writer // m_pWriter.SetOutputFilename(pwszOutputFile); }
private void Initialize(Bitmap hBitmap) { AMMediaType mt = new AMMediaType(); VideoInfoHeader videoInfo = new VideoInfoHeader(); // Create the VideoInfoHeader using info from the bitmap videoInfo.BmiHeader.Size = Marshal.SizeOf(typeof(BitmapInfoHeader)); videoInfo.BmiHeader.Width = hBitmap.Width; videoInfo.BmiHeader.Height = hBitmap.Height; videoInfo.BmiHeader.Planes = 1; // compression thru clrimportant don't seem to be used. Init them anyway videoInfo.BmiHeader.Compression = 0; videoInfo.BmiHeader.ImageSize = 0; videoInfo.BmiHeader.XPelsPerMeter = 0; videoInfo.BmiHeader.YPelsPerMeter = 0; videoInfo.BmiHeader.ClrUsed = 0; videoInfo.BmiHeader.ClrImportant = 0; switch (hBitmap.PixelFormat) { case PixelFormat.Format32bppRgb: mt.subType = MediaSubType.RGB32; videoInfo.BmiHeader.BitCount = 32; break; case PixelFormat.Format24bppRgb: mt.subType = MediaSubType.RGB24; videoInfo.BmiHeader.BitCount = 24; break; case PixelFormat.Format16bppRgb555: mt.subType = MediaSubType.RGB555; videoInfo.BmiHeader.BitCount = 16; break; default: throw new Exception("Unrecognized Pixelformat in bitmap"); } videoInfo.SrcRect = new Rectangle(0, 0, hBitmap.Width, hBitmap.Height); videoInfo.TargetRect = videoInfo.SrcRect; videoInfo.BmiHeader.ImageSize = hBitmap.Width * hBitmap.Height * (videoInfo.BmiHeader.BitCount / 8); videoInfo.BitRate = videoInfo.BmiHeader.ImageSize * m_iFrameRate; videoInfo.BitErrorRate = 0; videoInfo.AvgTimePerFrame = 10000 * 1000 / m_iFrameRate; mt.majorType = MediaType.Video; mt.fixedSizeSamples = true; mt.temporalCompression = false; mt.sampleSize = videoInfo.BmiHeader.ImageSize; mt.formatType = FormatType.VideoInfo; mt.unkPtr = IntPtr.Zero; mt.formatSize = Marshal.SizeOf(typeof(VideoInfoHeader)); mt.formatPtr = Marshal.AllocCoTaskMem(mt.formatSize); Marshal.StructureToPtr(videoInfo, mt.formatPtr, false); IWMInputMediaProps pProps; m_Writer.GetInputProps(0, out pProps); pProps.SetMediaType(mt); // Now take the inputprops, and set them on the file writer m_Writer.SetInputProps(0, pProps); }
/// <summary> /// Read the properties of the first bitmap to finish initializing the writer. /// </summary> /// <param name="hBitmap">First bitmap</param> private void Initialize(Bitmap hBitmap) { AMMediaType mt = new AMMediaType(); VideoInfoHeader videoInfo = new VideoInfoHeader(); // Create the VideoInfoHeader using info from the bitmap videoInfo.BmiHeader.Size = Marshal.SizeOf(typeof(BitmapInfoHeader)); videoInfo.BmiHeader.Width = hBitmap.Width; videoInfo.BmiHeader.Height = hBitmap.Height; videoInfo.BmiHeader.Planes = 1; // compression thru clrimportant don't seem to be used. Init them anyway videoInfo.BmiHeader.Compression = 0; videoInfo.BmiHeader.ImageSize = 0; videoInfo.BmiHeader.XPelsPerMeter = 0; videoInfo.BmiHeader.YPelsPerMeter = 0; videoInfo.BmiHeader.ClrUsed = 0; videoInfo.BmiHeader.ClrImportant = 0; switch (hBitmap.PixelFormat) { case PixelFormat.Format32bppRgb: mt.subType = MediaSubType.RGB32; videoInfo.BmiHeader.BitCount = 32; break; case PixelFormat.Format24bppRgb: mt.subType = MediaSubType.RGB24; videoInfo.BmiHeader.BitCount = 24; break; case PixelFormat.Format16bppRgb555: mt.subType = MediaSubType.RGB555; videoInfo.BmiHeader.BitCount = 16; break; default: throw new Exception("Unrecognized Pixelformat in bitmap"); } videoInfo.SrcRect = new Rectangle(0, 0, hBitmap.Width, hBitmap.Height); videoInfo.TargetRect = videoInfo.SrcRect; videoInfo.BmiHeader.ImageSize = hBitmap.Width * hBitmap.Height * (videoInfo.BmiHeader.BitCount / 8); videoInfo.BitRate = videoInfo.BmiHeader.ImageSize * m_iFrameRate; videoInfo.BitErrorRate = 0; videoInfo.AvgTimePerFrame = 10000 * 1000 / m_iFrameRate; mt.majorType = MediaType.Video; mt.fixedSizeSamples = true; mt.temporalCompression = false; mt.sampleSize = videoInfo.BmiHeader.ImageSize; mt.formatType = FormatType.VideoInfo; mt.unkPtr = IntPtr.Zero; mt.formatSize = Marshal.SizeOf(typeof(VideoInfoHeader)); // Lock the videoInfo structure, and put the pointer // into the mediatype structure GCHandle gHan = GCHandle.Alloc(videoInfo, GCHandleType.Pinned); try { // Set the inputprops using the structures mt.formatPtr = gHan.AddrOfPinnedObject(); m_pInputProps.SetMediaType(mt); } finally { gHan.Free(); mt.formatPtr = IntPtr.Zero; } // Now take the inputprops, and set them on the file writer m_pWMWriter.SetInputProps(m_dwVideoInput, m_pInputProps); // Done with config, prepare to write m_pWMWriter.BeginWriting(); m_Init = true; }
public static void CopyWmv(string inputFilePath, string outputFilePath, ulong startTime, long duration) { IWMWriterAdvanced writerAdvanced = null; IWMProfile profile = null; IWMStreamConfig streamConfig = null; uint streamCount = 0; uint inputCount = 0; ushort[] streamNumbers = null; WMT_STREAM_SELECTION[] streamSelections = null; ulong sampleTime, sampleDuration; uint flags, outputNum; ushort streamNum; INSSBuffer sample = null; IWMSyncReader reader = Helpers.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); IWMWriter writer = Helpers.CreateWriter(); try { reader.Open(inputFilePath); Logger.WriteLogMessage("Opened file [" + inputFilePath + "] for reading."); profile = (IWMProfile)reader; profile.GetStreamCount(out streamCount); streamNumbers = new ushort[streamCount]; streamSelections = new WMT_STREAM_SELECTION[streamCount]; for (uint i = 0; i < streamCount; i++) { profile.GetStream(i, out streamConfig); streamConfig.GetStreamNumber(out streamNumbers[i]); streamSelections[i] = WMT_STREAM_SELECTION.WMT_ON; // // Read compressed samples // reader.SetReadStreamSamples(streamNumbers[i], true); } // // select all streams // reader.SetStreamsSelected((ushort)streamCount, streamNumbers, streamSelections); writer.SetProfile(profile); writer.GetInputCount(out inputCount); for (uint i = 0; i < inputCount; i++) { writer.SetInputProps(i, null); // write compressed samples } writer.SetOutputFilename(outputFilePath); Logger.WriteLogMessage("Set output filename [" + outputFilePath + "] for writing."); writerAdvanced = (IWMWriterAdvanced)writer; // Copy attributes avoided // Copy Codec Info avoided // Copy all scripts in the header avoided writer.BeginWriting(); // // startTime, duration are in 100-nsec ticks // reader.SetRange(startTime, duration); // seek Logger.WriteLogMessage("Set range on reader, startTime [" + startTime + "], duration [" + duration + "]."); for (uint streamsRead = 0; streamsRead < streamCount;) { try { streamNum = 0; reader.GetNextSample(0, out sample, out sampleTime, out sampleDuration, out flags, out outputNum, out streamNum); Logger.WriteLogMessage("Grabbed next video sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "]."); writerAdvanced.WriteStreamSample(streamNum, sampleTime, 0, sampleDuration, flags, sample); Logger.WriteLogMessage("Wrote sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "]."); } catch (COMException e) { if (e.ErrorCode == Constants.NS_E_NO_MORE_SAMPLES) { streamsRead++; } else { throw; } } } writer.EndWriting(); } finally { reader.Close(); } }
public bool FindVideoInputFormat(uint inputNum, Guid subtype, ref VideoInfoHeader inputVideoInfoHeader, bool enableCompressedSamples) { bool success = false; IWMInputMediaProps writerInputProps = null; WM_MEDIA_TYPE mediaType; uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(VideoInfoHeader))); uint formatCount; Logger.WriteLogMessage("Finding video input formats for writer, input [" + inputNum + "]."); _writer.GetInputFormatCount(inputNum, out formatCount); Logger.WriteLogMessage("Video writer can consume " + formatCount + " possible video input formats."); IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize); try { for (uint j = 0; j < formatCount; j++) { uint size = 0; try { _writer.GetInputFormat(inputNum, j, out writerInputProps); writerInputProps.GetMediaType(IntPtr.Zero, ref size); if (size > bufferSize) { bufferSize = size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem((int)bufferSize); } writerInputProps.GetMediaType(buffer, ref size); mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if (mediaType.formattype == FormatTypes.WMFORMAT_VideoInfo) { Logger.WriteLogMessage("Found video writer input format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "]."); inputVideoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.pbFormat, typeof(VideoInfoHeader)); Logger.WriteLogMessage("Found input video stream, width [" + inputVideoInfoHeader.bmiHeader.biWidth + "], height [" + inputVideoInfoHeader.bmiHeader.biHeight + "], bit count [" + inputVideoInfoHeader.bmiHeader.biBitCount + "], image size [" + inputVideoInfoHeader.bmiHeader.biSizeImage + "]."); if (mediaType.subtype == subtype) { writerInputProps.SetMediaType(ref mediaType); if (!enableCompressedSamples) { _writer.SetInputProps(inputNum, writerInputProps); } else { _writer.SetInputProps(inputNum, null); } success = true; break; } } } catch (Exception) { // error handle throw; } finally { Marshal.ReleaseComObject(writerInputProps); writerInputProps = null; } } } catch (Exception) { // error handle throw; } finally { Marshal.FreeCoTaskMem(buffer); } return(success); }