gpt4 book ai didi

ios - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer :buffer withPresentationTime returning error NSURLErrorCannotCreateFile

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:33 24 4
gpt4 key购买 nike

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

NSURLErrorDomain Code=-3000 "Cannot create file" UserInfo=0x200be260 {NSUnderlyingError=0x200bb030 "The operation couldn’t be completed. (OSStatus error -12149.)", NSLocalizedDescription=Cannot create file}

我已经搜索了很多,但找不到任何东西。

这是创建路径的代码。

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

关于ios - AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer :buffer withPresentationTime returning error NSURLErrorCannotCreateFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com