Esempio n. 1
0
        public void Dispose()
        {
            _prevWriterDisposeTask?.Wait();
            _currentWriter?.Dispose();

            var currentFile = GetFileName(_currentFile);
            var prevFile    = GetFileName((_currentFile - 1 + NoOfFiles) % NoOfFiles);

            var currentDuration = _currentFrame * 1000 / _videoWriterArgs.FrameRate;
            var prevDuration    = _duration * 1000 - currentDuration;

            // Prev file only
            if (currentDuration == 0 && File.Exists(prevFile))
            {
                File.Move(prevFile, _videoWriterArgs.FileName);
            }
            else if (prevDuration == 0 || !File.Exists(prevFile)) // Current file only
            {
                File.Move(currentFile, _videoWriterArgs.FileName);
            }
            else // Concat
            {
                var argsBuilder = new FFmpegArgsBuilder();

                argsBuilder.AddInputFile(prevFile)
                .AddArg("ss", $"{TimeSpan.FromMilliseconds(currentDuration):g}");

                argsBuilder.AddInputFile(currentFile);

                var hasAudio = _videoWriterArgs.AudioProvider != null;

                const string filter          = "[0:v] [1:v] concat=n=2:v=1:a=0 [v]";
                const string filterWithAudio = "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]";

                var output = argsBuilder.AddOutputFile(_videoWriterArgs.FileName)
                             .AddArg($"-filter_complex \"{(hasAudio ? filterWithAudio : filter)}\"")
                             .AddArg("-map \"[v]\"");

                if (hasAudio)
                {
                    output.AddArg("-map \"[a]\"");
                }

                var args = argsBuilder.GetArgs();

                var process = FFmpegService.StartFFmpeg(args, _videoWriterArgs.FileName, out _);

                process.WaitForExit();
            }

            for (var i = 0; i < NoOfFiles; i++)
            {
                var filename = GetFileName(i);

                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
        }
Esempio n. 2
0
        public async Task StartAsync(VideoConverterArgs Args, IProgress <int> Progress)
        {
            if (!FFmpegService.FFmpegExists)
            {
                throw new FFmpegNotFoundException();
            }

            var argsBuilder = new FFmpegArgsBuilder();

            argsBuilder.AddInputFile(Args.InputFile);

            var output = argsBuilder.AddOutputFile(Args.FileName)
                         .SetFrameRate(Args.FrameRate);

            _videoCodec.Apply(ServiceProvider.Get <FFmpegSettings>(), Args, output);

            //if (Args.AudioProvider != null)
            {
                _videoCodec.AudioArgsProvider(Args.AudioQuality, output);
            }

            var process = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), Args.FileName, out var log);

            log.ProgressChanged += Progress.Report;

            await Task.Run(() => process.WaitForExit());

            if (process.ExitCode != 0)
            {
                throw new FFmpegException(process.ExitCode);
            }

            Progress.Report(100);
        }
Esempio n. 3
0
        public async Task Run(string SourceFile,
                              TimeSpan From,
                              TimeSpan To,
                              string DestFile,
                              bool HasAudio)
        {
            var argsBuilder = new FFmpegArgsBuilder();

            var inputArgs = argsBuilder.AddInputFile(SourceFile)
                            .AddArg("ss", From)
                            .AddArg("to", To);

            if (HasAudio)
            {
                inputArgs.SetAudioCodec("copy");
            }

            argsBuilder.AddOutputFile(DestFile);

            var args = argsBuilder.GetArgs();

            var process = FFmpegService.StartFFmpeg(args, DestFile, out _);

            await Task.Factory.StartNew(process.WaitForExit);

            if (process.ExitCode != 0)
            {
                throw new FFmpegException(process.ExitCode);
            }
        }
Esempio n. 4
0
        public async Task StartAsync(VideoConverterArgs Args, IProgress <int> Progress)
        {
            if (!FFmpegService.FFmpegExists)
            {
                throw new FFmpegNotFoundException();
            }

            var argsBuilder = new FFmpegArgsBuilder();

            argsBuilder.AddInputFile(Args.InputFile);

            const string filter = "\"[0:v] split [a][b];[a] palettegen [p];[b][p] paletteuse\"";

            argsBuilder.AddOutputFile(Args.FileName)
            .AddArg("filter_complex", filter)
            .SetFrameRate(Args.FrameRate);

            var process = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), Args.FileName, out var log);

            log.ProgressChanged += Progress.Report;

            await Task.Run(() => process.WaitForExit());

            if (process.ExitCode != 0)
            {
                throw new FFmpegException(process.ExitCode);
            }

            Progress.Report(100);
        }