AVAssetWriter是否因H264编解码器而失败?

时间:2016-01-18 00:05:18

标签: avfoundation avassetwriter

我已经使用AppleProRes422编解码器和AVAssetWriter获得了以下代码。

但是,如果我使用H264编解码器,代码会在适配器上出现NSError -12348失败。

我感到困惑。

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:videoOutputPath] fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];
NSParameterAssert(videoWriter);

NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
                                nil];

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecAppleProRes422, AVVideoCodecKey,
                               //AVVideoCodecH264, AVVideoCodecKey, // I use EITHER this key or the ProRes one
                               [NSNumber numberWithInt:imageSize.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:imageSize.height], AVVideoHeightKey,
                               nil];

AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
                                        assetWriterInputWithMediaType:AVMediaTypeVideo
                                        outputSettings:videoSettings];


AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                 sourcePixelBufferAttributes:outputSettings];

稍后,我从输入文件中获取原始像素数据(BGR-24 AVI文件已被转换为BGRA-32数据),如果我尝试了-12348错误,则弹出NSError在输出字典中使用H264编解码器而不是ProRes422编解码器。

    unsigned char *buf = [myAsset readFileFrame:i];  // contains 32-bit pixel data

    CVPixelBufferRef pxBuffer = nil;
    CVReturn status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, [adaptor pixelBufferPool], &pxBuffer);
    if (status != kCVReturnSuccess){
        NSLog(@"Failed to create pixel buffer");
    }

    CVPixelBufferLockBaseAddress(pxBuffer, 0);
    unsigned char *pxdata = CVPixelBufferGetBaseAddress(pxBuffer);
    memcpy(pxdata,buf,imageSize.width*imageSize.height*4);
    CVPixelBufferUnlockBaseAddress(pxBuffer, 0);

    free(buf);

    if (pxBuffer) {
        BOOL append_ok = NO;
        while (!append_ok) {
            if (adaptor.assetWriterInput.readyForMoreMediaData)  {
                //print out status:
                NSLog(@"Processing video frame (%lu)",(unsigned long)i);
                CMTime presTime = CMTimeMake((int64_t)i,(int32_t) fps);
                append_ok = [adaptor appendPixelBuffer:pxBuffer withPresentationTime:presTime];
                if(!append_ok){
                    NSError *error = videoWriter.error;
                    if(error!=nil) {
                        NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
                    }
                }
            }
            else {
                printf("adaptor not ready %lu\n", (unsigned long)i);
                [NSThread sleepForTimeInterval:0.1];
            }
        }
        //CVPixelBufferRelease(pxBuffer);
    }
}

NSLog(@"**************************************************");

//Finish the session:
[videoWriterInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler: ^{}];

产生的实际错误如下:

  

2016-01-17 15:52:46.132 AVIConvert [8495:292526]未解决的错误错误   Domain = AVFoundationErrorDomain Code = -11800"操作不能   完成" UserInfo = {NSUnderlyingError = 0x6280002423a0 {错误   Domain = NSOSStatusErrorDomain Code = -12348"(null)"},   NSLocalizedFailureReason =发生未知错误(-12348),   NSLocalizedDescription =无法完成操作},{       NSLocalizedDescription ="操作无法完成&#34 ;;       NSLocalizedFailureReason ="发生未知错误(-12348)&#34 ;;       NSUnderlyingError ="错误域= NSOSStatusErrorDomain代码= -12348 \"(null)\"&#34 ;;

我认为使用一些替代输出格式会起作用,但是,唉,不是:

  kCVPixelFormatType_32ARGB /* 32 bit ARGB */
  kCVPixelFormatType_32BGRA /* 32 bit BGRA */
  kCVPixelFormatType_32ABGR /* 32 bit ABGR */
  kCVPixelFormatType_32RGBA /* 32 bit RGBA */

有人可以提供一些见解吗?

0 个答案:

没有答案