我正在尝试使用AVFoundation为视频颜色应用程序编码视频(您可以在此处查看(。导出编解码器将是 H264 或 ProRes 或 HEVC(取决于用户选择(,如果这对帮助解决我的问题有任何用处。
我根据答案编写了我的代码:使用 AVAsset 制作带有图片数组和歌曲文件的电影文件
来自github的这段代码:https://github.com/caferrara/img-to-video/blob/master/img-to-video/ViewController.m#L29
这是我得到的(有一些外部变量和函数调用,但我认为很清楚发生了什么(:
NSURL * outURL = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%s", path]];
NSLog(outURL.absoluteString);
NSError * error = nil;
AVAssetWriter * videoWriter = [[AVAssetWriter alloc] initWithURL: outURL
fileType:AVFileTypeQuickTimeMovie
error:&error];
NSDictionary * videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
encoder->codec, AVVideoCodecKey,
[NSNumber numberWithInt:encoder->width], AVVideoWidthKey,
[NSNumber numberWithInt:encoder->height], AVVideoHeightKey,
nil ];
AVAssetWriterInput * videoWriterInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
AVAssetWriterInputPixelBufferAdaptor * adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:nil];
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
//Start a session:
[videoWriter startWriting];
NSLog(@"Write Started");
[videoWriter startSessionAtSourceTime:kCMTimeZero];
//Video encoding
CVPixelBufferRef buffer = NULL;
setMlvAlwaysUseAmaze(App->videoMLV);
for(uint64_t f = 0; f < getMlvFrames(App->videoMLV); ++f)
{
getMlvProcessedFrame16(App->videoMLV, f, encoder->data);
CVReturn success = CVPixelBufferCreateWithBytes( kCFAllocatorDefault,
encoder->width,
encoder->height,
kCVPixelFormatType_48RGB,
encoder->data,
sizeof(uint16_t) * encoder->width * 3,
NULL,
NULL,
NULL,
&buffer );
if (success != kCVReturnSuccess || buffer == NULL) NSLog(@"Failed to create pixel buffer.");
NSDictionary * colour_attachment = @{(id)kCVImageBufferICCProfileKey : (id)encoder->colour_profile_data};
CVBufferSetAttachments(buffer, (CFDictionaryRef)colour_attachment, kCVAttachmentMode_ShouldPropagate);
BOOL append_ok = NO;
int j = 0;
while (!append_ok && j < 30) {
if (adaptor.assetWriterInput.readyForMoreMediaData) {
//print out status:
NSLog(@"Processing video frame (%d, attempt %d)", (int)f, j);
CMTime frameTime = CMTimeMake(f * 5000.0, (int32_t)(encoder->fps * 1000.0));
append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
if(!append_ok){
NSError *error = videoWriter.error;
if(error!=nil) {
NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
}
}
while(!adaptor.assetWriterInput.readyForMoreMediaData) [NSThread sleepForTimeInterval:0.0001];
}
else {
printf("adaptor not ready %d, %dn", (int)f, j);
while(!adaptor.assetWriterInput.readyForMoreMediaData) [NSThread sleepForTimeInterval:0.0001];
}
j++;
}
if (!append_ok) {
printf("error appending image %d times %dn, with error.", (int)f, j);
}
}
[videoWriterInput markAsFinished];
[videoWriter finishWriting];
[videoWriterInput release];
[videoWriter release];
NSLog(@"Write Ended");
但是,每当我尝试运行它时,它都会打印以下错误:
2017-11-15 20:54:40.532 MLV App[21801:3488295] Unresolved error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12905), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x7fd446adeb70 {Error Domain=NSOSStatusErrorDomain Code=-12905 "(null)"}},{
NSLocalizedDescription = "The operation could not be completed";
NSLocalizedFailureReason = "An unknown error occurred (-12905)";
NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12905 "(null)""; }.
我在互联网上广泛寻找此错误代码的含义,但一直没有用。
我在这里使用 AVFoundation 有什么大问题吗?我也尝试切换到 8 位 RGB 帧,但没有解决问题。
并非列出的每种像素格式实际上都受框架支持。您选择的那个不受支持。坚持使用标准格式,如kCVPixelFormatType_32ARGB、kCVPixelFormatType_422YpCbCr8等。
kVTPixelTransferNotSupportedErr = -12905
通过 https://www.osstatus.com/
关于Core Video支持的像素格式的技术问答:https://developer.apple.com/library/content/qa/qa1501/_index.html
请注意,您可能需要运行代码以查找给定操作系统版本上受支持格式的最新列表。此外,并非每个框架中的每个路径都支持每种格式。换句话说,在像素格式之间进行转换的VideoToolbox可能不支持X格式,即使您可以创建该格式的CVPixelBuffer。
在您的情况下,请尝试kCVPixelFormatType_4444AYpCbCr16
、kCVPixelFormatType_422YpCbCr16
、kCVPixelFormatType_422YpCbCr10
或kCVPixelFormatType_64ARGB
,因为它们至少在解码端受支持:https://developer.apple.com/documentation/avfoundation/avassetreadertrackoutput