コード例 #1
0
ファイル: RegexEngine.cs プロジェクト: elithrade/FFmpeg.NET
        internal static void TestAudio(string data, FFmpegParameters engine)
        {
            var matchMetaAudio = _index[Find.MetaAudio].Match(data);

            if (!matchMetaAudio.Success)
            {
                return;
            }

            var fullMetadata = matchMetaAudio.Groups[1].ToString();

            var matchAudioFormatHzChannel = _index[Find.AudioFormatHzChannel].Match(fullMetadata).Groups;
            var matchAudioBitRate         = _index[Find.BitRate].Match(fullMetadata).Groups;

            if (engine.InputFile.MetaData == null)
            {
                engine.InputFile.MetaData = new MetaData();
            }

            if (engine.InputFile.MetaData.AudioData == null)
            {
                engine.InputFile.MetaData.AudioData = new MetaData.Audio
                {
                    Format        = matchAudioFormatHzChannel[1].ToString(),
                    SampleRate    = matchAudioFormatHzChannel[2].ToString(),
                    ChannelOutput = matchAudioFormatHzChannel[3].ToString(),
                    BitRateKbs    = !string.IsNullOrWhiteSpace(matchAudioBitRate[1].ToString()) ? Convert.ToInt32(matchAudioBitRate[1].ToString()) : 0
                }
            }
            ;
        }
コード例 #2
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        public async Task ConvertAsync(IInputArgument input, Stream output, ConversionOptions options, CancellationToken cancellationToken)
        {
            var pipeName   = $"{_pipePrefix}{Guid.NewGuid()}";
            var parameters = new FFmpegParameters
            {
                Task              = FFmpegTask.Convert,
                Input             = input,
                Output            = new OutputPipe(GetPipePath(pipeName)),
                ConversionOptions = options
            };

            var process = CreateProcess(parameters);
            var pipe    = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

            await pipe.WaitForConnectionAsync(cancellationToken);

            await Task.WhenAll(
                pipe.CopyToAsync(output, cancellationToken),
                process.ExecuteAsync(cancellationToken).ContinueWith(x =>
            {
                pipe.Disconnect();
                pipe.Dispose();
            }, cancellationToken)
                ).ConfigureAwait(false);

            Cleanup(process);
        }
コード例 #3
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        private async Task ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken)
        {
            var ffmpegProcess = CreateProcess(parameters);
            await ffmpegProcess.ExecuteAsync(cancellationToken).ConfigureAwait(false);

            Cleanup(ffmpegProcess);
        }
コード例 #4
0
        private async Task ExecuteAsync(ProcessStartInfo startInfo, FFmpegParameters parameters, CancellationToken?cancellationToken = null)
        {
            var       messages        = new List <string>();
            Exception caughtException = null;

            using (var ffmpegProcess = new Process()
            {
                StartInfo = startInfo
            })
            {
                ffmpegProcess.ErrorDataReceived += (sender, e) => OnData(new ConversionDataEventArgs(e.Data, parameters.InputFile, parameters.OutputFile));
                ffmpegProcess.ErrorDataReceived += (sender, e) => FFmpegProcessOnErrorDataReceived(e, parameters, ref caughtException, messages);

                await ffmpegProcess.WaitForExitAsync((exitCode) => OnException(messages, parameters, exitCode, caughtException), cancellationToken);

                if (caughtException != null || ffmpegProcess.ExitCode != 0)
                {
                    OnException(messages, parameters, ffmpegProcess.ExitCode, caughtException);
                }
                else
                {
                    OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFile, parameters.OutputFile));
                }
            }
        }
コード例 #5
0
        private void OnException(List <string> messages, FFmpegParameters parameters, int exitCode, Exception caughtException)
        {
            var exceptionMessage = GetExceptionMessage(messages);
            var exception        = new FFmpegException(exceptionMessage, caughtException, exitCode);

            OnConversionError(new ConversionErrorEventArgs(exception, parameters.InputFile, parameters.OutputFile));
        }
コード例 #6
0
        public async Task ExecuteAsync(string arguments, CancellationToken?cancellationToken = null)
        {
            var parameters = new FFmpegParameters {
                CustomArguments = arguments
            };

            await ExecuteAsync(parameters, cancellationToken);
        }
コード例 #7
0
        public async Task ExecuteAsync(FFmpegParameters parameters, string ffmpegFilePath, CancellationToken?cancellationToken = null)
        {
            var argumentBuilder = new FFmpegArgumentBuilder();
            var arguments       = argumentBuilder.Build(parameters);
            var startInfo       = GenerateStartInfo(ffmpegFilePath, arguments);

            await ExecuteAsync(startInfo, parameters, cancellationToken);
        }
コード例 #8
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        public async Task ExecuteAsync(string arguments, CancellationToken cancellationToken)
        {
            var parameters = new FFmpegParameters
            {
                CustomArguments = arguments,
            };

            await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);
        }
