Exemple #1
0
        /// <summary>
        /// Create a new <see cref="T:FfmpegSharp.FfmpegProcess"/> instance prepared to run Ffmpeg.
        /// </summary>
        /// <returns>The Ffmpeg process instance.</returns>
        public static FfmpegProcess Create(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new FfmpegException("Ffmpeg path not specified");
            }

            string ffmpegExec;

            if (File.Exists(path))
            {
                ffmpegExec = path;
            }
            else
            {
                throw new FileNotFoundException("Ffmpeg executable not found");
            }

            FfmpegProcess ffmpegProc = new FfmpegProcess();

            ffmpegProc.StartInfo.FileName         = ffmpegExec;
            ffmpegProc.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            ffmpegProc.StartInfo.EnvironmentVariables["AV_LOG_FORCE_NOCOLOR"] = "1";

            string ffmpegPath = Path.GetDirectoryName(ffmpegExec);

            if (!String.IsNullOrEmpty(ffmpegPath))
            {
                string pathEnv = Environment.GetEnvironmentVariable("PATH");
                pathEnv += Path.PathSeparator + ffmpegPath;
                ffmpegProc.StartInfo.EnvironmentVariables["PATH"] = pathEnv;
            }

            return(ffmpegProc);
        }
Exemple #2
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");
            }
        }
Exemple #3
0
        /// <summary>
        /// Kills the Ffmpeg process.
        /// </summary>
        public void Abort()
        {
            if ((FfmpegProcess_ != null) && !FfmpegProcess_.HasExited)
            {
                try
                {
                    FfmpegProcess_.Kill();
                }

                finally
                {
                    FfmpegProcess_.Dispose();
                    FfmpegProcess_ = null;
                }
            }
        }
Exemple #4
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;
                }
            }
        }