Example #1
0
        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));
            }
        }