Example #1
0
        public MediaWriter AddEncoder(Encoder encoder)
        {
            if (readyEncoders != null)
            {
                throw new InvalidOperationException($"该{nameof(MediaWriter)}对象已经初始化");
            }

            if (outputFormat == null)
            {
                var desc = FF.avcodec_descriptor_get(encoder.ID);
                if (desc == null)
                {
                    throw new InvalidOperationException("无法获得编码器的短名称描述");
                }
                outputFormat = FF.av_guess_format(desc->Name, null, null);
                if (outputFormat == null)
                {
                    throw new InvalidOperationException("无法确定媒体的输出格式");
                }
                formatContext->Oformat = outputFormat;
            }

            var stream = FF.avformat_new_stream(formatContext, encoder.codec);

            if (stream == null)
            {
                throw new InvalidOperationException("无法创建流");
            }
            // stream->TimeBase = encoder.codecContext->TimeBase;
            int result = FF.avcodec_parameters_from_context(stream->Codecpar, encoder.codecContext);

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