ios - FFmpeg API iOS "Resource temporarily unavailable"

标签 ios objective-c ffmpeg

我花了几个小时试图解决这个问题:

我正在尝试在 iOS 上使用 ffmpeg api。我的 Xcode 项目正在构建,我可以调用 ffmpeg api 函数。我正在尝试编写解码视频的代码(暂时不输出任何内容),但我不断收到错误 -35:“资源暂时不可用”。

输入文件来自相机胶卷 (.mov),我使用 Mpeg-4 进行解码。我目前所做的只是从文件中获取数据、解析它并将解析后的数据包发送到解码器。当我尝试获取帧时,我得到的只是一个错误。有谁知道我做错了什么?

+(void)test: (NSString*)filename outfile:(NSString*)outfilename {

/* register all the codecs */
avcodec_register_all();

AVCodec *codec;
AVCodecParserContext *parser;
AVCodecContext *c= NULL;
int frame_count;
FILE* f;
AVFrame* frame;
AVPacket* avpkt;
avpkt = av_packet_alloc();
//av_init_packet(avpkt);
char buf[1024];

uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
uint8_t *data;
size_t   data_size;

/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

printf("Decode video file %s to %s\n", [filename cStringUsingEncoding:NSUTF8StringEncoding], [outfilename cStringUsingEncoding:NSUTF8StringEncoding]);
/* find the h264 video decoder */
codec = avcodec_find_decoder(AV_CODEC_ID_MPEG4);
if (!codec) {
    fprintf(stderr, "Codec not found\n");
    exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
    fprintf(stderr, "Could not allocate video codec context\n");
    exit(1);
}
if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
    c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
/* For some codecs, such as msmpeg4 and mpeg4, width and height
 MUST be initialized there because this information is not
 available in the bitstream. */
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
    fprintf(stderr, "Could not open codec\n");
    exit(1);
}
f = fopen([filename cStringUsingEncoding:NSUTF8StringEncoding], "rb");
if (!f) {
    fprintf(stderr, "Could not open %s\n", [filename cStringUsingEncoding:NSUTF8StringEncoding]);
    exit(1);
}
frame = av_frame_alloc();
if (!frame) {
    fprintf(stderr, "Could not allocate video frame\n");
    exit(1);
}
frame_count = 0;

parser = av_parser_init(codec->id);
if (!parser) {
    fprintf(stderr, "parser not found\n");
    exit(1);
}

while (!feof(f)) {
    /* read raw data from the input file */
    data_size = fread(inbuf, 1, INBUF_SIZE, f);
    if (!data_size)
        break;
    /* use the parser to split the data into frames */
    data = inbuf;
    while (data_size > 0) {
        int ret = av_parser_parse2(parser, c, &avpkt->data, &avpkt->size, data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
        if (ret < 0) {
            fprintf(stderr, "Error while parsing\n");
            exit(1);
        }
        data      += ret;
        data_size -= ret;
        if (avpkt->size){
            char buf[1024];

            ret = avcodec_send_packet(c, avpkt);
            if (ret < 0) {

                fprintf(stderr, "Error sending a packet for decoding\n");
                continue;
                exit(1);
            }

            while (ret >= 0) {
                ret = avcodec_receive_frame(c, frame);
                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
                    char e [1024];
                    av_strerror(ret, e, 1024);
                    fprintf(stderr, "Fail: %s !\n", e);
// ~~~~~~~~ This is where my program exits ~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    return;
                }
                else if (ret < 0) {
                    fprintf(stderr, "Error during decoding\n");
                    exit(1);
                }
            }


        }
    }
}
/* some codecs, such as MPEG, transmit the I and P frame with a
 latency of one frame. You must do the following to have a
 chance to get the last frame of the video */

fclose(f);
avcodec_close(c);
av_free(c);
av_frame_free(&frame);
printf("\n");

最佳答案

AVERROR(EAGAIN) 不是错误,它只是表示输出尚不可用,您需要在第一次输出之前再调用几次 _send_packet()将从 _receive_frame() 获得。

输出被缓冲(延迟)以允许 B 帧和帧线程。

关于ios - FFmpeg API iOS "Resource temporarily unavailable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615831/

相关文章:

ios - 在 iOS 中比较两个频谱图

c# - FFmpeg 文件转换格式和文件扩展名

iphone - iOS -- 如何保存应用内购买数据?

ios - 如何在 Objective-C for iPhone 中录制用户语音

ios - 不使用任何 Apple API 加载 GLSL 着色器

ios - 架构 i386 的 undefined symbol - 来自 podfile 的所有项目

ios - 使用不兼容类型 float 的表达式初始化 "breuk *__strong"

ffmpeg - 优酷直播

audio - 使用 FFmpeg 将音频文件拆分为等长的片段

ios - 我如何摆脱这个警告