ios - FFMpeg avformat_write_header 总是返回 < 0

标签 ios iphone ffmpeg video-processing video-encoding

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

据我了解,我们正在尝试创建一个输出文件并为其创建一个 header ,但是 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;
}

最佳答案

如果 avformat_write_header 返回 < 0,问题可能是以下之一:

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

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

av_log_set_level(AV_LOG_DEBUG);

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

关于ios - FFMpeg avformat_write_header 总是返回 < 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193445/

相关文章:

c# - 如何将网络摄像机视频流转换为视频文件?

ios - 与我的开发人员共享我的 iTunes Connect App-Specific Shared Secret 是否安全?

iphone - 使用多个 window.location.href 调用触发 shouldStartLoadWithRequest

ios - 在 iOS 中获取用户封面图片的 facebook 图 url 是什么

iphone - 选项卡式导航 View 的正确设计模式?

iphone - 如何在 UIView 中不滚动显示多行文本?

ffmpeg - 当有多个drawtext目标时,如何编写函数 "avfilter_graph_send_command"(ffmpeg)的*args?

php - 首先使用php从视频中捕获缩略图?

ios - 选择器中的 Swift 传递参数

iOS - 使用 AVFoundation Framework 从相机模糊预览