【问题标题】:AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime returning error NSURLErrorCannotCreateFileAVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime 返回错误 NSURLErrorCannotCreateFile
【发布时间】:2013-12-26 20:23:23
【问题描述】:

我正在从一组图像创建一个视频文件。我可以在模拟器上创建视频文件,但是当我尝试在设备上运行相同的代码时,它会出现以下错误:

NSURLErrorDomain 代码=-3000 "无法创建文件" UserInfo=0x200be260 {NSUnderlyingError=0x200bb030 "操作无法完成。 (OSStatus 错误 -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");
}];

}

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    我在尝试将电影保存到现有路径时遇到了同样的错误。 尝试使用此代码为您的新视频创建路径:

    - (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))];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多