Пример #1
0
        internal VideoEncoder(AVStream *stream, VideoFormat format, VideoEncoderParameters encoderParams = null) : base(stream)
        {
            encoderParams = encoderParams ?? VideoEncoderParameters.Default;

            InFormat = format;
            Init(encoderParams);
        }
Пример #2
0
        private void Init(VideoEncoderParameters encoderParams)
        {
            var pixelFormats = GetSupportedPixelFormats(codec);

            if (pixelFormats != null && !pixelFormats.Contains(InFormat.PixelFormat))
            {
                outFormat = new VideoFormat(InFormat.Width, InFormat.Height, pixelFormats[0]);
                resampler = new VideoResampler(InFormat, OutFormat, encoderParams.ResampleFlags);
                tempFrame = new VideoFrame();
            }
            else
            {
                outFormat = InFormat;
            }

            framePerSecond = encoderParams.FrameRate;

            try {
                codecContext->PixFmt    = OutFormat.PixelFormat;
                codecContext->Width     = OutFormat.Width;
                codecContext->Height    = OutFormat.Height;
                codecContext->BitRate   = encoderParams.BitRate.Value;
                codecContext->TimeBase  = encoderParams.FrameRate.Reciprocal;
                codecContext->Framerate = encoderParams.FrameRate;
                if (encoderParams.GopSize != 0)
                {
                    codecContext->GopSize = encoderParams.GopSize;
                }
                if (encoderParams.MaxBFrames != 0)
                {
                    codecContext->MaxBFrames = encoderParams.MaxBFrames;
                }
                if (encoderParams.MbDecision != 0)
                {
                    codecContext->MbDecision = encoderParams.MbDecision;
                }
                if (encoderParams.Qmin != 0)
                {
                    codecContext->Qmin = encoderParams.Qmin;
                }
                if (encoderParams.Qmax != 0)
                {
                    codecContext->Qmax = encoderParams.Qmax;
                }

                var result = FF.avcodec_open2(codecContext, codec, null);
                if (result < 0)
                {
                    throw new CSharp.FFmpegException(result);
                }
            } catch {
                Dispose();
                throw;
            }
        }
Пример #3
0
        public VideoEncoder(AVCodecID codecID, VideoFormat format, VideoEncoderParameters encoderParams = null) : base(codecID)
        {
            encoderParams = encoderParams ?? VideoEncoderParameters.Default;
            if (codec->Type != AVMediaType.Video)
            {
                throw new ArgumentException($"{codecID}不是视频格式", nameof(codecID));
            }

            codecContext = FF.avcodec_alloc_context3(codec);
            if (codecContext == null)
            {
                throw new Exception("无法分配编码器上下文");
            }

            InFormat = format;
            Init(encoderParams);
        }
Пример #4
0
        public MediaWriter AddVideo(VideoFormat format, VideoEncoderParameters encoderParams = null)
        {
            if (readyEncoders != null)
            {
                throw new InvalidOperationException($"该{nameof(MediaWriter)}对象已经初始化");
            }
            if (outputFormat == null)
            {
                throw new InvalidOperationException("无法确定媒体的输出格式");
            }

            if (outputFormat->VideoCodec == AVCodecID.None)
            {
                throw new InvalidOperationException($"该{nameof(MediaWriter)}对象并不支持视频");
            }

            var stream = FF.avformat_new_stream(formatContext, Codec.GetEncoder(outputFormat->VideoCodec));

            if (stream == null)
            {
                throw new InvalidOperationException("无法创建流");
            }
            var codecContext = stream->Codec;
            var videoEncoder = new VideoEncoder(stream, format, encoderParams);

            stream->TimeBase     = codecContext->TimeBase;
            stream->AvgFrameRate = codecContext->Framerate;
            int result = FF.avcodec_parameters_from_context(stream->Codecpar, codecContext);

            if (result < 0)
            {
                throw new FFmpegException(result);
            }
            encoders.Add(videoEncoder);
            return(this);
        }