示例#1
0
        public void SetSelectedStreams(uint readerOutputCount, ushort[] readerStreamNumbers, WMT_STREAM_SELECTION[] readerStreamSelections)
        {
            Logger.WriteLogMessage("Selecting [" + readerOutputCount + "] streams on reader.");

            for (uint i = 0; i < readerOutputCount; i++)
            {
                Logger.WriteLogMessage("- stream [" + readerStreamNumbers[i] + "], selection [" + readerStreamSelections[i] + "].");
            }

            //
            // select all streams
            //
            _reader.SetStreamsSelected((ushort)readerOutputCount, readerStreamNumbers, readerStreamSelections);

            Logger.WriteLogMessage("Set selected streams on reader.");
        }
示例#2
0
文件: Helpers.cs 项目: tdhieu/openvss
		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();
			}
		}
示例#3
0
        private void Init(WaveFormat OutputFormat)
        {
            m_outputNumber = GetAudioOutputNumber(m_reader);
            if (m_outputNumber == InvalidOuput)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            int[] FormatIndexes = GetPCMOutputNumbers(m_reader, (uint)m_outputNumber);
            if (FormatIndexes.Length == 0)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            if (OutputFormat != null)
            {
                m_outputFormatNumber = -1;
                for (int i = 0; i < FormatIndexes.Length; i++)
                {
                    WaveFormat fmt = GetOutputFormat(m_reader, (uint)m_outputNumber, (uint)FormatIndexes[i]);
                    if (// (fmt.wFormatTag == OutputFormat.wFormatTag) &&
                        (fmt.AverageBytesPerSecond == OutputFormat.AverageBytesPerSecond) &&
                        (fmt.BlockAlign == OutputFormat.BlockAlign) &&
                        (fmt.Channels == OutputFormat.Channels) &&
                        (fmt.SampleRate == OutputFormat.SampleRate) &&
                        (fmt.BitsPerSample == OutputFormat.BitsPerSample))
                    {
                        m_outputFormatNumber = FormatIndexes[i];
                        m_outputFormat       = fmt;
                        break;
                    }
                }
                if (m_outputFormatNumber < 0)
                {
                    throw new ArgumentException("No PCM output found");
                }
            }
            else
            {
                m_outputFormatNumber = FormatIndexes[0];
                m_outputFormat       = GetOutputFormat(m_reader, (uint)m_outputNumber, (uint)FormatIndexes[0]);
            }
            uint OutputCtns = 0;

            m_reader.GetOutputCount(out OutputCtns);
            ushort[] StreamNumbers = new ushort[OutputCtns];
            WMT_STREAM_SELECTION[] StreamSelections = new WMT_STREAM_SELECTION[OutputCtns];
            for (uint i = 0; i < OutputCtns; i++)
            {
                m_reader.GetStreamNumberForOutput(i, out StreamNumbers[i]);
                if (i == m_outputNumber)
                {
                    StreamSelections[i] = WMT_STREAM_SELECTION.WMT_ON;
                    m_outputStream      = StreamNumbers[i];
                    m_reader.SetReadStreamSamples(m_outputStream, false);
                }
                else
                {
                    StreamSelections[i] = WMT_STREAM_SELECTION.WMT_OFF;
                }
            }
            m_reader.SetStreamsSelected((ushort)OutputCtns, StreamNumbers, StreamSelections);
            IWMOutputMediaProps Props = null;

            m_reader.GetOutputFormat((uint)m_outputNumber, (uint)m_outputFormatNumber, out Props);
            m_reader.SetOutputProps((uint)m_outputNumber, Props);

            int size = 0;

            Props.GetMediaType(IntPtr.Zero, ref size);
            IntPtr buffer = Marshal.AllocCoTaskMem(size);

            try
            {
                WM_MEDIA_TYPE mt;
                Props.GetMediaType(buffer, ref size);
                mt           = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));
                m_sampleSize = mt.lSampleSize;
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
                Props = null;
            }

            m_seekable = false;
            m_length   = -1;
            WMHeaderInfo head = new WMHeaderInfo(HeaderInfo);

            try
            {
                m_seekable = (bool)head[WM.g_wszWMSeekable];
                // Yuval Naveh
                ulong nanoDuration = (ulong)head[WM.g_wszWMDuration];
                m_duration = new TimeSpan((long)nanoDuration);
                m_length   = SampleTime2BytePosition(nanoDuration);
            }
            catch (COMException e)
            {
                if (e.ErrorCode != WM.ASF_E_NOTFOUND)
                {
                    throw (e);
                }
            }
        }
示例#4
0
文件: Helpers.cs 项目: tmpkus/openvss
        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();
            }
        }
