首页
学习
活动
专区
圈层
工具
发布

严格旗
EN

Stack Overflow用户
提问于 2017-07-07 19:32:06
回答 1查看 791关注 0票数 0

我正在开发使用libavcodec库的库。

我尝试用opus对音频帧进行编码,但是在avcodec_open2(.)之后。我拿到了这根木头

[opus @ 0x2335f30] The encoder 'opus' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.

在尝试av_fill_audio_frame(..)时也是如此我知道这个错误

Invalid argument

我的代码:

代码语言:javascript
复制
#include <libavcodec/avcodec.h>
#include "libavutil/opt.h"
#include <libavformat/avformat.h>
AVFrame *audio_frame = NULL;
AVPacket *audio_packet = NULL;
AVCodecContext *audio_encoder_codec_context = NULL;
AVCodec *audio_encoder_codec = NULL;

void init(){
    audio_frame = av_frame_alloc();
    audio_packet = av_packet_alloc();
    audio_encoder_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS);
    audio_encoder_codec_context = avcodec_alloc_context3(audio_encoder_codec);
    audio_encoder_codec_context->time_base = (AVRational){1,25};
    audio_encoder_codec_context->sample_rate = audio_sample_rate;
    audio_encoder_codec_context->sample_fmt = (enum AVSampleFormat) audio_sample_format == 0 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
    audio_encoder_codec_context->channels = 1;
    audio_encoder_codec_context->channel_layout = AV_CH_LAYOUT_MONO;
    audio_encoder_codec_context->codec_type = AVMEDIA_TYPE_AUDIO;
    audio_encoder_codec_context->extradata = NULL;
    avcodec_open2(audio_encoder_codec_context,audio_encoder_codec,NULL);
    audio_frame_size = av_samples_get_buffer_size(
            NULL,
            audio_encoder_codec_context->channels,
            audio_sample_rate,
            audio_encoder_codec_context->sample_fmt,
            1
    );
    audio_frame_buffer = (uint8_t*) av_malloc((audio_frame_size)*sizeof(uint8_t));
}

void encode(uint8_t *frame,int frame_size,uint8_t **packet, int *packet_size){
    memcpy(audio_frame_buffer, frame, (size_t) frame_size);
    av_init_packet(audio_packet);
    int isFin = 0;
    int r = avcodec_fill_audio_frame(audio_frame,audio_encoder_codec_context->channels,audio_encoder_codec_context->sample_fmt,audio_frame_buffer,audio_frame_size,0);
    printf("ERROR = %s",av_err2str(r));
    avcodec_encode_audio2(audio_encoder_codec_context,audio_packet,audio_frame,&isFin);
    *packet = audio_packet->data;
    *packet_size = audio_packet->size;
}

int main(){
    init();
    uint8_t *packet = NULL;
    int size = 0;
    encode(".....",44100,packet,size);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-28 20:55:11

invalid_argument错误可以通过在frame上设置nb_samples来解决--在设置这个错误之前,我也是这样做的。示例代码和输出:

代码语言:javascript
复制
AVFrame *frame = av_frame_alloc();
int ret = avcodec_fill_audio_frame(frame, channels, fmt, frame_buffer, frame_size, 0);
if (ret < 0)
{
    printf("ERROR = %s\n", av_err2str(ret));
}
else
{
    printf("Success (%i bytes required to allocate)\n", ret);
}

产量产出:

代码语言:javascript
复制
ERROR = Invalid argument

而在nb_samples设置正确的情况下:

代码语言:javascript
复制
AVFrame *frame = av_frame_alloc();
frame->nb_samples = 48000;
int ret = avcodec_fill_audio_frame(frame, channels, fmt, frame_buffer, frame_size, 0);
// etc...

在以下方面的成果:

代码语言:javascript
复制
Success (192000 bytes required to allocate)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44978438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档