public static string Serialize(EngineParameters engineParameters)
        {
            switch (engineParameters.Task)
            {
            case FFmpegTask.Storyboard:
                return(GetStoryboard(engineParameters));

            case FFmpegTask.Check:
                return(GetHealthCheck(engineParameters));

            case FFmpegTask.GIF:
                return(GetGIF(engineParameters));

            case FFmpegTask.GenerateHLS:
                return(GetHLS(engineParameters));

            case FFmpegTask.Convert:
                return(Convert(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions));

            case FFmpegTask.GetMetaData:
                return(GetMetadata(engineParameters.InputFile));

            case FFmpegTask.GetThumbnail:
                return(GetThumbnail(engineParameters.ThumbnailOptions));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #2
0
        private void HandleOutput(EngineParameters engineParameters, DataReceivedEventArgs received, List <string> receivedMessagesLog, ref Exception caughtException)
        {
            if (received.Data == null)
            {
                return;
            }

            var totalMediaDuration = new TimeSpan();

            try
            {
                receivedMessagesLog.Insert(0, received.Data);

                SetMetadata(engineParameters, received, totalMediaDuration);

                HandleProgressResult(received, totalMediaDuration);
                HandleConversionResult(engineParameters, received, totalMediaDuration);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                // catch the exception and kill the process since we're in a faulted state
                caughtException = ex;

                try
                {
                    this.FFmpegProcess.Kill();
                }
                catch
                {
                    // swallow exceptions that are thrown when killing the process,
                    // one possible candidate is the application ending naturally before we get a chance to kill it
                }
            }
        }
Exemple #3
0
        /// <summary>
        ///     <para> Retrieve media metadata</para>
        /// </summary>
        /// <param name="inputFile">Retrieves the metadata for the input file. </param>
        public void GetMetadata(MediaFile inputFile)
        {
            var engineParams = new EngineParameters
            {
                InputFile = inputFile,
                Task      = FFmpegTask.GetMetaData
            };

            this.FFmpegEngine(engineParams);
        }
Exemple #4
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Extracts all frames from a video. </summary>
        /// <param name="inputFile">    Video file. </param>
        public void ExtractFrames(MediaFile inputFile)
        {
            EngineParameters engineParams = new EngineParameters
            {
                InputFile = inputFile,
                Task      = FFmpegTask.ExtractFrames
            };

            this.FFmpegEngine(engineParams);
        }
Exemple #5
0
        public void CustomCommand(string ffmpegCommand)
        {
            if (ffmpegCommand.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("ffmpegCommand");
            }

            EngineParameters engineParameters = new EngineParameters { CustomArguments = ffmpegCommand };

            this.StartFFmpegProcess(engineParameters);
        }
Exemple #6
0
        /// <summary>   Retrieve a thumbnail image from a video file. </summary>
        /// <param name="inputFile">    Video file. </param>
        /// <param name="outputFile">   Image file. </param>
        /// <param name="options">      Conversion options. </param>
        public void GetThumbnail(ThumbnailOptions options)
        {
            var engineParams = new EngineParameters
            {
                InputFile        = new MediaFile(options.InputFile),
                ThumbnailOptions = options,
                Task             = FFmpegTask.GetThumbnail
            };

            this.FFmpegEngine(engineParams);
        }
Exemple #7
0
        /// <summary>
        ///     <para> Converts media with default options</para>
        /// </summary>
        /// <param name="inputFile">    Input file. </param>
        /// <param name="outputFile">   Output file. </param>
        public void Convert(MediaFile inputFile, MediaFile outputFile)
        {
            var engineParams = new EngineParameters
            {
                InputFile  = inputFile,
                OutputFile = outputFile,
                Task       = FFmpegTask.Convert
            };

            this.FFmpegEngine(engineParams);
        }
Exemple #8
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     <para> ---</para>
        ///     <para> Converts media with default options</para>
        /// </summary>
        /// <param name="inputFile">    Input file. </param>
        /// <param name="outputFile">   Output file. </param>
        public void Convert(MediaFile inputFile, MediaFile outputFile)
        {
            EngineParameters engineParams = new EngineParameters
                {
                    InputFile = inputFile,
                    OutputFile = outputFile,
                    Task = FFmpegTask.Convert
                };

            this.FFmpegEngine(engineParams);
        }
Exemple #9
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     <para> ---</para>
        ///     <para> Converts media with conversion options</para>
        /// </summary>
        /// <param name="inputFile">    Input file. </param>
        /// <param name="outputFile">   Output file. </param>
        /// <param name="options">      Conversion options. </param>
        public void Convert(MediaFile inputFile, MediaFile outputFile, ConversionOptions options)
        {
            var engineParams = new EngineParameters
            {
                InputFile         = inputFile,
                OutputFile        = outputFile,
                ConversionOptions = options,
                Task = FFmpegTask.Convert
            };

            FFmpegEngine(engineParams);
        }
        public static string GetGIF(EngineParameters engineParameters)
        {
            if (!(engineParameters is GIFParameters gif))
            {
                throw new ArgumentException("Paramters must be of type GIfParamters");
            }

            return(string.Concat($"-i {engineParameters.InputFile.Filename}",
                                 gif.GifOptions.Serialize(),
                                 " ",
                                 gif.OutputFile.Filename));
        }
Exemple #11
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Retrieve a thumbnail image from a video file. </summary>
        /// <param name="inputFile">    Video file. </param>
        /// <param name="outputFile">   Image file. </param>
        /// <param name="options">      Conversion options. </param>
        public void GetThumbnail(MediaFile inputFile, MediaFile outputFile, ConversionOptions options)
        {
            EngineParameters engineParams = new EngineParameters
            {
                InputFile         = inputFile,
                OutputFile        = outputFile,
                ConversionOptions = options,
                Task = FFmpegTask.GetThumbnail
            };

            this.FFmpegEngine(engineParams);
        }
Exemple #12
0
        public byte[] GetThumbnail(MediaFile inputFile, ConversionOptions options)
        {
            EngineParameters engineParams = new EngineParameters
            {
                InputFile         = inputFile,
                ConversionOptions = options,
                Task = FFmpegTask.GetThumbnailStream,
                ReturningStandardOutput = true
            };

            this.FFmpegEngine(engineParams);
            return(this.thumbnailByteArray);
        }
Exemple #13
0
        private void HandleConversionResult(EngineParameters engineParameters, DataReceivedEventArgs received, TimeSpan totalMediaDuration)
        {
            if (engineParameters.Task != FFmpegTask.Convert)
            {
                return;
            }

            if (RegexEngine.IsConvertCompleteData(received.Data, out ConversionCompleteEventArgs convertCompleteEvent))
            {
                convertCompleteEvent.TotalDuration = totalMediaDuration;
                this.OnConversionComplete(convertCompleteEvent);
            }
        }
Exemple #14
0
        public void CustomCommand(string ffmpegCommand)
        {
            if (ffmpegCommand.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(ffmpegCommand));
            }

            var engineParameters = new EngineParameters {
                CustomArguments = ffmpegCommand
            };

            this.StartFFmpegProcess(engineParameters);
        }
        internal static string Serialize(EngineParameters engineParameters)
        {
            switch (engineParameters.Task)
            {
                case FFmpegTask.Convert:
                    return Convert(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions);

                case FFmpegTask.GetMetaData:
                    return GetMetadata(engineParameters.InputFile);

                case FFmpegTask.GetThumbnail:
                    return GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions);
            }
            return null;
        }
        internal static string Serialize(EngineParameters engineParameters)
        {
            switch (engineParameters.Task)
            {
            case FFmpegTask.Convert:
                return(Convert(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions));

            case FFmpegTask.GetMetaData:
                return(GetMetadata(engineParameters.InputFile));

            case FFmpegTask.GetThumbnail:
                return(GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions));
            }
            return(null);
        }
        internal static string Serialize(EngineParameters engineParameters)
        {
            switch (engineParameters.Task)
            {
                case FFmpegTask.Convert:
                    return Convert(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions);

                case FFmpegTask.GetMetaData:
                    return GetMetadata(engineParameters.InputFile);

                case FFmpegTask.GetThumbnail:
                    return GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
Exemple #18
0
        private void FFmpegEngine(EngineParameters engineParameters)
        {
            if (!engineParameters.InputFile.Filename.StartsWith("http://") && !File.Exists(engineParameters.InputFile.Filename))
            {
                throw new FileNotFoundException(Strings.Exception_Media_Input_File_Not_Found, engineParameters.InputFile.Filename);
            }

            try
            {
                this.Mutex.WaitOne();
                this.StartFFmpegProcess(engineParameters);
            }
            finally
            {
                this.Mutex.ReleaseMutex();
            }
        }
        internal static string Serialize(EngineParameters engineParameters)
        {
            switch (engineParameters.Task)
            {
            case FFmpegTask.Convert:
                return(Convert(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions));

            case FFmpegTask.GetMetaData:
                return(GetMetadata(engineParameters.InputFile));

            case FFmpegTask.GetThumbnail:
                return(GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public static string GetHLS(EngineParameters engineParameters)
        {
            var o = engineParameters.HLSOptions;

            return(string.Concat(
                       "-hide_banner -y ",
                       $"-i {engineParameters.InputFile.Filename}",
                       o.DefaultParams,
                       o.SerializeSpeed(),
                       o.SerializeAudio(),
                       o.SerializeVideo(),
                       o.SerializeScale(),
                       o.SerializeBitrate(),
                       o.SerializeCRF(),
                       o.SerializeKeyframes(),
                       o.SerializeHLSConfig()));
        }
Exemple #21
0
        private static void SetMetadata(EngineParameters engineParameters, DataReceivedEventArgs received, TimeSpan totalMediaDuration)
        {
            if (engineParameters.InputFile != null)
            {
                RegexEngine.TestVideo(received.Data, engineParameters);
                RegexEngine.TestAudio(received.Data, engineParameters);

                Match matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(received.Data);
                if (matchDuration.Success)
                {
                    if (engineParameters.InputFile.Metadata == null)
                    {
                        engineParameters.InputFile.Metadata = new Metadata();
                    }

                    TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                    engineParameters.InputFile.Metadata.Duration = totalMediaDuration;
                }
            }
        }
Exemple #22
0
        /// <summary>   Starts FFmpeg process. </summary>
        /// <exception cref="InvalidOperationException">
        ///     Thrown when the requested operation is
        ///     invalid.
        /// </exception>
        /// <exception cref="Exception">
        ///     Thrown when an exception error condition
        ///     occurs.
        /// </exception>
        /// <param name="engineParameters"> The engine parameters. </param>
        private void StartFFmpegProcess(EngineParameters engineParameters)
        {
            var receivedMessagesLog = new List <string>();
            var processStartInfo    = engineParameters.HasCustomArguments
                    ? this.GenerateStartInfo(engineParameters.CustomArguments)
                    : this.GenerateStartInfo(engineParameters);

            using (this.FFmpegProcess = Process.Start(processStartInfo))
            {
                Exception caughtException = null;
                if (this.FFmpegProcess == null)
                {
                    throw new InvalidOperationException(Strings.Exceptions_FFmpeg_Process_Not_Running);
                }

                //FFMPEG outputs to "sterr" to keep "stdout" for redirecting to other apps
                this.FFmpegProcess.ErrorDataReceived += (sender, received) =>
                                                        HandleOutput(engineParameters,
                                                                     received,
                                                                     receivedMessagesLog,
                                                                     ref caughtException);

                this.FFmpegProcess.BeginErrorReadLine();
                this.FFmpegProcess.WaitForExit();

                if ((this.FFmpegProcess.ExitCode != 0) || caughtException != null)
                {
                    // If we are checking the file, do not throw
                    if (engineParameters.Task == FFmpegTask.Check)
                    {
                        HandleCheckResult(engineParameters.InputFile, receivedMessagesLog);
                    }
                    else
                    {
                        throw new Exception(
                                  this.FFmpegProcess.ExitCode + ": " + receivedMessagesLog[1] + receivedMessagesLog[0],
                                  caughtException);
                    }
                }
            }
        }
Exemple #23
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Starts FFmpeg process. </summary>
        /// <exception cref="InvalidOperationException">
        ///     Thrown when the requested operation is
        ///     invalid.
        /// </exception>
        /// <exception cref="Exception">
        ///     Thrown when an exception error condition
        ///     occurs.
        /// </exception>
        /// <param name="engineParameters"> The engine parameters. </param>
        private void StartFFmpegProcess(EngineParameters engineParameters)
        {
            List<string> receivedMessagesLog = new List<string>();
            TimeSpan totalMediaDuration = new TimeSpan();
         
            ProcessStartInfo processStartInfo = engineParameters.HasCustomArguments 
                                              ? this.GenerateStartInfo(engineParameters.CustomArguments)
                                              : this.GenerateStartInfo(engineParameters);

            using (this.FFmpegProcess = Process.Start(processStartInfo))
            {
                Exception caughtException = null;
                if (this.FFmpegProcess == null)
                {
                    throw new InvalidOperationException(Resources.Exceptions_FFmpeg_Process_Not_Running);
                }

                this.FFmpegProcess.ErrorDataReceived += (sender, received) =>
                {
                    if (received.Data == null) return;
#if (DebugToConsole)
                    Console.WriteLine(received.Data);
#endif
                    try
                    {
                        
                        receivedMessagesLog.Insert(0, received.Data);
                        if (engineParameters.InputFile != null)
                        {
                            RegexEngine.TestVideo(received.Data, engineParameters);
                            RegexEngine.TestAudio(received.Data, engineParameters);
                        
                            Match matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(received.Data);
                            if (matchDuration.Success)
                            {
                                if (engineParameters.InputFile.Metadata == null)
                                {
                                    engineParameters.InputFile.Metadata = new Metadata();
                                }

                                TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                                engineParameters.InputFile.Metadata.Duration = totalMediaDuration;
                            }
                        }
                        ConversionCompleteEventArgs convertCompleteEvent;
                        ConvertProgressEventArgs progressEvent;

                        if (RegexEngine.IsProgressData(received.Data, out progressEvent))
                        {
                            progressEvent.TotalDuration = totalMediaDuration;
                            this.OnProgressChanged(progressEvent);
                        }
                        else if (RegexEngine.IsConvertCompleteData(received.Data, out convertCompleteEvent))
                        {
                            convertCompleteEvent.TotalDuration = totalMediaDuration;
                            this.OnConversionComplete(convertCompleteEvent);
                        }
                    }
                    catch (Exception ex)
                    {
                        // catch the exception and kill the process since we're in a faulted state
                        caughtException = ex;

                        try
                        {
                            this.FFmpegProcess.Kill();
                        }
                        catch (InvalidOperationException)
                        {
                            // swallow exceptions that are thrown when killing the process, 
                            // one possible candidate is the application ending naturally before we get a chance to kill it
                        }
                    }
                };

                this.FFmpegProcess.BeginErrorReadLine();
                this.FFmpegProcess.WaitForExit();

                if ((this.FFmpegProcess.ExitCode != 0 && this.FFmpegProcess.ExitCode != 1) || caughtException != null)
                {
                    throw new Exception(
                        this.FFmpegProcess.ExitCode + ": " + receivedMessagesLog[1] + receivedMessagesLog[0],
                        caughtException);
                }
            }
        }
Exemple #24
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Starts FFmpeg process. </summary>
        /// <exception cref="InvalidOperationException">
        ///     Thrown when the requested operation is
        ///     invalid.
        /// </exception>
        /// <exception cref="Exception">
        ///     Thrown when an exception error condition
        ///     occurs.
        /// </exception>
        /// <param name="engineParameters"> The engine parameters. </param>
        private void StartFFmpegProcess(EngineParameters engineParameters)
        {
            var receivedMessagesLog = new List <string>();
            var totalMediaDuration  = new TimeSpan();

            var processStartInfo = engineParameters.HasCustomArguments
                ? GenerateStartInfo(engineParameters.CustomArguments)
                : GenerateStartInfo(engineParameters);

            using (FFmpegProcess = Process.Start(processStartInfo))
            {
                Exception caughtException = null;

                if (FFmpegProcess == null)
                {
                    throw new InvalidOperationException(Resources.Exceptions_FFmpeg_Process_Not_Running);
                }

                FFmpegProcess.ErrorDataReceived += (sender, received) =>
                {
                    if (received.Data == null)
                    {
                        return;
                    }
#if (DebugToConsole)
                    Console.WriteLine(received.Data);
#endif
                    try
                    {
                        receivedMessagesLog.Insert(0, received.Data);
                        if (engineParameters.InputFile != null)
                        {
                            RegexEngine.TestVideo(received.Data, engineParameters);
                            RegexEngine.TestAudio(received.Data, engineParameters);

                            var matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(received.Data);
                            if (matchDuration.Success)
                            {
                                if (engineParameters.InputFile.Metadata == null)
                                {
                                    engineParameters.InputFile.Metadata = new Metadata();
                                }

                                TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                                engineParameters.InputFile.Metadata.Duration = totalMediaDuration;
                            }
                        }

                        if (RegexEngine.IsProgressData(received.Data, out ConvertProgressEventArgs progressEvent))
                        {
                            progressEvent.TotalDuration = totalMediaDuration;
                            OnProgressChanged(progressEvent);
                        }
                        else if (RegexEngine.IsConvertCompleteData(received.Data, out ConversionCompleteEventArgs convertCompleteEvent))
                        {
                            convertCompleteEvent.TotalDuration = totalMediaDuration;
                            OnConversionComplete(convertCompleteEvent);
                        }
                    }
                    catch (Exception ex)
                    {
                        // catch the exception and kill the process since we're in a faulted state
                        caughtException = ex;

                        try
                        {
                            FFmpegProcess.Kill();
                        }
                        catch (InvalidOperationException)
                        {
                            // swallow exceptions that are thrown when killing the process,
                            // one possible candidate is the application ending naturally before we get a chance to kill it
                        }
                    }
                };

                FFmpegProcess.BeginErrorReadLine();
                FFmpegProcess.WaitForExit();

                if (FFmpegProcess.ExitCode != 0 && FFmpegProcess.ExitCode != 1 || caughtException != null)
                {
                    throw new Exception(
                              FFmpegProcess.ExitCode + ": " + receivedMessagesLog[1] + receivedMessagesLog[0],
                              caughtException);
                }
            }
        }
Exemple #25
0
        private ProcessStartInfo GenerateStartInfo(EngineParameters engineParameters)
        {
            string ffmpegCommand = CommandBuilder.Serialize(engineParameters);

            return new ProcessStartInfo
            {
                Arguments = "-nostdin -y -loglevel info " + ffmpegCommand,
                FileName = FFmpegFilePath,
                CreateNoWindow = true,
                RedirectStandardInput = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                WorkingDirectory = Path.GetTempPath()
            };
        }
Exemple #26
0
        private ProcessStartInfo GenerateStartInfo(EngineParameters engineParameters)
        {
            var arguments = CommandBuilder.Serialize(engineParameters);

            return(this.GenerateStartInfo(arguments));
        }
Exemple #27
0
        /// <summary>
        ///     <para> ---</para>
        ///     <para> Converts media with conversion options</para>
        /// </summary>
        /// <param name="inputFile">Input file</param>
        /// <param name="outputFile">Output file</param>
        /// <param name="options">Conversion options</param>
        public void Convert(MediaFile inputFile, MediaFile outputFile, ConversionOptions options)
        {
            var engineParams = new EngineParameters
            {
                InputFile = inputFile,
                OutputFile = outputFile,
                ConversionOptions = options,
                Task = FFmpegTask.Convert
            };

            FFmpegEngine(engineParams);
        }
Exemple #28
0
 public void GenerateGIF(EngineParameters paramters)
 {
     paramters.Task = FFmpegTask.GIF;
     this.FFmpegEngine(paramters);
 }
Exemple #29
0
        private void FFmpegEngine(EngineParameters engineParameters)
        {
            if (!engineParameters.InputFile.Filename.StartsWith("http://") && !File.Exists(engineParameters.InputFile.Filename))
            {
                throw new FileNotFoundException(Resources.Exception_Media_Input_File_Not_Found, engineParameters.InputFile.Filename);
            }

            try
            {
                this.Mutex.WaitOne();
                this.StartFFmpegProcess(engineParameters);
            }
            finally
            {
                this.Mutex.ReleaseMutex();
            }
        }
Exemple #30
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     <para> Retrieve media metadata</para>
        /// </summary>
        /// <param name="inputFile">    Retrieves the metadata for the input file. </param>
        public void GetMetadata(MediaFile inputFile)
        {
            EngineParameters engineParams = new EngineParameters
                {
                    InputFile = inputFile,
                    Task = FFmpegTask.GetMetaData
                };

            this.FFmpegEngine(engineParams);
        }
Exemple #31
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Starts FFmpeg process. </summary>
        /// <exception cref="InvalidOperationException">
        ///     Thrown when the requested operation is
        ///     invalid.
        /// </exception>
        /// <exception cref="Exception">
        ///     Thrown when an exception error condition
        ///     occurs.
        /// </exception>
        /// <param name="engineParameters"> The engine parameters. </param>
        private void StartFFmpegProcess(EngineParameters engineParameters)
        {
            List <string> receivedMessagesLog = new List <string>();
            TimeSpan      totalMediaDuration  = new TimeSpan();

            ProcessStartInfo processStartInfo = engineParameters.HasCustomArguments
                                              ? this.GenerateStartInfo(engineParameters.CustomArguments)
                                              : this.GenerateStartInfo(engineParameters);

            using (this.FFmpegProcess = Process.Start(processStartInfo))
            {
                Exception caughtException = null;
                if (this.FFmpegProcess == null)
                {
                    throw new InvalidOperationException(Resources.Exceptions_FFmpeg_Process_Not_Running);
                }

                this.FFmpegProcess.ErrorDataReceived += (sender, received) =>
                {
                    if (received.Data == null)
                    {
                        return;
                    }
#if (DebugToConsole)
                    Console.WriteLine(received.Data);
#endif
                    try
                    {
                        receivedMessagesLog.Insert(0, received.Data);
                        if (engineParameters.InputFile != null)
                        {
                            RegexEngine.TestVideo(received.Data, engineParameters);
                            RegexEngine.TestAudio(received.Data, engineParameters);

                            Match matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(received.Data);
                            if (matchDuration.Success)
                            {
                                if (engineParameters.InputFile.Metadata == null)
                                {
                                    engineParameters.InputFile.Metadata = new Metadata();
                                }

                                TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                                engineParameters.InputFile.Metadata.Duration = totalMediaDuration;
                            }
                        }
                        ConversionCompleteEventArgs convertCompleteEvent;
                        ConvertProgressEventArgs    progressEvent;

                        if (RegexEngine.IsProgressData(received.Data, out progressEvent))
                        {
                            progressEvent.TotalDuration = totalMediaDuration;
                            this.OnProgressChanged(progressEvent);
                        }
                        else if (RegexEngine.IsConvertCompleteData(received.Data, out convertCompleteEvent))
                        {
                            convertCompleteEvent.TotalDuration = totalMediaDuration;
                            this.OnConversionComplete(convertCompleteEvent);
                        }
                    }
                    catch (Exception ex)
                    {
                        // catch the exception and kill the process since we're in a faulted state
                        caughtException = ex;

                        try
                        {
                            this.FFmpegProcess.Kill();
                        }
                        catch (InvalidOperationException)
                        {
                            // swallow exceptions that are thrown when killing the process,
                            // one possible candidate is the application ending naturally before we get a chance to kill it
                        }
                    }
                };

                if (engineParameters.ReturningStandardOutput)
                {
                    FileStream baseStream = this.FFmpegProcess.StandardOutput.BaseStream as FileStream;
                    this.thumbnailByteArray = null;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        baseStream.CopyTo(ms);
                        this.thumbnailByteArray = ms.ToArray();
                    }
                }

                this.FFmpegProcess.BeginErrorReadLine();
                this.FFmpegProcess.WaitForExit();

                if ((this.FFmpegProcess.ExitCode != 0 && this.FFmpegProcess.ExitCode != 1) || caughtException != null)
                {
                    throw new Exception(
                              this.FFmpegProcess.ExitCode + ": " + receivedMessagesLog[1] + receivedMessagesLog[0],
                              caughtException);
                }
            }
        }
Exemple #32
0
        private ProcessStartInfo GenerateStartInfo(EngineParameters engineParameters)
        {
            string arguments = CommandBuilder.Serialize(engineParameters);

            return this.GenerateStartInfo(arguments);
        }
Exemple #33
0
        /// <summary>
        ///     Where the magic happens
        /// </summary>
        /// <param name="engineParameters">The engine parameters</param>
        private void FFmpegEngine(EngineParameters engineParameters)
        {
            if (!File.Exists(engineParameters.InputFile.Filename))
                throw new FileNotFoundException("Input file not found", engineParameters.InputFile.Filename);

            // Locking the process to ensure FFmpeg is used by one thread at a time.
            lock (Lock)
            {
                //Initialize();

                var receivedMessagesLog = new List<string>();
                var totalMediaDuration = new TimeSpan();

                ProcessStartInfo processStartInfo = GenerateStartInfo(engineParameters);
                using (_ffmpegProcess = Process.Start(processStartInfo))
                {
                    if (_ffmpegProcess == null) throw new InvalidOperationException("FFmpeg process is not running.");

                    _ffmpegProcess.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs received)
                    {
                        if (received.Data == null) return;

            #if (DebugToConsole)
                        Console.WriteLine(received.Data);
            #endif
                        receivedMessagesLog.Insert(0, received.Data);

                        RegexEngine.TestVideo(received.Data, engineParameters);
                        RegexEngine.TestAudio(received.Data, engineParameters);

                        Match matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(received.Data);
                        if (matchDuration.Success)
                        {
                            if (engineParameters.InputFile.Metadata == null)
                                engineParameters.InputFile.Metadata = new Metadata();

                            TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                            engineParameters.InputFile.Metadata.Duration = totalMediaDuration;
                        }

                        ConversionCompleteEventArgs convertCompleteEvent;
                        ConvertProgressEventArgs progressEvent;

                        if (RegexEngine.IsProgressData(received.Data, out progressEvent))
                        {
                            progressEvent.TotalDuration = totalMediaDuration;
                            OnProgressChanged(progressEvent);
                        }
                        else if (RegexEngine.IsConvertCompleteData(received.Data, out convertCompleteEvent))
                        {
                            convertCompleteEvent.TotalDuration = totalMediaDuration;
                            OnConversionComplete(convertCompleteEvent);
                        }
                    };

                    _ffmpegProcess.BeginErrorReadLine();
                    _ffmpegProcess.WaitForExit();

                    if (_ffmpegProcess.ExitCode != 0 && _ffmpegProcess.ExitCode != 1)
                        throw new Exception(_ffmpegProcess.ExitCode + ": " + receivedMessagesLog[1] +
                                            receivedMessagesLog[0]);
                }
            }
        }
        private static string GetStoryboard(EngineParameters engineParameters)
        {
            var result = $"-hide_banner -y -i {engineParameters.InputFile.Filename} {engineParameters.StoryBoardOptions.Serialize()}";

            return(result);
        }
 public static string GetHealthCheck(EngineParameters engineParameters)
 {
     return($"-hide_banner -y -i {engineParameters.InputFile.Filename} -v error -f null 2>&1");
 }
Exemple #36
0
        public void GenerateHLS(EngineParameters parameters)
        {
            parameters.Task = FFmpegTask.GenerateHLS;

            this.FFmpegEngine(parameters);
        }
Exemple #37
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>   Retrieve a thumbnail image from a video file. </summary>
        /// <param name="inputFile">    Video file. </param>
        /// <param name="outputFile">   Image file. </param>
        /// <param name="options">      Conversion options. </param>
        public void GetThumbnail(MediaFile inputFile, MediaFile outputFile, ConversionOptions options)
        {
            EngineParameters engineParams = new EngineParameters
                {
                    InputFile = inputFile,
                    OutputFile = outputFile,
                    ConversionOptions = options,
                    Task = FFmpegTask.GetThumbnail
                };

            this.FFmpegEngine(engineParams);
        }