ffmpeg 资源暂时不可用

5
我将尝试使用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);
    }
}
}

你找到解决方法了吗? 我遇到了一个类似的问题,即将从Opus解码的帧编码为AAC。 - zevarito
我使用的是 Ffmpeg 3.1.2,但无法解决问题,您可以尝试使用 Ffmpeg 4.0,或许能够解决该问题。 - KoLiBer
这里也出现了同样的错误,你是怎么解决的? - datwelk
1个回答

1

这是来自 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):在当前状态下无法获得输出-用户必须尝试发送输入。
继续提供数据,直到获得返回代码0(成功)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接