我正在使用 UIImagePickerController 录制视频,问题是它以 mov 格式录制视频以实现 android 兼容性。
我需要使用下面的代码将视频转换为 mp4 格式,问题是 6 秒的视频需要花费大约 30 到 35 秒的时间。
我可以直接以 mp4 格式或更快的方法录制视频的任何解决方案都会有很大帮助。提前致谢
-(void)movToMp4:(NSURL *)videoURL{ // method for mov to mp4 conversion
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) { // switch case to get completion case where i put my delegate
return;
break;
case AVAssetExportSessionStatusCompleted: {
[self.delegate mp4Response:videoPath];
break;
}
}
}
}];
}
}
最佳答案
是的,因为使用 AVURLAsset
从库/相册加载视频需要时间.
所以你需要使用block
在这里从库中加载视频。
也行
[self.delegate mp4Response:videoPath];
在完成 block 中 - 它应该在主线程上。
遵循这种方法:
UIImagePickerController
从库中获取视频的委托(delegate)方法。- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"Capture video complete");
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
}
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (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);
}];
}
在
didFinishPickingMediaWithInfo
方法观察到这条线:[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
它将调用另一种方法
doneCompressing
在主线程中(在前台)。这样你就可以在 doneCompressing
中调用委托(delegate)方法.这将减少时间。- (void) doneCompressing {
[self.delegate mp4Response:videoPath];
}
关于ios - 以 Mp4 格式捕获视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656069/