是否可以保存视频并将其添加到以MP4格式从UIImagePicker捕获的自定义ALAsset中?还是我必须将其保存在.mov中并通过AVAssetExportSession进行压缩?
最佳答案
是的,您可以使用AVAssetExportSession
压缩视频。您可以在此处指定视频类型,质量和用于压缩视频的输出URL。
参见以下方法:
- (void) saveVideoToLocal:(NSURL *)videoURL {
@try {
NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [documentsDirectory objectAtIndex:0];
NSString *videoName = [NSString stringWithFormat:@"sampleVideo.mp4"];
NSString *videoPath = [docPath stringByAppendingPathComponent:videoName];
NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
NSLog(@"Loading video");
[self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"Compression is done");
}
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
}];
}
@catch (NSException *exception) {
NSLog(@"Exception :%@",exception.description);
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
}
}
//---------------------------------------------------------------
- (void)convertVideoToLowQuailtyWithInputURL:(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:AVAssetExportPresetPassthrough];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
handler(exportSession);
}];
}
在这里,我将压缩视频保存到应用程序的文档目录中。您可以在下面的示例代码中检查其详细工作:
Sample demo:
关于uiimagepickercontroller - 带有mp4格式的iOS UIImagePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13912139/