FFMpeg avformat_write_header始终返回< 0

时间:2015-10-18 01:41:30

标签: ios iphone ffmpeg video-processing video-encoding

我们已经要求自由职业者使用FFMPeg为iOS构建视频编码器,但是有一个错误并且自由职业者不再可用。我对FFMpeg和视频编码缺乏经验,我正在尝试调试此错误。

据我所知,我们正在尝试创建输出文件并为其创建标题,但是,avformat_write_header始终小于零。如果我发表评论,它就不起作用

- (BOOL) writeHeaderWithError:(NSError *__autoreleasing *)error {
    AVDictionary *options = NULL;

    // Write header for output file
    int writeHeaderValue = avformat_write_header(self.formatContext, &options);
    if (writeHeaderValue < 0) {
        if (error != NULL) {
            *error = [FFUtilities errorForAVError:writeHeaderValue];
        }
        av_dict_free(&options);
        return NO;
    }
    av_dict_free(&options);
    return YES;
}

以下是我们如何实例化FFOutputFile的一些相关代码

    - (AVFormatContext*) formatContextForOutputPath:(NSString*)outputPath options:(NSDictionary*)options {
    AVFormatContext *outputFormatContext = NULL;
    NSString *outputFormatString = [options objectForKey:kFFmpegOutputFormatKey];

    int openOutputValue = avformat_alloc_output_context2(&outputFormatContext, NULL, [outputFormatString UTF8String], [outputPath UTF8String]);
    if (openOutputValue < 0) {
        avformat_free_context(outputFormatContext);
        return nil;
    }
    return outputFormatContext;
}

- (void) addOutputStream:(FFOutputStream*)outputStream {
    [self.streams addObject:outputStream];
}

- (id) initWithPath:(NSString *)path options:(NSDictionary *)options {
    if (self = [super initWithPath:path options:options]) {
        self.formatContext = [self formatContextForOutputPath:path options:options];
        self.streams = [NSMutableArray array];
        self.bitstreamFilters = [NSMutableSet set];
    }
    return self;
}

1 个答案:

答案 0 :(得分:1)

如果avformat_write_header返回&lt; 0,问题可能是其中之一:

  • 无效的编解码器选项(不支持选项/无效选项)
  • 无效的格式选项(它很少发生在上面)
  • 使用未安装的编解码器(在这种情况下,avcodec_find_encoder / avcodec_open2应该首先返回&lt; 0)

我建议您在调试模式下运行代码。它将通过标准错误消息打印您的上下文中无效的内容。您应该将它添加到可以确保它在FFMPEG代码运行之前运行的位置。

av_log_set_level(AV_LOG_DEBUG);

如果你很幸运,你会发现AVFormatContext出了什么问题。