【发布时间】:2014-03-27 19:22:22
【问题描述】:
在默认的照片应用程序中,Apple 允许您将视频分享到 youtube、facebook、vimeo 等。我想重现此功能,但我以 1080p 录制我的视频,所以它们是非常大的文件。苹果通过在上传前压缩视频来解决这个问题。我尝试做同样的事情,但惨遭失败。谁能指出我正确的方向?我发现这个问题很有用,但我不明白为什么它对我不起作用:iPhone:Programmatically compressing recorded video to share?。
这是我正在尝试的:
- (void)convertVideoToLowQualityWithInputURL:(NSURL *)inputURL
outputURL:(NSURL *)outputURL
handler:(void (^)(AVAssetExportSession *))handler {
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
break;
}
}];
}
-someMethod{
NSURL *videoURL = [NSURL fileURLWithPath:self.currentVideoURL];
NSArray *videoSplit = [[NSString stringWithFormat:@"%@",videoURL] componentsSeparatedByString: @"."];
NSString *first = [videoSplit firstObject];
NSString *output = [NSString stringWithFormat:@"%@_Low_Qual.%@",first,[videoSplit objectAtIndex:1]];
NSLog(@"VIDEO URL IS: %@",videoURL);
NSLog(@"OUTPUT URL IS: %@",output);
NSURL *outputURL = [NSURL fileURLWithPath:output];
[self convertVideoToLowQualityWithInputURL:videoURL outputURL:outputURL handler: ^(AVAssetExportSession *exportSession)
{
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
printf("completed\n");
}
else {
printf("error\n");
}
}];
}
但是,它每次都给我“导出失败:操作无法完成”。视频网址是有效的,所以我不知道为什么它不起作用。有什么想法吗?
【问题讨论】:
-
使用 AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:theAsset presetName:AVAssetExportPresetLowQuality];
-
刚试了没用:(
-
你还在导出失败吗?
-
打印 NSError。使用 AVAssetExportSession 属性状态和错误。
标签: ios objective-c cocoa-touch cocoa avfoundation