Example #1
0
        /// <summary>
        /// Creates a new instance of <see cref="FFMpegWriter"/>.
        /// </summary>
        public FFMpegWriter(FFMpegVideoWriterArgs Args)
        {
            var settings = ServiceProvider.Get <Settings>();

            _videoBuffer = new byte[Args.ImageProvider.Width * Args.ImageProvider.Height * 4];

            var audioPipeName = GetPipeName();
            var videoPipeName = GetPipeName();

            var videoInArgs  = $"-framerate {Args.FrameRate} -f rawvideo -pix_fmt rgb32 -video_size {Args.ImageProvider.Width}x{Args.ImageProvider.Height} -i {PipePrefix}{videoPipeName}";
            var videoOutArgs = $"{Args.VideoArgsProvider(Args.VideoQuality)} -r {Args.FrameRate}";

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

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

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

                videoOutArgs += $" -vf scale={width}:{height}";
            }

            string audioInArgs = "", audioOutArgs = "";

            if (Args.AudioProvider != null)
            {
                audioInArgs  = $"-f s16le -acodec pcm_s16le -ar {Args.Frequency} -ac {Args.Channels} -i {PipePrefix}{audioPipeName}";
                audioOutArgs = Args.AudioArgsProvider(Args.AudioQuality);

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

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

            _ffmpegProcess = FFMpegService.StartFFMpeg($"{videoInArgs} {audioInArgs} {videoOutArgs} {audioOutArgs} {Args.OutputArgs} \"{Args.FileName}\"", Args.FileName);
        }
Example #2
0
        /// <inheritdoc />
        public void Dispose()
        {
            _ffMpegWriter.Dispose();

            var videoInArgs  = $"-i \"{_tempFileName}\"";
            var videoOutArgs = $"{_args.VideoArgsProvider(_args.VideoQuality)} -r {_args.FrameRate}";

            var audioOutArgs = "";

            if (_args.AudioProvider != null)
            {
                audioOutArgs = _args.AudioArgsProvider(_args.AudioQuality);
            }

            var process = FFMpegService.StartFFMpeg($"{videoInArgs} {videoOutArgs} {audioOutArgs} {_args.OutputArgs} \"{_args.FileName}\"", _args.FileName);

            process.WaitForExit();

            File.Delete(_tempFileName);
        }