コード例 #9
0
        public async Task ExecuteAsync(FFmpegParameters parameters, string ffmpegFilePath, CancellationToken cancellationToken = default)
        {
            var argumentBuilder = new FFmpegArgumentBuilder();
            var arguments       = argumentBuilder.Build(parameters);
            var startInfo       = GenerateStartInfo(ffmpegFilePath, arguments);

            Debug.WriteLine($"ffmpeg arguments: {arguments}");
            await ExecuteAsync(startInfo, parameters, cancellationToken);
        }
コード例 #10
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        private FFmpegProcess CreateProcess(FFmpegParameters parameters)
        {
            var ffmpegProcess = new FFmpegProcess(parameters, _ffmpegPath);

            ffmpegProcess.Progress  += OnProgress;
            ffmpegProcess.Completed += OnComplete;
            ffmpegProcess.Error     += OnError;
            ffmpegProcess.Data      += OnData;
            return(ffmpegProcess);
        }
コード例 #11
0
        private async Task ExecuteAsync(FFmpegParameters parameters, CancellationToken?cancellationToken = null)
        {
            var ffmpegProcess = new FFmpegProcess();

            ffmpegProcess.Progress  += OnProgress;
            ffmpegProcess.Completed += OnComplete;
            ffmpegProcess.Error     += OnError;
            ffmpegProcess.Data      += OnData;
            await ffmpegProcess.ExecuteAsync(parameters, _ffmpegPath, cancellationToken);
        }
コード例 #12
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        public async Task <MetaData> GetMetaDataAsync(InputFile mediaFile, CancellationToken cancellationToken)
        {
            var parameters = new FFmpegParameters
            {
                Task  = FFmpegTask.GetMetaData,
                Input = mediaFile
            };

            await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);

            return(mediaFile.MetaData);
        }
コード例 #13
0
        public async Task <MetaData> GetMetaDataAsync(MediaFile mediaFile, CancellationToken?cancellationToken = null)
        {
            var parameters = new FFmpegParameters
            {
                Task      = FFmpegTask.GetMetaData,
                InputFile = mediaFile
            };

            await ExecuteAsync(parameters, cancellationToken);

            return(parameters.InputFile.MetaData);
        }
コード例 #14
0
ファイル: FFmpegProcess.cs プロジェクト: cmxl/FFmpeg.NET
        private void FFmpegProcessOnErrorDataReceived(DataReceivedEventArgs e, FFmpegParameters parameters, ref Exception exception, List <string> messages)
        {
            var totalMediaDuration = new TimeSpan();

            if (e.Data == null)
            {
                return;
            }

            try
            {
                TryUpdateMediaInfo(e.Data);
                messages.Insert(0, e.Data);
                if (parameters.Input != null)
                {
                    RegexEngine.TestVideo(e.Data, parameters);
                    RegexEngine.TestAudio(e.Data, parameters);

                    var matchDuration = RegexEngine._index[RegexEngine.Find.Duration].Match(e.Data);
                    if (matchDuration.Success)
                    {
                        if (parameters.Input is MediaFile input)
                        {
                            if (input.MetaData == null)
                            {
                                input.MetaData = new MetaData {
                                    FileInfo = input.FileInfo
                                }
                            }
                            ;

                            RegexEngine.TimeSpanLargeTryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                            input.MetaData.Duration = totalMediaDuration;
                        }
                    }
                }

                if (RegexEngine.IsProgressData(e.Data, out var progressData))
                {
                    if (parameters.Input != null)
                    {
                        progressData.TotalDuration = parameters.Input.MetaData?.Duration ?? totalMediaDuration;
                    }

                    OnProgressChanged(new ConversionProgressEventArgs(progressData, parameters.Input, parameters.Output, _mediaInfo));
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }
コード例 #15
0
        public static string Build(FFmpegParameters parameters)
        {
            if (parameters.HasCustomArguments)
            {
                return(parameters.CustomArguments);
            }

            return(parameters.Task switch
            {
                FFmpegTask.Convert => Convert(parameters.Input, parameters.Output, parameters.ConversionOptions),
                FFmpegTask.GetMetaData => GetMetadata(parameters.Input),
                FFmpegTask.GetThumbnail => GetThumbnail(parameters.Input, parameters.Output, parameters.ConversionOptions),
                _ => throw new ArgumentOutOfRangeException(),
            });
コード例 #16
0
ファイル: Engine.cs プロジェクト: cmxl/FFmpeg.NET
        public async Task <MediaFile> ConvertAsync(InputFile input, OutputFile output, ConversionOptions options, CancellationToken cancellationToken)
        {
            var parameters = new FFmpegParameters
            {
                Task              = FFmpegTask.Convert,
                Input             = input,
                Output            = output,
                ConversionOptions = options
            };

            await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);

            return(output);
        }
コード例 #17
0
ファイル: Engine.cs プロジェクト: zhml530/FFmpeg.NET
        public async Task <MediaFile> GetThumbnailAsync(MediaFile input, MediaFile output, ConversionOptions options, CancellationToken cancellationToken = default)
        {
            var parameters = new FFmpegParameters
            {
                Task              = FFmpegTask.GetThumbnail,
                InputFile         = input,
                OutputFile        = output,
                ConversionOptions = options
            };

            await ExecuteAsync(parameters, cancellationToken);

            return(parameters.OutputFile);
        }
コード例 #18
0
        public string GetConversionString(MediaFile input, MediaFile output, ConversionOptions options)
        {
            var parameters = new FFmpegParameters
            {
                Task              = FFmpegTask.Convert,
                InputFile         = input,
                OutputFile        = output,
                ConversionOptions = options
            };
            var argumentBuilder = new FFmpegArgumentBuilder();
            var arguments       = argumentBuilder.Build(parameters);

            return(arguments);
        }
コード例 #19
0
        public async Task <MediaFile> ConvertAsync(MediaFile input, MediaFile output, ConversionOptions options, CancellationToken?cancellationToken = null)
        {
            var parameters = new FFmpegParameters
            {
                Task              = FFmpegTask.Convert,
                InputFile         = input,
                OutputFile        = output,
                ConversionOptions = options
            };

            await ExecuteAsync(parameters, cancellationToken);

            return(parameters.OutputFile);
        }
コード例 #20
0
        private async Task ExecuteAsync(ProcessStartInfo startInfo, FFmpegParameters parameters, CancellationToken cancellationToken = default)
        {
            using (Process ffmpegProcess = new Process()
            {
                StartInfo = startInfo
            })
            {
                ffmpegProcess.ErrorDataReceived += OnDataHandler;


                Task <int> task = null;
                try
                {
                    task = ffmpegProcess.WaitForExitAsync(null, cancellationToken);
                    await task;
                }
                catch (Exception)
                {
                    // An exception occurs if the user cancels the operation by calling Cancel on the CancellationToken.
                    // Exc.Message will be "A task was canceled." (in English).
                    // task.IsCanceled will be true.
                    if (task.IsCanceled)
                    {
                        throw new TaskCanceledException(task);
                    }
                    // I don't think this can occur, but if some other exception, rethrow it.
                    throw;
                }
                finally
                {
                    task?.Dispose();
                    ffmpegProcess.ErrorDataReceived -= OnDataHandler;
                }


                if (caughtException != null || ffmpegProcess.ExitCode != 0)
                {
                    OnException(messages, parameters, ffmpegProcess.ExitCode, caughtException);
                }
                else
                {
                    OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFile, parameters.OutputFile));
                }
            }
        }
