Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public void GetWaveform(MediaFile inputFile, MediaFile outputFile, ConversionOptions options = null)
        {
            var engineParams = new EngineParameters
            {
                InputFile         = inputFile,
                OutputFile        = outputFile,
                ConversionOptions = options,
                Task = FFmpegTask.GetWaveform
            };

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

            EngineParameters engineParameters = new EngineParameters {
                CustomArguments = ffmpegCommand
            };

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

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

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 7
0
        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));

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

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 8
0
        private void FFmpegEngine(EngineParameters engineParameters)
        {
            if (!engineParameters.InputFile.Filename.StartsWith("http://") && !File.Exists(engineParameters.InputFile.Filename))
            {
                throw new FileNotFoundException(engineParameters.InputFile.Filename);
            }

            try
            {
                if (Mutex != null)
                {
                    this.Mutex.WaitOne();
                }
                this.StartFFmpegProcess(engineParameters);
            }
            finally
            {
                if (Mutex != null)
                {
                    this.Mutex.ReleaseMutex();
                }
            }
        }
Ejemplo n.º 9
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();
                }

                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;
                            }
                        }
                        ConversionCompleted    convertCompleted;
                        ConvertProgressEmitted progressEvent;

                        if (RegexEngine.IsProgressData(received.Data, out progressEvent))
                        {
                            //progress calculated here
                            progressEvent.TotalDuration = totalMediaDuration;
                            var progress = (double)progressEvent.ProcessedDuration.Ticks / (double)totalMediaDuration.Ticks;
                            this.OnProgressChanged(progressEvent);
                        }
                        else if (RegexEngine.IsConvertCompleteData(received.Data, out convertCompleted))
                        {
                            convertCompleted.TotalDuration = totalMediaDuration;
                            this.OnConversionComplete(convertCompleted);
                        }
                    }
                    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);
                }
            }
        }
Ejemplo n.º 10
0
        private ProcessStartInfo GenerateStartInfo(EngineParameters engineParameters)
        {
            string arguments = CommandBuilder.Serialize(engineParameters);

            return(this.GenerateStartInfo(arguments));
        }