Пример #1
0
        /// <summary>
        /// Creates a new instance of <see cref="FFmpegWriter"/>.
        /// </summary>
        public FFmpegWriter(FFmpegVideoWriterArgs args)
        {
            var settings = ServiceProvider.Get <FFmpegSettings>();

            _videoBuffer = new byte[args.ImageProvider.Width * args.ImageProvider.Height * 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 rgb32")
            .SetVideoSize(args.ImageProvider.Width, args.ImageProvider.Height);

            var output = argsBuilder.AddOutputFile(args.FileName)
                         .AddArg(args.VideoArgsProvider(args.VideoQuality))
                         .SetFrameRate(args.FrameRate);

            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);

                output.AddArg(args.AudioArgsProvider(args.AudioQuality));

                // 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);

            output.AddArg(args.OutputArgs);

            _ffmpegProcess = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), args.FileName);
        }