/// <inheritdoc />
        public void Dispose()
        {
            _ffMpegWriter.Dispose();

            var argsBuilder = new FFmpegArgsBuilder();

            argsBuilder.AddInputFile(_tempFileName);

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

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

            output.AddArg(_args.OutputArgs);

            var process = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), _args.FileName);

            process.WaitForExit();

            File.Delete(_tempFileName);
        }
示例#2
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];
            frameCount   = 0;
            _keyVector   = Args.keyVector;
            _keyVector.CreateKeyOutputFile();

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

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

            var videoInArgs  = $"-thread_queue_size 512 -use_wallclock_as_timestamps 1 -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.Resize)
            {
                var width  = settings.ResizeWidth;
                var height = settings.ResizeHeight;

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

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

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

            string audioInArgs = "", audioOutArgs = "";

            if (Args.AudioProvider != null)
            {
                audioInArgs  = $"-thread_queue_size 512 -f s16le -acodec pcm_s16le -ar {Args.Frequency} -ac {Args.Channels} -i {PipePrefix}{audioPipeName}";
                audioOutArgs = 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);

            _ffmpegProcess = FFmpegService.StartFFmpeg($"{videoInArgs} {audioInArgs} {videoOutArgs} {audioOutArgs} {Args.OutputArgs} \"{Args.FileName}\"", Args.FileName);
        }
示例#3
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);
        }
        /// <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);
        }
示例#5
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);
        }
示例#6
0
        /// <summary>
        /// Creates a new instance of <see cref="FFmpegWriter"/>.
        /// </summary>
        public FFmpegWriter(FFmpegVideoWriterArgs Args)
        {
            var settings = ServiceProvider.Get <FFmpegSettings>();

            VideoInputArgs = Args;
            var crf = (51 * (100 - VideoInputArgs.VideoQuality)) / 99;

            outputFolderName             = Args.FileName.Substring(0, VideoInputArgs.FileName.Length - 4);
            additionalVideoInputArgsPre  = " -r " + Args.FrameRate + " -start_number 1 -i \"";
            additionalVideoInputArgsPost = "_%d.png\" -s " + Args.ImageProvider.Width + "*" + Args.ImageProvider.Height + " -vcodec libx264 -crf " + crf + " -pix_fmt " + settings.X264.PixelFormat + " -preset " + settings.X264.Preset;
            if (settings.Resize)
            {
                additionalVideoInputArgsPost += " -vf scale=" + settings.ResizeWidth + ":" + settings.ResizeHeight;
            }

            if (File.Exists(capturePropertiesPath))
            {
                foreach (string row in File.ReadAllLines(capturePropertiesPath))
                {
                    string[] rowValues = row.Split('=');
                    if (!row.StartsWith("#") && rowValues.Length == 2)
                    {
                        if (rowValues[0].Trim() == "PIPE_TIMEOUT_IN_SECONDS")
                        {
                            try
                            {
                                waitTimeForPipeConnection = int.Parse(rowValues[1].Trim()) * 1000;
                                if (waitTimeForPipeConnection != 30000)
                                {
                                    WriteLog("Wait Timeout: " + waitTimeForPipeConnection);
                                }
                            }
                            catch (Exception) {  }
                        }
                    }
                }
            }

            if (settings.RawBackup)
            {
                framesToBeWritten = new Queue <byte[]>();
                (new Thread(ThreadForAppendFrames)).Start();
            }

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