如何用ffmpeg库编码音频?

时间:2018-01-20 17:30:25

标签: ffmpeg

我试图重新制作doc/examples/transcoding.c以便编码opus音频。我怎么能这样做?

这就是我现在所拥有的:

            encoder = avcodec_find_encoder(AV_CODEC_ID_OPUS);

            if (!encoder) {
                av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
                return AVERROR_INVALIDDATA;
            }
            enc_ctx = avcodec_alloc_context3(encoder);
            if (!enc_ctx) {
                av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
                return AVERROR(ENOMEM);
            }

            enc_ctx->thread_count = 1;


            /* enc_ctx->sample_rate = dec_ctx->sample_rate; */
            enc_ctx->sample_rate = 48000;

            /* enc_ctx->channel_layout = dec_ctx->channel_layout; */
            enc_ctx->channel_layout = AV_CH_LAYOUT_MONO;


            enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
            /* take first format from list of supported formats */
            enc_ctx->sample_fmt = encoder->sample_fmts[0];
            enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};

            enc_ctx->bit_rate = 32000;

我应该转换为48000 Hz和带滤波器的单声道,还是编码器会自行计算出来?

如何将音频从一个数据包复制到另一个数据包? (看起来每个数据包应包含恰好20ms的音频,即960个样本)。这个页面https://ffmpeg.org/doxygen/trunk/structAVFrame.html说明了AVFrame.buf,如何从那里复制所有样本?

[libopus @ 02ec1d60] more samples than frame size (avcodec_encode_audio2)

0 个答案:

没有答案
相关问题