コード例 #21
0
        public string Build(FFmpegParameters parameters)
        {
            if (parameters.HasCustomArguments)
            {
                return(parameters.CustomArguments);
            }

            switch (parameters.Task)
            {
            case FFmpegTask.Convert:
                return(Convert(parameters.InputFile, parameters.OutputFile, parameters.ConversionOptions));

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

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

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #22
0
ファイル: RegexEngine.cs プロジェクト: elithrade/FFmpeg.NET
        internal static void TestVideo(string data, FFmpegParameters engine)
        {
            var matchMetaVideo = _index[Find.MetaVideo].Match(data);

            if (!matchMetaVideo.Success)
            {
                return;
            }

            var fullMetadata = matchMetaVideo.Groups[1].ToString();

            var matchVideoFormatColorSize = _index[Find.VideoFormatColorSize].Match(fullMetadata).Groups;
            var matchVideoFps             = _index[Find.VideoFps].Match(fullMetadata).Groups;
            var matchVideoBitRate         = _index[Find.BitRate].Match(fullMetadata);

            if (engine.InputFile.MetaData == null)
            {
                engine.InputFile.MetaData = new MetaData();
            }

            if (engine.InputFile.MetaData.VideoData == null)
            {
                engine.InputFile.MetaData.VideoData = new MetaData.Video
                {
                    Format     = matchVideoFormatColorSize[1].ToString(),
                    ColorModel = matchVideoFormatColorSize[2].ToString(),
                    FrameSize  = matchVideoFormatColorSize[3].ToString(),
                    Fps        = matchVideoFps[1].Success && !string.IsNullOrEmpty(matchVideoFps[1].ToString()) ? Convert.ToDouble(matchVideoFps[1].ToString(), new CultureInfo("en-US")) : 0,
                    BitRateKbs =
                        matchVideoBitRate.Success
                            ? (int?)Convert.ToInt32(matchVideoBitRate.Groups[1].ToString())
                            : null
                }
            }
            ;
        }
コード例 #23
0
 public FFmpegProcess(FFmpegParameters parameters, string ffmpegFilePath, CancellationToken cancellationToken = default)
 {
     this.parameters        = parameters;
     this.ffmpegFilePath    = ffmpegFilePath;
     this.cancellationToken = cancellationToken;
 }
コード例 #24
0
ファイル: FFmpegProcess.cs プロジェクト: cmxl/FFmpeg.NET
 public FFmpegProcess(FFmpegParameters parameters, string ffmpegFilePath)
 {
     _parameters     = parameters;
     _ffmpegFilePath = ffmpegFilePath;
 }