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);
        }
Beispiel #2
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);
            }
        }
        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);
        }
        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);
                }
            }
        }
        public FFmpegAudioWriter(string FileName, int AudioQuality, FFmpegAudioArgsProvider AudioArgsProvider, int Frequency = 44100, int Channels = 2)
        {
            if (!FFmpegService.FFmpegExists)
            {
                throw new FFmpegNotFoundException();
            }

            var argsBuilder = new FFmpegArgsBuilder();

            argsBuilder.AddStdIn()
            .SetFormat("s16le")
            .SetAudioCodec("pcm_s16le")
            .SetAudioFrequency(Frequency)
            .SetAudioChannels(Channels)
            .DisableVideo();

            var output = argsBuilder.AddOutputFile(FileName);

            AudioArgsProvider(AudioQuality, output);

            _ffmpegProcess = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), FileName, out _);

            _ffmpegIn = _ffmpegProcess.StandardInput.BaseStream;
        }
        /// <summary>
        /// Creates a new instance of <see cref="FFmpegWriter"/>.
        /// </summary>
        public FFmpegWriter(FFmpegVideoWriterArgs Args)
        {
            if (!FFmpegService.FFmpegExists)
            {
                throw new FFmpegNotFoundException();
            }

            var nv12 = Args.ImageProvider.DummyFrame is INV12Frame;

            var settings = ServiceProvider.Get <FFmpegSettings>();

            var w = Args.ImageProvider.Width;
            var h = Args.ImageProvider.Height;

            _videoBuffer = new byte[(int)(w * h * (nv12 ? 1.5 : 4))];

            Console.WriteLine($"Video Buffer Allocated: {_videoBuffer.Length}");

            var videoPipeName = GetPipeName();

            var argsBuilder = new FFmpegArgsBuilder();

            argsBuilder.AddInputPipe(videoPipeName)
            .AddArg("thread_queue_size", 512)
            .AddArg("framerate", Args.FrameRate)
            .SetFormat("rawvideo")
            .AddArg("pix_fmt", nv12 ? "nv12" : "rgb32")
            .SetVideoSize(w, h);

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

            Args.VideoCodec.Apply(settings, Args, output);

            if (settings.Resize)
            {
                var width  = settings.ResizeWidth;
                var height = settings.ResizeHeight;

                if (width % 2 == 1)
                {
                    ++width;
                }

                if (height % 2 == 1)
                {
                    ++height;
                }

                output.AddArg("vf", $"scale={width}:{height}");
            }

            if (Args.AudioProvider != null)
            {
                var audioPipeName = GetPipeName();

                argsBuilder.AddInputPipe(audioPipeName)
                .AddArg("thread_queue_size", 512)
                .SetFormat("s16le")
                .SetAudioCodec("pcm_s16le")
                .SetAudioFrequency(Args.Frequency)
                .SetAudioChannels(Args.Channels);

                Args.VideoCodec.AudioArgsProvider(Args.AudioQuality, output);

                var wf = Args.AudioProvider.WaveFormat;

                _audioBytesPerFrame = (int)((1.0 / Args.FrameRate)
                                            * wf.SampleRate
                                            * wf.Channels
                                            * (wf.BitsPerSample / 8.0));

                // UpdatePeriod * Frequency * (Bytes per Second) * Channels * 2
                var audioBufferSize = (int)((1000.0 / Args.FrameRate) * 44.1 * 2 * 2 * 2);

                _audioPipe = new NamedPipeServerStream(audioPipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, audioBufferSize);
            }

            _ffmpegIn = new NamedPipeServerStream(videoPipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, _videoBuffer.Length);

            _ffmpegProcess = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), Args.FileName, out _);
        }