コード例 #1
0
        public MediaInfo?GetMediaInfo(string inputFile)
        {
            InputFile = inputFile;
            var ms = new MemoryStream();

            try {
                FFprobeProcess = Process.Start(
                    new ProcessStartInfo(Utils.FfprobePath,
                                         $" -hide_banner -loglevel error -print_format json -sexagesimal -show_format -show_streams  \"{inputFile}\"")
                {
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    WorkingDirectory       = Path.GetDirectoryName(Utils.FfprobePath),
                    RedirectStandardInput  = false,
                    RedirectStandardOutput = true,
                });

                if (FFprobeProcess == null)
                {
                    Logger.Instance.Info(Properties.Resources.FFMpegProcessWasAborted);
                    throw new FFProbeException(-1, Properties.Resources.FFprobeProcessWasAborted);
                }

                //start reading here, otherwise the streams fill up and ffmpeg will block forever
                var imgDataTask = FFprobeProcess.StandardOutput.BaseStream.CopyToAsync(ms);

                WaitProcessForExit();

                if (!imgDataTask.Wait((int)ExecutionTimeout.TotalMilliseconds))
                {
                    throw new TimeoutException("Copying ffprobe output timed out.");
                }
                if (!ms.TryGetBuffer(out var buf))
                {
                    throw new TimeoutException("Failed to get memory buffer.");
                }
                var result = FFProbeJsonReader.Read(buf.AsSpan(), inputFile);
                FFprobeProcess?.Close();
                return(result);
            }
            catch (Exception ex) {
                if (FFprobeProcess != null)
                {
                    EnsureProcessStopped();
                }
                Logger.Instance.Info(string.Format(Properties.Resources.FFprobeError, ex.Message, inputFile));
                return(null);
            }
            finally {
                ms.Dispose();
            }
        }
コード例 #2
0
        public MediaInfo GetMediaInfo(string inputFile)
        {
            InputFile = inputFile;
            try {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append(" -hide_banner -loglevel error -print_format json -sexagesimal -show_format -show_streams");
                if (!string.IsNullOrEmpty(CustomArgs))
                {
                    stringBuilder.Append(CustomArgs);
                }
                stringBuilder.AppendFormat(" \"{0}\" ", inputFile);
                FFprobeProcess = Process.Start(new ProcessStartInfo(Utils.FfprobePath, stringBuilder.ToString())
                {
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    WorkingDirectory       = Path.GetDirectoryName(Utils.FfprobePath),
                    RedirectStandardInput  = false,
                    RedirectStandardOutput = true,
                });

                if (FFprobeProcess == null)
                {
                    Logger.Instance.Info(Properties.Resources.FFMpegProcessWasAborted);
                    throw new FFProbeException(-1, Properties.Resources.FFprobeProcessWasAborted);
                }

                var ms = new MemoryStream();
                //start reading here, otherwise the streams fill up and ffmpeg will block forever
                var imgDataTask = FFprobeProcess.StandardOutput.BaseStream.CopyToAsync(ms);

                WaitProcessForExit();

                imgDataTask.Wait(1000);
                var result = FFProbeJsonReader.Read(ms.ToArray(), inputFile);
                FFprobeProcess?.Close();
                return(result);
            }
            catch (Exception ex) {
                if (FFprobeProcess != null)
                {
                    EnsureProcessStopped();
                }
                Logger.Instance.Info(string.Format(Properties.Resources.FFprobeError, ex.Message, inputFile));
                return(null);
            }
        }