gpt4 book ai didi

iphone - AVAssetExportSession 输出文件

转载 作者:可可西里 更新时间:2023-11-01 03:36:48 28 4
gpt4 key购买 nike

AVAssetExportSession 的输出文件应该是什么样子的?我正在尝试从 ALAsset 项目中压缩视频,但它不起作用。我猜输出文件与它有关。

这是我使用的代码:

NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie"];
[self convertVideoToLowQualityWithInputURL:asset.defaultRepresentation.url outputURL:[NSURL URLWithString:destinationPath]];

- (void)convertVideoToLowQualityWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL {

if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithContentsOfURL:outputURL encoding: 0 error:Nil]]) [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

AVURLAsset *assetAV = [AVURLAsset URLAssetWithURL:inputURL options:nil];

NSLog(@"url string from asset: %@", assetAV.URL);
NSLog(@"output url: %@", [outputURL absoluteString]);

[[NSFileManager defaultManager] createFileAtPath:[outputURL path] contents:nil attributes:nil];

NSLog(@"duration: %lld", assetAV.duration.value); //it logs a valid value, so it's all good so far

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:assetAV presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"success");
} else {
NSLog(@"error: %@", [exportSession error]);
//error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x2023b720 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x2023bb70 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780)}
}
}];
}

有人可以帮帮我吗?

更新:

找到解决方案。我认为问题出在输出文件上,所以这里是生成有效文件的代码:

            NSUInteger count = 0;
NSString *filePath = nil;
do {
NSString *extension = ( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
NSString *fileNameNoExtension = [[asset.defaultRepresentation.url URLByDeletingPathExtension] lastPathComponent];
NSString *fileName = [NSString stringWithFormat:@"%@-%@-%u",fileNameNoExtension , AVAssetExportPresetLowQuality, count];
filePath = NSTemporaryDirectory();
filePath = [filePath stringByAppendingPathComponent:fileName];
filePath = [filePath stringByAppendingPathExtension:extension];
count++;

} while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]);

NSURL *outputURL = [NSURL fileURLWithPath:filePath];

最佳答案

您的问题是 [NSURL URLWithString:destinationPath] 并且仅此而已。您需要像在解决方案中那样使用 [NSURL fileURLWithPath:xxx] 。发布这个只是为了澄清这是唯一的问题。

关于iphone - AVAssetExportSession 输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12529250/

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