IT门户网,专注电脑知识与技术及手机硬件信息服务网站!

当前位置:IT门户网 > 系统教程 >

iOS视频录制、压缩导出、取帧等

时间:2021-09-14    来源:IT门户网    人气:

首先,我们弹出系统的视频录制界面,也就是UIImagePickerController节制器来实现,可是我们需要验证用户授权,只有有录制视频的权限,才气继承往下。

我们还需要判定UIImagePickerControllerSourceTypeCamera是否支持,好比模仿器就不支持,虽然真机是否有不支持的并不知道,不外更安详的写法是要这么写的。视频录制可以配置录制的视频的质量,也就是判别率的坎坷,通过videoQuality属性来配置。我们还可以配置录制视频的最大时长,通过videoMaximumDuration属性配置,好比这里配置为5分钟。

// 7.0AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) { NSLog(@"摄像头已被禁用,您可在配置应用措施中举办开启"); return;}if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.videoQuality = UIImagePickerControllerQualityType640x480; //录像质量 picker.videoMaximumDuration = 5 * 60.0f; // 限制视频录制最多不高出5分钟 picker.mediaTypes = @[(NSString *)kUTTypeMovie]; [self PResentViewController:picker animated:YES completion:NULL]; self.shouldAsync = YES;} else { NSLog(@"手机不支持摄像");}

然后实现署理,就可以拿到录制的视频了。

从相册选择视频

从相册选择视频与弹出录制视频的代码差不多,只是sourceType纷歧样罢了。我们一样要求先判定权限,用户是否授权,若不答允,就没有步伐了。

指定sourceType为UIImagePickerControllerSourceTypeSavedPhotosAlbum就是获取生存到相册中的media。我们还要指定mediaTypes,只需要配置为kUTTypeMovie就可以了。

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) { NSLog(@"摄像头已被禁用,您可在配置应用措施中举办开启"); return;}if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; picker.mediaTypes = @[(NSString *)kUTTypeMovie]; [self presentViewController:picker animated:YES completion:NULL]; self.shouldAsync = NO;} else { NSLog(@"手机不支持摄像");}

同样,实现署理要领,就可以取到所选择的视频了。

生存视频到相册

写入相册可以通过ALAssetsLibrary类来实现,它提供了写入相册的API,异步写入,完成是要回到主线程更新UI:

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];dispatch_async(dispatch_get_global_queue(0, 0), ^{ // 判定相册是否兼容视频,兼容才气生存到相册 if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) { [library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ // 写入相册 if (error == nil) { NSLog(@"写入相册乐成"); } else { NSLog(@"写入相册失败"); } } }]; }});获取视频帧图同步获取帧图

同步获取中间帧,需要指定哪个时间点的帧,当获取到今后,返返来的图片工具是CFRetained过的,需要外面手动CGImageRelease一下,释放内存。通过AVAsset来会见详细的视频资源,然后通过AVAssetImageGenerator图片生成器来生成某个帧图片:

// Get the video's center frame as video poster image- (UIImage *)frameImageFromVideoURL:(NSURL *)videoURL { // result UIImage *image = nil; // AVAssetImageGenerator AVAsset *asset = [AVAsset assetWithURL:videoURL]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; imageGenerator.appliesPreferredTrackTransform = YES; // calculate the midpoint time of video Float64 duration = CMTimeGetSeconds([asset duration]); // 取某个帧的时间,参数一暗示哪个时间(秒),参数二暗示每秒几多帧 // 凡是来说,600是一个常用的民众参数,苹果有说明: // 24 frames per second (fps) for film, 30 fps for NTSC (used for TV in North America and // Japan), and 25 fps for PAL (used for TV in Europe). // Using a timescale of 600, you can exactly represent any number of frames in these systems CMTime midpoint = CMTimeMakeWithSeconds(duration / 2.0, 600); // get the image from NSError *error = nil; CMTime actualTime; // Returns a CFRetained CGImageRef for an asset at or near the specified time. // So we should mannully release it CGImageRef centerFrameImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error]; if (centerFrameImage != NULL) { image = [[UIImage alloc] initWithCGImage:centerFrameImage]; // Release the CFRetained image CGImageRelease(centerFrameImage); } return image;}异步获取帧图

相关文章

系统教程排行榜

更多>>

网络知识排行榜

更多>>

系统教程排行榜

更多>>

服务号