Beispiel #1
0
        /// <summary>
        /// Creates a new instance of <see cref="FFMpegWriter"/>.
        /// </summary>
        /// <param name="FileName">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegWriter(string FileName, IImageProvider ImageProvider, int FrameRate,
                            int VideoQuality, FFMpegVideoArgsProvider VideoArgsProvider,
                            int AudioQuality, FFMpegAudioArgsProvider AudioArgsProvider,
                            IAudioProvider AudioProvider, int Frequency = 44100, int Channels = 2)
        {
            _videoBuffer = new byte[ImageProvider.Width * ImageProvider.Height * 4];

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

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

            string audioInArgs = "", audioOutArgs = "";

            if (AudioProvider != null)
            {
                audioInArgs  = $"-f s16le -acodec pcm_s16le -ar {Frequency} -ac {Channels} -i {pipePrefix}{audioPipeName}";
                audioOutArgs = AudioArgsProvider(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} \"{FileName}\"");
        }
Beispiel #2
0
 FFMpegItem(string Name, string Extension, FFMpegVideoArgsProvider VideoArgsProvider, FFMpegAudioArgsProvider AudioArgsProvider)
 {            
     this.Extension = Extension;
     _videoArgsProvider = VideoArgsProvider;
     _audioArgsProvider = AudioArgsProvider;
     _name = Name;
 }
 public FFMpegPostProcessingItem(string Name, string Extension, FFMpegVideoArgsProvider VideoArgsProvider, FFMpegAudioArgsProvider AudioArgsProvider)
 {
     _name = Name;
     _videoArgsProvider = VideoArgsProvider;
     _audioArgsProvider = AudioArgsProvider;
     this.Extension     = Extension;
 }
Beispiel #4
0
 FFMpegItem(string Name, Func <string> Extension, FFMpegVideoArgsProvider VideoArgsProvider, FFMpegAudioArgsProvider AudioArgsProvider)
 {
     _extension         = Extension;
     _videoArgsProvider = VideoArgsProvider;
     _audioArgsProvider = AudioArgsProvider;
     _name = Name;
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new instance of <see cref="FFMpegVideoWriter"/>.
        /// </summary>
        /// <param name="FileName">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegVideoWriter(string FileName, int FrameRate, int Quality, FFMpegVideoArgsProvider VideoArgsProvider)
        {
            _ffmpegProcess = new Process
            {
                StartInfo =
                {
                    FileName              = "ffmpeg.exe",
                    Arguments             = $"-r {FrameRate} -f image2pipe -i - {VideoArgsProvider(Quality)} \"{FileName}\"",
                    UseShellExecute       = false,
                    CreateNoWindow        = true,
                    RedirectStandardInput = true
                }
            };

            _ffmpegProcess.Start();

            _ffmpegIn = _ffmpegProcess.StandardInput.BaseStream;
        }
Beispiel #6
0
        /// <summary>
        /// Creates a new instance of <see cref="FFMpegMuxedWriter"/>.
        /// </summary>
        /// <param name="FilePath">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegMuxedWriter(string FilePath, int FrameRate, int VideoQuality, FFMpegVideoArgsProvider VideoArgsProvider, int AudioQuality, FFMpegAudioArgsProvider AudioArgsProvider, IAudioProvider AudioProvider)
        {
            if (AudioProvider == null)
            {
                throw new ArgumentNullException(nameof(AudioProvider), $"{nameof(AudioProvider)} can't be null. Use {nameof(FFMpegVideoWriter)} instead.");
            }

            if (!Directory.Exists(BaseDir))
            {
                Directory.CreateDirectory(BaseDir);
            }

            var fileName = Path.GetFileName(FilePath);

            tempVideoPath = Path.Combine(BaseDir, fileName);
            tempAudioPath = Path.Combine(BaseDir, Path.ChangeExtension(fileName, ".wav"));

            _audioWriter = new AudioFileWriter(tempAudioPath, AudioProvider.WaveFormat);

            _videoWriter = new FFMpegVideoWriter(tempVideoPath, FrameRate, VideoQuality, VideoArgsProvider);

            _ffmpegArgs = $"-i {tempVideoPath} -vcodec copy -i {tempAudioPath} {AudioArgsProvider(AudioQuality)} \"{FilePath}\"";
        }
Beispiel #7
0
 FFMpegItem(string Name, Func <string> Extension, FFMpegVideoArgsProvider VideoArgsProvider, FFMpegAudioArgsProvider AudioArgsProvider)
     : this(Name, Extension)
 {
     _videoArgsProvider = VideoArgsProvider;
     _audioArgsProvider = AudioArgsProvider;
 }
Beispiel #8
0
 public static FFMpegVideoWriterArgs FromVideoWriterArgs(VideoWriterArgs Args, FFMpegVideoArgsProvider VideoArgsProvider, FFMpegAudioArgsProvider AudioArgsProvider)
 {
     return(new FFMpegVideoWriterArgs
     {
         FileName = Args.FileName,
         ImageProvider = Args.ImageProvider,
         FrameRate = Args.FrameRate,
         VideoQuality = Args.VideoQuality,
         VideoArgsProvider = VideoArgsProvider,
         AudioQuality = Args.AudioQuality,
         AudioArgsProvider = AudioArgsProvider,
         AudioProvider = Args.AudioProvider
     });
 }
Beispiel #9
0
        /// <summary>
        /// Creates a new instance of <see cref="FFMpegVideoWriter"/>.
        /// </summary>
        /// <param name="FileName">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegVideoWriter(string FileName, IImageProvider ImageProvider, int FrameRate, int Quality, FFMpegVideoArgsProvider VideoArgsProvider)
        {
            // x264 requires video dimensions to be even.
            if (VideoArgsProvider == FFMpegItem.x264 || VideoArgsProvider == FFMpegItem.x265)
            {
                int h = ImageProvider.Height,
                    w = ImageProvider.Width;

                if (h % 2 == 1)
                {
                    --h;
                }

                if (w % 2 == 1)
                {
                    --w;
                }

                if (h != ImageProvider.Height || w != ImageProvider.Width)
                {
                    resize = new Rectangle(Point.Empty, new Size(w, h));
                }
            }

            _videoBuffer = new byte[(resize?.Width ?? ImageProvider.Width) * (resize?.Height ?? ImageProvider.Height) * 4];

            _ffmpegProcess = new Process
            {
                StartInfo =
                {
                    FileName              = ServiceProvider.FFMpegExePath,
                    Arguments             = $"-hide_banner -framerate {FrameRate} -f rawvideo -pix_fmt rgb32 -video_size {(resize?.Width ?? ImageProvider.Width)}x{(resize?.Height ?? ImageProvider.Height)} -i - {VideoArgsProvider(Quality)} -r {FrameRate} \"{FileName}\"",
                    UseShellExecute       = false,
                    CreateNoWindow        = true,
                    RedirectStandardInput = true
                }
            };

            _ffmpegProcess.Exited += (s, e) => _error = _ffmpegProcess.ExitCode != 0;

            _ffmpegProcess.Start();

            _ffmpegIn = _ffmpegProcess.StandardInput.BaseStream;
        }