/// <summary>
 /// Parses the output of X264 or X265 to return the info of all input streams.
 /// </summary>
 /// <param name="outputText">The text containing the file information to parse.</param>
 public void ParseFileInfo(string outputText, ProcessOptionsEncoder options)
 {
     IsParsed = true;
     if (options?.FrameCount > 0)
     {
         FrameCount = options.FrameCount;
     }
     else
     {
         FrameCount = ParseFrameCount(outputText);
     }
 }
Exemple #2
0
 public ProcessWorkerEncoder(IMediaConfig config, IProcessFactory processFactory, IFileSystemService fileSystemService, IFileInfoParserFactory parserFactory, ProcessOptionsEncoder options)
     : base(config, processFactory, options ?? new ProcessOptionsEncoder())
 {
     _fileSystem    = fileSystemService ?? throw new ArgumentNullException(nameof(fileSystemService));
     _parserFactory = parserFactory ?? throw new ArgumentNullException(nameof(parserFactory));
     OutputType     = ProcessOutput.Error;
 }
        /// <summary>
        /// Parses the output of FFmpeg to return the info of all input streams.
        /// </summary>
        /// <param name="outputText">The text containing the file information to parse.</param>
        public void ParseFileInfo(string outputText, ProcessOptionsEncoder options)
        {
            IsParsed     = true;
            FileDuration = new TimeSpan();
            FileStreams.Clear();

            if (string.IsNullOrEmpty(outputText))
            {
                return;
            }

            var outLines = outputText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            // Find duration line.
            var durationIndex = -1;

            for (var i = 0; i < outLines.Length; i++)
            {
                if (outLines[i].StartsWith("  Duration: ", StringComparison.InvariantCulture))
                {
                    durationIndex = i;
                    // Parse duration line.
                    var durationInfo   = outLines[i].Trim().Split(new string[] { ", " }, StringSplitOptions.None);
                    var durationString = durationInfo[0].Split(' ')[1];
                    if (durationString == "N/A")
                    {
                        FileDuration = new TimeSpan(0);
                    }
                    else if (!string.IsNullOrWhiteSpace(durationString))
                    {
                        try
                        {
                            FileDuration = TimeSpan.Parse(durationString, CultureInfo.InvariantCulture);
                        }
                        catch (FormatException) { }
                        catch (OverflowException) { }
                    }
                    break;
                }
            }

            // Find input streams.
            MediaStreamInfo?itemInfo;

            for (var i = durationIndex + 1; i < outLines.Length; i++)
            {
                if (outLines[i].StartsWith("    Stream #0:", StringComparison.InvariantCulture))
                {
                    // Parse input stream.
                    itemInfo = ParseStreamInfo(outLines[i]);
                    if (itemInfo != null)
                    {
                        FileStreams.Add(itemInfo);
                    }
                }
                else if (outLines[i].StartsWith("Output ", StringComparison.InvariantCulture))
                {
                    break;
                }
            }

            // Calculate FrameCount.
            if (options?.FrameCount > 0)
            {
                FrameCount = options.FrameCount;
            }
            else if (VideoStream != null)
            {
                FrameCount = (long)(FileDuration.TotalSeconds * VideoStream.FrameRate);
            }
        }