gpt4 book ai didi

ios - 以 Mp4 格式捕获视频

转载 作者:行者123 更新时间:2023-12-01 17:39:09 25 4
gpt4 key购买 nike

我正在使用 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/

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