예제 #1
0
        /// <summary>
        /// Gets information about the given file.
        /// </summary>
        /// <returns>File information as a <see cref="MediaInfo"/> instance.</returns>
        /// <param name="inputFile">Input file.</param>
        public MediaInfo GetInfo(string inputFile)
        {
            if (!File.Exists(inputFile))
            {
                throw new FileNotFoundException("File not found: " + inputFile);
            }

            using (FfmpegProcess ffmpeg = FfmpegProcess.Create(Path))
            {
                ffmpeg.StartInfo.RedirectStandardError = true;
                ffmpeg.StartInfo.Arguments             = "-hide_banner -i " + inputFile;
                ffmpeg.Start();

                LastCommand = Path + " " + ffmpeg.StartInfo.Arguments;

                string output = ffmpeg.StandardError.ReadToEnd();

                if (ffmpeg.WaitForExit(10000) == false)
                {
                    throw new TimeoutException("FFmpeg response timeout");
                }

                if (output != null)
                {
                    TimeSpan duration = TimeSpan.MinValue;

                    Match matchDuration = FfmpegProcess.DurationRegEx.Match(output);
                    //Match matchVideoMetadata = FfmpegProcess.VideoMetadataRegEx.Match(output);

                    if (!matchDuration.Success)
                    {
                        throw new FfmpegException("Unexpected output from FFmpeg");
                    }

                    TimeSpan.TryParseExact(matchDuration.Groups[1].Value, @"hh\:mm\:ss\.ff", CultureInfo.InvariantCulture, out duration);

                    /*
                     * if (matchVideoMetadata.Success)
                     * {
                     * Match matchVideoDetails = FfmpegProcess.VideoDetailsRegEx.Match(matchVideoMetadata.Groups[1].Value);
                     *
                     * if (matchVideoDetails.Success)
                     * {
                     *
                     * }
                     * }
                     */

                    return(new MediaInfo(duration));
                }

                throw new FfmpegException("Unexpected output from FFmpeg");
            }
        }
예제 #2
0
        /// <summary>
        /// Spawns a new Ffmpeg process using the specified options in this instance.
        /// </summary>
        /// <param name="inputFiles">Media files to be processed.</param>
        /// <param name="outputFiles">Output files.</param>
        public void Process(InputFile[] inputFiles, OutputFile[] outputFiles)
        {
            FfmpegProcess_ = FfmpegProcess.Create(Path);

            try
            {
                FfmpegProcess_.ErrorDataReceived += OnFfmpegOutputReceived;

                List <string> args = new List <string>();

                args.Add("-hide_banner");
                args.Add("-nostdin");
                args.Add("-stats");
                args.Add("-loglevel fatal");

                // Avoid FFmpeg asking for overwrite if a specified output file already exists.

                args.Add(OverwriteOutput ? "-y" : "-n");

                // Global options.

                foreach (var customArg in CustomArgs)
                {
                    if (string.IsNullOrEmpty(customArg.Value))
                    {
                        args.Add("-" + customArg.Key);
                    }
                    else
                    {
                        args.Add(string.Format("-{0} {1}", customArg.Key, customArg.Value));
                    }
                }

                // Input options and files.

                if ((inputFiles != null) && (inputFiles.Length > 0))
                {
                    foreach (InputFile inputFile in inputFiles)
                    {
                        args.Add(inputFile.ToString());
                    }
                }
                else
                {
                    throw new FfmpegException("No input files specified");
                }

                // Output options and files.

                if ((outputFiles != null) && (outputFiles.Length > 0))
                {
                    foreach (OutputFile outputFile in outputFiles)
                    {
                        args.Add(outputFile.ToString());
                    }
                }
                else
                {
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        args.Add("NUL");
                    }
                    else
                    {
                        args.Add("/dev/null");
                    }
                }

                FfmpegProcess_.StartInfo.Arguments = String.Join(" ", args);
                LastCommand = Path + " " + FfmpegProcess_.StartInfo.Arguments;

                try
                {
                    FfmpegProcess_.Start();
                    FfmpegProcess_.BeginErrorReadLine();
                    FfmpegProcess_.WaitForExit();
                }

                catch (Exception ex)
                {
                    throw new FfmpegException("Cannot spawn FFmpeg process", ex);
                }
            }

            finally
            {
                if (FfmpegProcess_ != null)
                {
                    FfmpegProcess_.Dispose();
                    FfmpegProcess_ = null;
                }
            }
        }