示例#5
0
        private void Init(WaveFormat OutputFormat)
        {
            m_outputNumber = GetAudioOutputNumber(m_reader);
            if (m_outputNumber == InvalidOuput)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            int[] FormatIndexes = GetPCMOutputNumbers(m_reader, (uint)m_outputNumber);
            if (FormatIndexes.Length == 0)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            if (OutputFormat != null)
            {
                m_outputFormatNumber = -1;
                for (int i = 0; i < FormatIndexes.Length; i++)
                {
                    WaveFormat fmt = GetOutputFormat(m_reader, (uint)m_outputNumber, (uint)FormatIndexes[i]);
                    if (// (fmt.wFormatTag == OutputFormat.wFormatTag) &&
                      (fmt.AverageBytesPerSecond == OutputFormat.AverageBytesPerSecond) &&
                      (fmt.BlockAlign == OutputFormat.BlockAlign) &&
                      (fmt.Channels == OutputFormat.Channels) &&
                      (fmt.SampleRate == OutputFormat.SampleRate) &&
                      (fmt.BitsPerSample == OutputFormat.BitsPerSample))
                    {
                        m_outputFormatNumber = FormatIndexes[i];
                        m_outputFormat = fmt;
                        break;
                    }
                }
                if (m_outputFormatNumber < 0)
                {
                    throw new ArgumentException("No PCM output found");
                }
            }
            else
            {
                m_outputFormatNumber = FormatIndexes[0];
                m_outputFormat = GetOutputFormat(m_reader, (uint)m_outputNumber, (uint)FormatIndexes[0]);
            }
            uint OutputCtns = 0;
            m_reader.GetOutputCount(out OutputCtns);
            ushort[] StreamNumbers = new ushort[OutputCtns];
            WMT_STREAM_SELECTION[] StreamSelections = new WMT_STREAM_SELECTION[OutputCtns];
            for (uint i = 0; i < OutputCtns; i++)
            {
                m_reader.GetStreamNumberForOutput(i, out StreamNumbers[i]);
                if (i == m_outputNumber)
                {
                    StreamSelections[i] = WMT_STREAM_SELECTION.WMT_ON;
                    m_outputStream = StreamNumbers[i];
                    m_reader.SetReadStreamSamples(m_outputStream, false);
                }
                else
                {
                    StreamSelections[i] = WMT_STREAM_SELECTION.WMT_OFF;
                }
            }
            m_reader.SetStreamsSelected((ushort)OutputCtns, StreamNumbers, StreamSelections);
            IWMOutputMediaProps Props = null;
            m_reader.GetOutputFormat((uint)m_outputNumber, (uint)m_outputFormatNumber, out Props);
            m_reader.SetOutputProps((uint)m_outputNumber, Props);

            int size = 0;
            Props.GetMediaType(IntPtr.Zero, ref size);
            IntPtr buffer = Marshal.AllocCoTaskMem(size);
            try
            {
                WM_MEDIA_TYPE mt;
                Props.GetMediaType(buffer, ref size);
                mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));
                m_sampleSize = mt.lSampleSize;
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
                Props = null;
            }

            m_seekable = false;
            m_length = -1;
            WMHeaderInfo head = new WMHeaderInfo(HeaderInfo);
            try
            {
                m_seekable = (bool)head[WM.g_wszWMSeekable];
                // Yuval Naveh
                ulong nanoDuration = (ulong)head[WM.g_wszWMDuration];
                m_duration = new TimeSpan((long)nanoDuration);
                m_length = SampleTime2BytePosition(nanoDuration);
            }
            catch (COMException e)
            {
                if (e.ErrorCode != WM.ASF_E_NOTFOUND)
                {
                    throw (e);
                }
            }

        }
示例#6
0
        private void Init(WaveFormat outputFormat)
        {
            m_OutputNumber = GetAudioOutputNumber(m_Reader);
            if (m_OutputNumber == InvalidOuput)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            int[] formatIndexes = GetPCMOutputNumbers(m_Reader, (uint)m_OutputNumber);
            if (formatIndexes.Length == 0)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            if (outputFormat != null)
            {
                m_OutputFormatNumber = -1;
                for (int i = 0; i < formatIndexes.Length; i++)
                {
                    WaveFormat fmt = GetOutputFormat(m_Reader, (uint)m_OutputNumber, (uint)formatIndexes[i]);
                    if ((fmt.wFormatTag == outputFormat.wFormatTag) &&
                      (fmt.nAvgBytesPerSec == outputFormat.nAvgBytesPerSec) &&
                      (fmt.nBlockAlign == outputFormat.nBlockAlign) &&
                      (fmt.nChannels == outputFormat.nChannels) &&
                      (fmt.nSamplesPerSec == outputFormat.nSamplesPerSec) &&
                      (fmt.wBitsPerSample == outputFormat.wBitsPerSample))
                    {
                        m_OutputFormatNumber = formatIndexes[i];
                        m_OutputFormat = fmt;
                        break;
                    }
                }
                if (m_OutputFormatNumber < 0)
                {
                    throw new ArgumentException("No PCM output found");
                }
            }
            else
            {
                m_OutputFormatNumber = formatIndexes[0];
                m_OutputFormat = GetOutputFormat(m_Reader, (uint)m_OutputNumber, (uint)formatIndexes[0]);
            }
            uint outputCtns = 0;
            m_Reader.GetOutputCount(out outputCtns);
            var streamNumbers = new ushort[outputCtns];
            var streamSelections = new WMT_STREAM_SELECTION[outputCtns];
            for (uint i = 0; i < outputCtns; i++)
            {
                m_Reader.GetStreamNumberForOutput(i, out streamNumbers[i]);
                if (i == m_OutputNumber)
                {
                    streamSelections[i] = WMT_STREAM_SELECTION.WMT_ON;
                    m_OuputStream = streamNumbers[i];
                    m_Reader.SetReadStreamSamples(m_OuputStream, false);
                }
                else
                {
                    streamSelections[i] = WMT_STREAM_SELECTION.WMT_OFF;
                }
            }
            m_Reader.SetStreamsSelected((ushort)outputCtns, streamNumbers, streamSelections);
            IWMOutputMediaProps props = null;
            m_Reader.GetOutputFormat((uint)m_OutputNumber, (uint)m_OutputFormatNumber, out props);
            m_Reader.SetOutputProps((uint)m_OutputNumber, props);

            uint size = 0;
            props.GetMediaType(IntPtr.Zero, ref size);
            IntPtr buffer = Marshal.AllocCoTaskMem((int)size);
            try
            {
                props.GetMediaType(buffer, ref size);
                var mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));
                m_SampleSize = mt.lSampleSize;
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
                props = null;
            }

            m_Seekable = false;
            m_Length = -1;
            var head = new WMHeaderInfo(HeaderInfo);
            try
            {
                m_Seekable = (bool)head[WM.g_wszWMSeekable];
                m_Length = SampleTime2BytePosition((ulong)head[WM.g_wszWMDuration]);
            }
            catch (COMException e)
            {
                if (e.ErrorCode != WM.ASF_E_NOTFOUND)
                {
                    throw;
                }
            }
        }
