ffmpeg资源暂时不可用

时间:2017-09-29 22:13:49

标签: c ffmpeg libav opus

我尝试使用ffmpeg库和Opus编解码器对音频帧进行编码 但我遇到了这个错误:

Resource temporarily unavailable

我的源代码:

void encode_audio(uint8_t *frame , int frame_size , void (*onPacket)(uint8_t *packet , int packet_size)){
if(audio_encoder_codec_context != NULL){
    memset(audio_encoder_frame_buffer , 0 , (size_t) audio_encoder_frame_size);
    swr_convert(
            s16_to_flt_resampler,
            &audio_encoder_frame_buffer,
            audio_encoder_frame_size,
            (const uint8_t **) &frame,
            frame_size
    );
    int result = avcodec_send_frame(audio_encoder_codec_context , audio_encoder_frame);
    while(result >= 0){
        result = avcodec_receive_packet(audio_encoder_codec_context , audio_encoder_packet);
        char *a = malloc(1024);
        av_strerror(result , a , 1024);
        printf("%s\n",a);
        if (result == AVERROR(EAGAIN) || result == AVERROR_EOF || result < 0){
            break;
        }
        onPacket(audio_encoder_packet->data , audio_encoder_packet->size);
        av_packet_unref(audio_encoder_packet);
    }
}
}

1 个答案:

答案 0 :(得分:0)

这来自AVERROR(EAGAIN)

当返回 AVERROR(EAGAIN) 时,您应该发送更多帧。

文档针对 avcodec_receive_packet

说明了这一点

avcodec.h:

/**
 * Read encoded data from the encoder.
 *
 * @param avctx codec context
 * @param avpkt This will be set to a reference-counted packet allocated by the
 *              encoder. Note that the function will always call
 *              av_packet_unref(avpkt) before doing anything else.
 * @return 0 on success, otherwise negative error code:
 *      AVERROR(EAGAIN):   output is not available in the current state - user
 *                         must try to send input
 *      AVERROR_EOF:       the encoder has been fully flushed, and there will be
 *                         no more output packets
 *      AVERROR(EINVAL):   codec not opened, or it is a decoder
 *      other errors: legitimate encoding errors
 */
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);

根据数据,FFmpeg 可能需要额外的输入/数据才能产生输出。

来自文档的更多信息:

AVERROR(EAGAIN): output is not available in the current state - user must try to send input.

继续给它数据直到你得到返回码 0(成功)。