AVFoundation视频导出-无原因错误[Objective-C]

AVFoundation视频导出-无原因错误[Objective-C],objective-c,macos,video,export,avfoundation,Objective C,Macos,Video,Export,Avfoundation,我正在尝试用AVFoundation为视频彩色应用程序编码视频(你可以查看)。导出编解码器将是H264或ProRes或HEVC(取决于用户选择),如果这有助于解决我的问题 我已经根据答案编写了代码: 这段代码来自github: 下面是我得到的(有一些外部变量和函数调用,但我认为发生的事情非常清楚): 我在互联网上到处寻找这个错误代码的含义,但这毫无用处 我在这里使用AVFoundation有什么严重问题吗?? 我也尝试过切换到8位RGB帧,但没有解决问题。并非所有列出的像素格式都得到了框架的支持

我正在尝试用AVFoundation为视频彩色应用程序编码视频(你可以查看)。导出编解码器将是H264或ProRes或HEVC(取决于用户选择),如果这有助于解决我的问题

我已经根据答案编写了代码:

这段代码来自github:

下面是我得到的(有一些外部变量和函数调用,但我认为发生的事情非常清楚):

我在互联网上到处寻找这个错误代码的含义,但这毫无用处

我在这里使用AVFoundation有什么严重问题吗??
我也尝试过切换到8位RGB帧,但没有解决问题。

并非所有列出的像素格式都得到了框架的支持。您所选的不受支持。坚持标准格式,如kCVPixelFormatType_32ARGB、kCVPixelFormatType_422YpCbCr8等

kVTPixelTransferNotSupportedErr=-12905
通过

关于核心视频支持的像素格式的技术问答:

请注意,您可能希望运行代码以查找给定OS版本上支持的格式的最新列表。此外,并非每个框架中的每个路径都支持每种格式。换句话说,VideoToolbox可以在像素格式之间进行转换,它可能不支持格式X,即使您可以以该格式创建CVPixelBuffer

在您的情况下,请尝试
kCVPixelFormatType\u 4444AYpCbCr16
kCVPixelFormatType\u 422YpCbCr16
kCVPixelFormatType\u 422YpCbCr10
kCVPixelFormatType\u 64ARGB
,因为它们至少在解码端受支持:

并非所有列出的像素格式都受到框架的实际支持。您所选的不受支持。坚持标准格式,如kCVPixelFormatType_32ARGB、kCVPixelFormatType_422YpCbCr8等

kVTPixelTransferNotSupportedErr=-12905
通过

关于核心视频支持的像素格式的技术问答:

请注意,您可能希望运行代码以查找给定OS版本上支持的格式的最新列表。此外,并非每个框架中的每个路径都支持每种格式。换句话说,VideoToolbox可以在像素格式之间进行转换,它可能不支持格式X,即使您可以以该格式创建CVPixelBuffer

在您的情况下,请尝试
kCVPixelFormatType\u 4444AYpCbCr16
kCVPixelFormatType\u 422YpCbCr16
kCVPixelFormatType\u 422YpCbCr10
kCVPixelFormatType\u 64ARGB
,因为它们至少在解码端受支持:

猜测:可能
AVFoundation
在执行RGB时喜欢RGBA,或者像素缓冲区属性需要指定
kCVPixelBufferPixelFormatTypeKey
。一个可运行的代码片段在这里会有很大帮助。猜测:可能是
AVFoundation
在执行RGB时喜欢RGBA,或者可能像素缓冲区属性需要指定
kcvpixelbufferpixelformatypeKey
。一个可运行的代码片段在这里会有很大帮助。谢谢!事实上,我几天前意识到有类似的事情发生,只是不记得更新答案。它现在可以使用8位RGB,但这并没有利用prores 4444编解码器中的10位功能,你知道有任何像素格式可以让我向其提供16位数据吗?再次看到答案。以前有吗?如果是这样,我很抱歉错过了。我在打电话。非常感谢你的建议。谢谢!事实上,我几天前意识到有类似的事情发生,只是不记得更新答案。它现在可以使用8位RGB,但这并没有利用prores 4444编解码器中的10位功能,你知道有任何像素格式可以让我向其提供16位数据吗?再次看到答案。以前有吗?如果是这样,我很抱歉错过了。我在打电话。非常感谢你的建议。
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, %d\n", (int)f, j);
            while(!adaptor.assetWriterInput.readyForMoreMediaData) [NSThread sleepForTimeInterval:0.0001];
        }
        j++;
    }
    if (!append_ok) {
        printf("error appending image %d times %d\n, 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)\""; }.