示例#7
0
        private void Init(WaveFormat outputFormat)
        {
            m_OutputNumber = GetAudioOutputNumber(m_Reader);
            if (m_OutputNumber == InvalidOuput)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            int[] formatIndexes = GetPCMOutputNumbers(m_Reader, (uint)m_OutputNumber);
            if (formatIndexes.Length == 0)
            {
                throw new ArgumentException("An audio stream was not found");
            }
            if (outputFormat != null)
            {
                m_OutputFormatNumber = -1;
                for (int i = 0; i < formatIndexes.Length; i++)
                {
                    WaveFormat fmt = GetOutputFormat(m_Reader, (uint)m_OutputNumber, (uint)formatIndexes[i]);
                    if ((fmt.wFormatTag == outputFormat.wFormatTag) &&
                        (fmt.nAvgBytesPerSec == outputFormat.nAvgBytesPerSec) &&
                        (fmt.nBlockAlign == outputFormat.nBlockAlign) &&
                        (fmt.nChannels == outputFormat.nChannels) &&
                        (fmt.nSamplesPerSec == outputFormat.nSamplesPerSec) &&
                        (fmt.wBitsPerSample == outputFormat.wBitsPerSample))
                    {
                        m_OutputFormatNumber = formatIndexes[i];
                        m_OutputFormat       = fmt;
                        break;
                    }
                }
                if (m_OutputFormatNumber < 0)
                {
                    throw new ArgumentException("No PCM output found");
                }
            }
            else
            {
                m_OutputFormatNumber = formatIndexes[0];
                m_OutputFormat       = GetOutputFormat(m_Reader, (uint)m_OutputNumber, (uint)formatIndexes[0]);
            }
            uint outputCtns = 0;

            m_Reader.GetOutputCount(out outputCtns);
            var streamNumbers    = new ushort[outputCtns];
            var streamSelections = new WMT_STREAM_SELECTION[outputCtns];

            for (uint i = 0; i < outputCtns; i++)
            {
                m_Reader.GetStreamNumberForOutput(i, out streamNumbers[i]);
                if (i == m_OutputNumber)
                {
                    streamSelections[i] = WMT_STREAM_SELECTION.WMT_ON;
                    m_OuputStream       = streamNumbers[i];
                    m_Reader.SetReadStreamSamples(m_OuputStream, false);
                }
                else
                {
                    streamSelections[i] = WMT_STREAM_SELECTION.WMT_OFF;
                }
            }
            m_Reader.SetStreamsSelected((ushort)outputCtns, streamNumbers, streamSelections);
            IWMOutputMediaProps props = null;

            m_Reader.GetOutputFormat((uint)m_OutputNumber, (uint)m_OutputFormatNumber, out props);
            m_Reader.SetOutputProps((uint)m_OutputNumber, props);

            uint size = 0;

            props.GetMediaType(IntPtr.Zero, ref size);
            IntPtr buffer = Marshal.AllocCoTaskMem((int)size);

            try
            {
                props.GetMediaType(buffer, ref size);
                var mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));
                m_SampleSize = mt.lSampleSize;
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
                props = null;
            }

            m_Seekable = false;
            m_Length   = -1;
            var head = new WMHeaderInfo(HeaderInfo);

            try
            {
                m_Seekable = (bool)head[WM.g_wszWMSeekable];
                m_Length   = SampleTime2BytePosition((ulong)head[WM.g_wszWMDuration]);
            }
            catch (COMException e)
            {
                if (e.ErrorCode != WM.ASF_E_NOTFOUND)
                {
                    throw;
                }
            }
        }