public void Initialize(IWMProfile profile, string filePath) { if (filePath == null || filePath.Length == 0) { throw new ArgumentNullException("filePath", "Invalid string parameter."); } // // assign profile to writer // _writer.SetProfile(profile); Logger.WriteLogMessage("Set profile on writer."); _writer.SetOutputFilename(filePath); Logger.WriteLogMessage("Set output filename [" + filePath + "] for writing."); _writer.GetInputCount(out _writerInputCount); Logger.WriteLogMessage("Found " + _writerInputCount + " writer inputs."); profile.GetStreamCount(out _writerStreamCount); Logger.WriteLogMessage("Found " + _writerStreamCount + " writer streams."); }
/// <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"); } }
private void TestProfile() { // Initialize all member variables IWMProfile pWMProfile = CreateProfile(); m_Writer.SetProfile(pWMProfile); m_Writer.SetProfileByID(g); }
//------------------------------------------------------------------------------ // 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); }
/// <summary> /// Create filename from specified profile using specified framerate /// </summary> /// <param name="lpszFileName">File name to create</param> /// <param name="guidProfileID">WM Profile to use for compression</param> /// <param name="iFrameRate">Frames Per Second</param> public CwmvFile(string lpszFileName, ref Guid guidProfileID, int iFrameRate) { Guid guidInputType; int dwInputCount; IWMProfileManager pWMProfileManager = null; IWMProfile pWMProfile = null; // Initialize all member variables m_iFrameRate = iFrameRate; m_dwVideoInput = -1; m_dwCurrentVideoSample = 0; m_msVideoTime = 0; m_pWMWriter = null; m_pInputProps = null; m_Init = false; try { // Open the profile manager WMUtils.WMCreateProfileManager(out pWMProfileManager); // Convert pWMProfileManager to a IWMProfileManager2 IWMProfileManager2 pProfileManager2 = (IWMProfileManager2)pWMProfileManager; // Specify the version number of the profiles to use pProfileManager2.SetSystemProfileVersion(WMVersion.V8_0); // Load the profile specified by the caller pProfileManager2.LoadProfileByID(guidProfileID, out pWMProfile); // Create a writer. This is the interface we actually write with WMUtils.WMCreateWriter(IntPtr.Zero, out m_pWMWriter); // Set the profile we got into the writer. This controls compression, video // size, # of video channels, # of audio channels, etc m_pWMWriter.SetProfile(pWMProfile); // Find out how many inputs are in the current profile m_pWMWriter.GetInputCount(out dwInputCount); // Assume we won't find any video pins m_dwVideoInput = -1; // Find the first video input on the writer for (int i = 0; i < dwInputCount; i++) { // Get the properties of channel #i m_pWMWriter.GetInputProps(i, out m_pInputProps); // Read the type of the channel m_pInputProps.GetType(out guidInputType); // If it is video, we are done if (guidInputType == MediaType.Video) { m_dwVideoInput = i; break; } } // Didn't find a video channel if (m_dwVideoInput == -1) { throw new Exception("Profile does not accept video input"); } // Specify the file name for the output m_pWMWriter.SetOutputFilename(lpszFileName); } catch { Close(); throw; } finally { // Release the locals if (pWMProfile != null) { Marshal.ReleaseComObject(pWMProfile); pWMProfile = null; } if (pWMProfileManager != null) { Marshal.ReleaseComObject(pWMProfileManager); pWMProfileManager = null; } } }
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(); } }
/// <summary> /// Load a WM Profile (system or custom). /// </summary> /// <param name="prxFile"></param> /// <param name="prIndex"></param> /// <returns></returns> public bool ConfigProfile(String prxFile, uint prIndex) { IWMProfile profile; uint hr = WMFSDKFunctions.WMCreateProfileManager(out profileManager); if (prxFile == "") { //use system profile Guid prg = ProfileIndexToGuid(prIndex); if (prg == Guid.Empty) { profile = null; Debug.WriteLine("Unsupported Profile index."); return(false); } try { GUID prG = WMGuids.ToGUID(prg); profileManager.LoadProfileByID(ref prG, out profile); } catch (Exception e) { eventLog.WriteEntry("Failed to load system profile: " + e.ToString(), EventLogEntryType.Error, 1000); Debug.WriteLine("Failed to load system profile: " + e.ToString()); profile = null; return(false); } } else { //use custom profile profile = LoadCustomProfile(prxFile); if (profile == null) { return(false); } } /// Tell the writer to use this profile. try { writer.SetProfile(profile); string name = GetProfileName(profile); Debug.WriteLine("Using profile: " + name); } catch (Exception e) { eventLog.WriteEntry("Failed to set writer profile: " + e.ToString(), EventLogEntryType.Error, 1000); Debug.WriteLine("Failed to set writer profile: " + e.ToString()); profile = null; return(false); } /// A slightly confusing point: Streams are subobjects of the profile, /// while inputs are subobjects of the Writer. The difference is in the /// multi-bitrate scenario where there may be multiple streams per input. /// Stream numbers start with 1, while input numbers and stream indexes begin at 0. /// If we have a profile that supports scripts, we need the stream number of /// the script stream. For audio and video, we just need input number. scriptBitrate = 0; audioInput = videoInput = 0; scriptStreamNumber = 0; audioProps = videoProps = null; /// If the profile has a script stream, find the bitrate and stream number. uint cStreams; IWMStreamConfig streamConfig; GUID streamType; profile.GetStreamCount(out cStreams); for (uint i = 0; i < cStreams; i++) { profile.GetStream(i, out streamConfig); streamConfig.GetStreamType(out streamType); if (WMGuids.ToGuid(streamType) == WMGuids.WMMEDIATYPE_Script) { streamConfig.GetStreamNumber(out scriptStreamNumber); streamConfig.GetBitrate(out scriptBitrate); } } /// Iterate over writer inputs, holding on to the IWMInputMediaProps* for each, /// so we can later configure them. Also save input numbers for audio and video here. uint cInputs; writer.GetInputCount(out cInputs); GUID guidInputType; IWMInputMediaProps inputProps = null; for (uint i = 0; i < cInputs; i++) { writer.GetInputProps(i, out inputProps); inputProps.GetType(out guidInputType); if (WMGuids.ToGuid(guidInputType) == WMGuids.WMMEDIATYPE_Audio) { audioProps = inputProps; audioInput = i; } else if (WMGuids.ToGuid(guidInputType) == WMGuids.WMMEDIATYPE_Video) { videoProps = inputProps; videoInput = i; } else if (WMGuids.ToGuid(guidInputType) == WMGuids.WMMEDIATYPE_Script) { } else { Debug.WriteLine("Profile contains unrecognized media type."); return(false); } } // We require an audio input, since that drives the timing for the whole stream. if (audioProps == null) { Debug.WriteLine("Profile should contain at least one audio input."); return(false); } return(true); }