簡體   English   中英

AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime返回錯誤NSURLErrorCannotCreateFile

[英]AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime returning error NSURLErrorCannotCreateFile

我正在從一系列圖像創建一個視頻文件。 我能夠在模擬器上創建視頻文件,但是當我嘗試在設備上運行相同的代碼時,它會出現以下錯誤:

NSURLErrorDomain代碼= -3000“無法創建文件”UserInfo = 0x200be260 {NSUnderlyingError = 0x200bb030“操作無法完成。(OSStatus error -12149。)”,NSLocalizedDescription =無法創建文件}

我經常搜索但找不到任何東西。

這是創建路徑的代碼。

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/movie.mp4"]];


-(void)exportImages:(NSArray *)imageArray
      asVideoToPath:(NSString *)path
      withFrameSize:(CGSize)imageSize
    framesPerSecond:(NSUInteger)fps {

NSLog(@"Start building video from defined frames.");

NSError *error = nil;

NSURL *url = [NSURL fileURLWithPath:path];

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              url fileType:AVFileTypeMPEG4
                                                          error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:imageSize.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:imageSize.height], AVVideoHeightKey,
                               nil];

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


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

NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];

//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;

//convert uiimage to CGImage.
int frameCount = 0;

for(UIImage * img in imageArray) {
    buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:imageSize];

    while (1) {
        if (adaptor.assetWriterInput.readyForMoreMediaData) {
            break;
        }
    }

    BOOL append_ok = NO;
    int j = 0;
    while (!append_ok && j < 30) {
        NSString *border = @"**************************************************";
        NSLog(@"\n%@\nProcessing video frame (%d,%d).\n%@",border,frameCount,[imageArray count],border);

        CMTime frameTime = CMTimeMake(frameCount,(int32_t) fps);
        append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
        if(!append_ok){
            NSError *error = videoWriter.error;
            if(error!=nil) {
                NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
            }
        }
        j++;
    }
    if (!append_ok) {
        printf("error appending image %d times %d\n, with error.", frameCount, j);
    }
    frameCount++;
}

//Finish the session:
[videoWriterInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler:^{
    NSLog(@"Write Ended");
}];

}

當我試圖在現有路徑中保存電影時,我遇到了同樣的錯誤。 嘗試使用此代碼為新視頻創建路徑:

- (NSURL *)createOutputURLWithDate
{
    NSDateFormatter *kDateFormatter;

    kDateFormatter = [[NSDateFormatter alloc] init];
    kDateFormatter.dateStyle = NSDateFormatterMediumStyle;
    kDateFormatter.timeStyle = kCFDateFormatterLongStyle;

    return [[[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory 
                                                    inDomain:NSUserDomainMask 
                                           appropriateForURL:nil 
                                           create:@YES error:nil] URLByAppendingPathComponent:[kDateFormatter stringFromDate:[NSDate date]]] URLByAppendingPathExtension:CFBridgingRelease(UTTypeCopyPreferredTagWithClass((CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension))];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM