public void Process() { using (MFSystem.Start()) { var readWriteFactory = new ReadWriteClassFactory(); var attributes = new Attributes { ReadWriterEnableHardwareTransforms = true, SourceReaderEnableVideoProcessing = true, }; var destAttributes = new Attributes { ReadWriterEnableHardwareTransforms = true, SourceReaderEnableVideoProcessing = true, MaxKeyFrameSpacing = 3000 }; var shortSourceReader = readWriteFactory.CreateSourceReaderFromURL(shortSourceFile, attributes); var shortSourceReader2 = readWriteFactory.CreateSourceReaderFromURL(shortSourceFile, attributes); var mainSourceReader = readWriteFactory.CreateSourceReaderFromURL(mainSourceFile, attributes); var sinkWriter = readWriteFactory.CreateSinkWriterFromURL(destinationFile, destAttributes); var writeToSink = ConnectStreams(shortSourceReader, shortSourceReader2, mainSourceReader, sinkWriter); ProcessSample progress = sample => { if(!sample.Flags.EndOfStream) Trace.WriteLine(string.Format("sourceTime: {0}, sampleTime: {1}", sample.Timestamp.FromNanoToSeconds(), sample.SampleTime.FromNanoToSeconds())); return writeToSink(sample); }; var fadeSegments = AVOperations.FadeIn(AVOperations.FadeOut(progress)); var editoutMiddle = AVOperations.Cut(9.FromSecondsToNano(), 19.FromSecondsToNano(), fadeSegments); var overlay = AVOperations.Overlay(applyOverlay, editoutMiddle); var first4Seconds = AVOperations.Cut(0.FromSecondsToNano(), 4.FromSecondsToNano(), progress, progress); var readers = AVOperations.Combine(new[] { shortSourceReader, mainSourceReader }, 1.1); using (sinkWriter.BeginWriting()) { AVOperations.StartConcat(readers, overlay, AVOperations.Concat(shortSourceReader, first4Seconds, () => false), () => false); } } }
public static VideoDetails For(string videoFileName) { List<int> supportedAudioBitRates = new List<int>(); using (MFSystem.Start()) { var readWriteFactory = new ReadWriteClassFactory(); var sourceReader = readWriteFactory.CreateSourceReaderFromURL(videoFileName, null); var videoStream = sourceReader.Streams.First(s => s.IsSelected && s.NativeMediaType.IsVideo); var audioStream = sourceReader.Streams.First(s => s.IsSelected && s.NativeMediaType.IsAudio); var channels = audioStream.NativeMediaType.AudioNumberOfChannels; var sampleRate = audioStream.NativeMediaType.AudioSamplesPerSecond; var types = MFSystem.TranscodeGetAudioOutputAvailableTypes(MediaFoundation.MFMediaType.WMAudioV9, MediaFoundation.Transform.MFT_EnumFlag.All); foreach (var bitRate in types .Where(t => t.AudioNumberOfChannels == channels && t.AudioSamplesPerSecond == sampleRate) .Select(t => t.AudioAverageBytesPerSecond) .Distinct() .OrderBy(t => t)) { supportedAudioBitRates.Add(bitRate * 8); } int videoBitRate = 0; videoStream.NativeMediaType.TryGetBitRate(out videoBitRate); return new VideoDetails(supportedAudioBitRates.ToArray(), videoStream.NativeMediaType.FrameRate.ToInt(), videoStream.NativeMediaType.FrameSize, videoBitRate / 1000000); } }
public SourceReaderExtra CreateSourceReader(ReadWriteClassFactory readWriteFactory, Attributes attributes) { TraceDebug.WriteLine("Attempting to open file {0}".F(this.FileName)); var reader = readWriteFactory.CreateSourceReaderFromURL(this.FileName, attributes); TraceDebug.WriteLine("Opened file {0}. Duration: {1}".F(this.FileName, reader.Duration.FromNanoToSeconds())); return new SourceReaderExtra(this.FileName, this.State, reader); }
public SourceReaderExtra CreateSourceReader(ReadWriteClassFactory readWriteFactory, Attributes attributes) { TraceDebug.WriteLine("Attempting to open file {0}".F(this.FileName)); var reader = readWriteFactory.CreateSourceReaderFromURL(this.FileName, attributes); TraceDebug.WriteLine("Opened file {0}. Duration: {1}".F(this.FileName, reader.Duration.FromNanoToSeconds())); return(new SourceReaderExtra(this.FileName, this.State, reader)); }
public void Process() { using (MFSystem.Start()) { var readWriteFactory = new ReadWriteClassFactory(); var attributes = new Attributes { ReadWriterEnableHardwareTransforms = true, SourceReaderEnableVideoProcessing = true, }; var destAttributes = new Attributes { ReadWriterEnableHardwareTransforms = true, SourceReaderEnableVideoProcessing = true, MaxKeyFrameSpacing = 3000 }; var sourceReader = readWriteFactory.CreateSourceReaderFromURL(sourceFile, attributes); var sinkWriter = readWriteFactory.CreateSinkWriterFromURL(destinationFile, destAttributes); var writeToSink = ConnectStreams(sourceReader, sinkWriter); using (sinkWriter.BeginWriting()) { sourceReader.Samples(sample => { Console.Clear(); Console.WriteLine(TimeSpan.FromSeconds(sample.Timestamp.FromNanoToSeconds())); return writeToSink(sample); }); } } }
public static VideoDetails TestFor(string videoFileName) { List<int> supportedAudioBitRates = new List<int>(); using (MFSystem.Start()) { var readWriteFactory = new ReadWriteClassFactory(); var sourceReader = readWriteFactory.CreateSourceReaderFromURL(videoFileName, null); var videoStream = sourceReader.Streams.FirstOrDefault(s => s.IsSelected && s.NativeMediaType.IsVideo); if (videoStream.IsNull) throw new Exception("Unable to find video track within file."); var audioStream = sourceReader.Streams.FirstOrDefault(s => s.IsSelected && s.NativeMediaType.IsAudio); if (audioStream.IsNull) throw new Exception("Unable to find audio track within file."); var channels = audioStream.NativeMediaType.AudioNumberOfChannels; var sampleRate = audioStream.NativeMediaType.AudioSamplesPerSecond; var types = MFSystem.TranscodeGetAudioOutputAvailableTypes(MediaFoundation.MFMediaType.WMAudioV9, MediaFoundation.Transform.MFT_EnumFlag.All); foreach (var bitRate in types .Where(t => t.AudioSamplesPerSecond == sampleRate) .Select(t => t.AudioAverageBytesPerSecond) .Distinct() .OrderBy(t => t)) { supportedAudioBitRates.Add(bitRate); } int videoBitRate = 0; videoStream.NativeMediaType.TryGetBitRate(out videoBitRate); var audioSamplesPerSecond = audioStream.NativeMediaType.AudioSamplesPerSecond; var audioAverageBytesPerSecond = audioStream.NativeMediaType.AudioAverageBytesPerSecond; var audioSubTypeName = audioStream.CurrentMediaType.SubTypeName; var videoSubTypeName = videoStream.CurrentMediaType.SubTypeName; var transcoder = new Transcoder { VideoFiles = new[] { new SourceReaderExtra(videoFileName, null) }, DestinationFile = Path.ChangeExtension(videoFileName, "wmv"), VideoBitRate = 5000000 }; var errorMessage = transcoder.TestVideoConversion(); return new VideoDetails( supportedAudioBitRates.ToArray(), videoStream.NativeMediaType.FrameRate.ToInt(), videoStream.NativeMediaType.FrameSize, videoBitRate / 1000000, audioSamplesPerSecond, audioAverageBytesPerSecond, videoSubTypeName, audioSubTypeName, transcoder, errorMessage); } }
public static VideoDetails TestFor(string videoFileName) { List <int> supportedAudioBitRates = new List <int>(); using (MFSystem.Start()) { var readWriteFactory = new ReadWriteClassFactory(); var sourceReader = readWriteFactory.CreateSourceReaderFromURL(videoFileName, null); var videoStream = sourceReader.Streams.FirstOrDefault(s => s.IsSelected && s.NativeMediaType.IsVideo); if (videoStream.IsNull) { throw new Exception("Unable to find video track within file."); } var audioStream = sourceReader.Streams.FirstOrDefault(s => s.IsSelected && s.NativeMediaType.IsAudio); if (audioStream.IsNull) { throw new Exception("Unable to find audio track within file."); } var channels = audioStream.NativeMediaType.AudioNumberOfChannels; var sampleRate = audioStream.NativeMediaType.AudioSamplesPerSecond; var types = MFSystem.TranscodeGetAudioOutputAvailableTypes(MediaFoundation.MFMediaType.WMAudioV9, MediaFoundation.Transform.MFT_EnumFlag.All); foreach (var bitRate in types .Where(t => t.AudioSamplesPerSecond == sampleRate) .Select(t => t.AudioAverageBytesPerSecond) .Distinct() .OrderBy(t => t)) { supportedAudioBitRates.Add(bitRate); } int videoBitRate = 0; videoStream.NativeMediaType.TryGetBitRate(out videoBitRate); var audioSamplesPerSecond = audioStream.NativeMediaType.AudioSamplesPerSecond; var audioAverageBytesPerSecond = audioStream.NativeMediaType.AudioAverageBytesPerSecond; var audioSubTypeName = audioStream.CurrentMediaType.SubTypeName; var videoSubTypeName = videoStream.CurrentMediaType.SubTypeName; var transcoder = new Transcoder { VideoFiles = new[] { new SourceReaderExtra(videoFileName, null) }, DestinationFile = Path.ChangeExtension(videoFileName, "wmv"), VideoBitRate = 5000000 }; var errorMessage = transcoder.TestVideoConversion(); return(new VideoDetails( supportedAudioBitRates.ToArray(), videoStream.NativeMediaType.FrameRate.ToInt(), videoStream.NativeMediaType.FrameSize, videoBitRate / 1000000, audioSamplesPerSecond, audioAverageBytesPerSecond, videoSubTypeName, audioSubTypeName, transcoder, errorMessage)); } }
public SourceReaderExtra CreateSourceReader(ReadWriteClassFactory readWriteFactory, Attributes attributes) { var reader = readWriteFactory.CreateSourceReaderFromURL(this.FileName, attributes); return new SourceReaderExtra(this.FileName, this.State, reader); }