Beispiel #1
0
        private MediaMetadata ProbeFile(string inFile)
        {
            string args     = $"-print_format xml=fully_qualified=1 -show_format -show_streams -- \"{inFile}\"";
            var    exResult = ManagedExecution.Start(FFprobePath, args);

            string xmlData = string.Join("\n", exResult.Output);

            if (FFprobeData.Deserialize(xmlData, out FFprobeData t))
            {
                List <MediaStream> audioStreams    = new List <MediaStream>();
                List <MediaStream> videoStreams    = new List <MediaStream>();
                List <MediaStream> subtitleStreams = new List <MediaStream>();
                foreach (var s in t.streams)
                {
                    switch (s.codec_type)
                    {
                    case "audio":
                        audioStreams.Add(s);
                        break;

                    case "video":
                        videoStreams.Add(s);
                        break;

                    case "subtitle":
                        subtitleStreams.Add(s);
                        break;

                    default:
                        break;
                    }
                }

                var metadata = new Dictionary <string, string>();
                if (t.format.tag != null)
                {
                    foreach (var item in t.format.tag)
                    {
                        if (!metadata.ContainsKey(item.key))
                        {
                            metadata.Add(item.key.ToLower(System.Globalization.CultureInfo.InvariantCulture), item.value);
                        }
                    }
                }

                var firstVideoStream = videoStreams.FirstOrDefault(x => CommandBuilder.SupportedCodecs.ContainsKey(x.codec_name));
                var firstAudioStream = audioStreams.FirstOrDefault(x => CommandBuilder.SupportedCodecs.ContainsKey(x.codec_name));

                if (!decimal.TryParse(firstVideoStream?.r_frame_rate, out decimal framerate))
                {
                    framerate = 24;
                }

                float duration = t.format != null ? t.format.duration : 0;

                var meta = new MediaMetadata(videoStreams, audioStreams, subtitleStreams, metadata, firstVideoStream?.bit_rate ?? t.format.bit_rate, framerate, duration);
                return(meta);
            }

            return(null);
        }
Beispiel #2
0
        public MediaMetadata ProbeFile(string inFile, out FFprobeData rawProbe)
        {
            string args     = $"-print_format xml=fully_qualified=1 -show_format -show_streams -- \"{inFile}\"";
            var    exResult = ManagedExecution.Start(FFprobePath, args);

            string xmlData = string.Join("\n", exResult.Output);

            rawProbe = FFprobeData.Deserialize(xmlData);
            List <MediaStream> audioStreams    = new List <MediaStream>();
            List <MediaStream> videoStreams    = new List <MediaStream>();
            List <MediaStream> subtitleStreams = new List <MediaStream>();

            foreach (var s in rawProbe.streams)
            {
                switch (s.codec_type)
                {
                case "audio":
                    audioStreams.Add(s);
                    break;

                case "video":
                    videoStreams.Add(s);
                    break;

                case "subtitle":
                    subtitleStreams.Add(s);
                    break;

                default:
                    break;
                }
            }

            var metadata = new Dictionary <string, string>();

            if (rawProbe.format.tag != null)
            {
                foreach (var item in rawProbe.format.tag)
                {
                    if (!metadata.ContainsKey(item.key))
                    {
                        metadata.Add(item.key.ToLower(System.Globalization.CultureInfo.InvariantCulture), item.value);
                    }
                }
            }

            var firstVideoStream = videoStreams.FirstOrDefault(x => Constants.SupportedInputCodecs.ContainsKey(x.codec_name)) ?? videoStreams.FirstOrDefault();

            decimal framerate = 0;
            long    bitrate   = 0;

            if (firstVideoStream == null)
            {
                // Leave them as zero.
            }
            else
            {
                if (decimal.TryParse(firstVideoStream.r_frame_rate, out framerate))
                {
                }
                else if (firstVideoStream.r_frame_rate.Contains("/"))
                {
                    try
                    {
                        framerate = firstVideoStream.r_frame_rate
                                    .Split('/')
                                    .Select(component => decimal.Parse(component))
                                    .Aggregate((dividend, divisor) => dividend / divisor);
                    }
                    catch (Exception)
                    {
                        // Leave it as zero.
                    }
                }

                bitrate = firstVideoStream.bit_rate != 0 ? firstVideoStream.bit_rate : (rawProbe.format?.bit_rate ?? 0);
            }

            float duration = rawProbe.format != null ? rawProbe.format.duration : 0;

            var meta = new MediaMetadata(inFile, videoStreams, audioStreams, subtitleStreams, metadata, bitrate, framerate, duration);

            return(meta);
        }