FFMpeg avformat_write_header 始终返回 < 0

问题描述 投票:0回答:1

我们已要求一名自由职业者使用 FFMPeg for 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;
}
ios iphone ffmpeg video-processing video-encoding
1个回答
3
投票

如果 avformat_write_header 返回 < 0, The problem could be one of these

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

我建议您在调试模式下运行。它将显示详细的错误消息。添加以下代码,您可以确保它在 FFMPEG 代码运行之前运行。

av_log_set_level(AV_LOG_DEBUG);

我相信你会发现关于AVFormatContext的错误信息。

© www.soinside.com 2019 - 2024. All rights reserved.