void TranscodeVideoTest(string filename) { using (MFSystem.Start()) { var details = VideoAttributes.TestFor(filename); TraceInfo.WriteLine("Frame Rate: {0}, Frame Size: {1}x{2}, Video: {3} @ {4}Mbs, Audio: {5}, {6}Khz @ {7}Kbs, ".F (details.FrameRate, details.FrameSize.Width, details.FrameSize.Height, details.VideoEncoding, details.BitRate == 0 ? "-- " : details.BitRate.ToString(), details.AudioEncoding, details.AudioSamplesPerSecond / 1000, details.AudioAverageBytesPerSecond / 1000)); TraceInfo.WriteLine("Begining video re-encoding."); details.Transcoder.ProcessVideo((readers, saveToSink) => { readers.First().SourceReader.Samples(AVOperations.FadeIn(saveToSink)); }); TraceInfo.WriteLine("Video converted. Review the video file {0} to confirm it looks OK.", details.Transcoder.DestinationFile); TraceInfo.WriteLine("Success!"); } }
public void _OverlayRaceDataOntoVideo(Action <long, long> progress, Action completed, bool highlightOnly, CancellationToken token) { bool TranscodeFull = !highlightOnly; var transcodeHigh = new Task(() => TranscodeAndOverlayMarshaled.Apply("HighLights", gameDataFile, videoBitRate, destinationHighlightsFile, true, highlightOnly ? progress : null, token)); var transcodeFull = new Task(() => TranscodeAndOverlayMarshaled.Apply("Full", gameDataFile, videoBitRate, destinationFile, false, progress, token)); using (MFSystem.Start()) { var waits = new List <Task>(); transcodeHigh.Start(); waits.Add(transcodeHigh); //Seem to have some kind of bug in MediaFoundation - where if two threads attempt to open source Readers to the same file, we get exception raised. //To work around issue, delay the start of the second transcoder - so we dont have two threads opening at the same time. if (TranscodeFull) { Thread.Sleep(10000); transcodeFull.Start(); waits.Add(transcodeFull); } Task.WaitAll(waits.ToArray()); } completed(); }
public Main() { InitializeComponent(); mfSystem = MFSystem.Start(); player = new Player(this.Handle); }
void TranscodeVideoTest(string filename) { List <int> supportedAudioBitRates = new List <int>(); using (MFSystem.Start()) { var details = VideoAttributes.TestFor(filename); TraceInfo.WriteLine("Frame Rate: {0}, Frame Size: {1}x{2}, Video: {3} @ {4}Mbs, Audio: {5}, {6}Khz @ {7}Kbs, ".F (details.FrameRate, details.FrameSize.Width, details.FrameSize.Height, details.VideoEncoding, details.BitRate == 0 ? "-- " : details.BitRate.ToString(), details.AudioEncoding, details.AudioSamplesPerSecond / 1000, details.AudioAverageBytesPerSecond / 1000)); TraceInfo.WriteLine("Begining video re-encoding."); details.Transcoder.ProcessVideo((readers, saveToSink) => { int lastSecond = 0; var fn = AVOperations.FadeIn(saveToSink); readers.First().SourceReader.Samples(sample => { if (sample.Stream.CurrentMediaType.IsVideo && sample.Sample != null) { var s = (int)sample.Sample.SampleTime.FromNanoToSeconds(); if (s != lastSecond) { TraceInfo.WriteLine("Converted: {0} seconds", s); } lastSecond = s; if (s > 10) { return(false); } } return(fn(sample)); }); }); TraceInfo.WriteLine("Video converted. Review the video file {0} to confirm it looks OK.", details.Transcoder.DestinationFile); TraceInfo.WriteLine("Success!"); } }
MediaType CreateTargetAudioMediaType(MediaType nativeMediaType) { var numberOfChannels = nativeMediaType.AudioNumberOfChannels; var sampleRate = nativeMediaType.AudioSamplesPerSecond; var availableTypes = MFSystem.TranscodeGetAudioOutputAvailableTypes(TARGET_AUDIO_FORMAT, MFT_EnumFlag.All); TraceDebug.WriteLine("Searching for audio transcoding for sampleRate of {0}khz and {1} channels".F(sampleRate / 1000, numberOfChannels)); var type = availableTypes .OrderByDescending(t => t.AudioAverageBytesPerSecond) .ThenByDescending(t => t.AudioNumberOfChannels) .FirstOrDefault(t => t.AudioSamplesPerSecond == sampleRate && t.AudioNumberOfChannels == numberOfChannels); if (type == null) { TraceDebug.WriteLine("No audio transcoder found. Searching for transcoding with 2 or fewer channels at any sample rate"); type = availableTypes .OrderByDescending(t => t.AudioAverageBytesPerSecond) .ThenByDescending(t => t.AudioNumberOfChannels) .FirstOrDefault(t => t.AudioNumberOfChannels <= 2); } if (type == null) { TraceDebug.WriteLine("No audio transcoder found. Search for first compatible transcoder"); type = availableTypes .OrderByDescending(t => t.AudioAverageBytesPerSecond) .ThenByDescending(t => t.AudioNumberOfChannels) .FirstOrDefault(); } if (type != null) { TraceDebug.WriteLine("Found audio track. SampleRate: {0}, Channels: {1}, BitRate: {2}kbs".F(type.AudioSamplesPerSecond / 1000, type.AudioNumberOfChannels, type.AudioAverageBytesPerSecond * 8 / 1000)); return(new MediaType(type)); } throw new Exception("Unable to find target audio format"); }
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)); } }