我们已要求一名自由职业者使用 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/