这里没有主要是说下和视频相关的小技术点,都是一些相对比较基本的,但是对于没有怎么和视频打交道的人而言,找一些资料还不是很方便,所以今天就没事白这些东西给稍微整理了一下。
先看看视频的录制,这里主要说的是使用系统的UIImagePickerController这个类进行视频的录制。注意:导入这个框架才能录制视频#import<MobileCoreServices/MobileCoreServices.h>这个框架后才能播放进入系统的录制视频界面。
//1、判断照相机是否可用 不可用就直接return 一般都是这样
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
return;
}
//2.初始化
UIImagePickerController *picker = [UIImagePickerController new];
//3.设置类型
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//通常4和5是一块设置的
//4.设置媒体类型 默认是拍照
picker.mediaTypes = @[(NSString *)kUTTypeMovie];
//注意:如果是UIImagePickerControllerSourceTypePhotoLibrary设置下面的两句会崩溃
//5.设置摄像机模式
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo; //6.设置视频质量
// picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
//7.设置代理
picker.delegate = self;
//8.模态弹出
[self presentViewController:picker animated:YES completion:nil];
再看一下录制的视频如何保存到本地。注意:保存视频到本地 需要这个资源库<AssetsLibrary/AssetsLibrary.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//1.判断是否是视频的媒体类型
NSString *mediaType = info[UIImagePickerControllerMediaType];
//2.保存视频到本地 需要这个资源库<AssetsLibrary/AssetsLibrary.h>
//必须做这个判断
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//这个已经过期了 iOS9需要换成PHPhotoLibrary
//2.1创建ALAssetsLibrary对象
ALAssetsLibrary *assetsLibrary = [ALAssetsLibrary new];
//2.2 这个url是指要保存的视频的url
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error) {
}];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
下面是视频的压缩的一个类方法,以及删除压缩后视频的方法。代码内部有十分详细的注释。说明一下第一个方法中传入的sourceVideoPathString是这样获取的。在(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info这个方法中,通过
self.videoPathString = (NSString *)([info[@"UIImagePickerControllerMediaURL"] path]);这样的形式获取到路径。
+ (void)compressVideoWithSourceVideoPathString:(NSString *)sourceVideoPathString
CompressType:(NSString *)compressType
CompressSuccessBlock:(SuccessBlock)compressSuccessBlock
CompressFailedBlock:(FailedBlock)compressFailedBlock
CompressNotSupportBlock:(NotSupportBlock)compressNotSupportBlock {
// 源视频路径
NSURL *sourceVideoPathUrl = [NSURL fileURLWithPath:sourceVideoPathString];
// 利用源视频路径将源视频转化为 AVAsset 多媒体载体对象
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceVideoPathUrl options:nil];
// 源视频载体对象支持的压缩格式
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
// 源视频载体对象支持的压缩格式中是否包含我们选择的压缩格式
if ([compatiblePresets containsObject:compressType]) {
// 存放压缩视频的文件夹
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *compressVideoFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/compressVideoFolder"];
if (![fileManager fileExistsAtPath:compressVideoFolder]) {
[fileManager createDirectoryAtPath:compressVideoFolder withIntermediateDirectories:YES attributes:nil error:nil];
}
// 用当前系统时间给文件命名, 避免因名字重复而覆盖存储
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
NSString *currentDateString = [formatter stringFromDate:[NSDate date]];
/**
* 第一个参数 : 要压缩的 AVAsset 对象
第二个参数 : 我们选择的压缩方式
*/
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:compressType];
// 压缩视频的输出路径
NSString *compressVideoPathString = [compressVideoFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"compressVideo-%@.mp4", currentDateString]];
NSURL *compressFilePathUrl = [NSURL fileURLWithPath:compressVideoPathString];
exportSession.outputURL = compressFilePathUrl;
// 压缩文件的输出格式
exportSession.outputFileType = AVFileTypeMPEG4;
// 压缩文件应保证优化网络使用
exportSession.shouldOptimizeForNetworkUse = YES;
// 开始压缩
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
compressSuccessBlock(compressVideoPathString);
}else {
compressFailedBlock();
}
}];
}else {
compressNotSupportBlock();
}
}
//删除压缩后视频的方法
+ (void)deleteCompressVideoFromPath:(NSString *)compressVideoPathString {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *compressVideoFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/compressVideoFolder"];
if ([fileManager fileExistsAtPath:compressVideoFolder]) {
[fileManager removeItemAtPath:compressVideoFolder error:nil];
}
}
视频的base64转码,转码成功后将返回一堆字符串。
// 编码
+ (void)base64StringFromString:(NSString *)filePathString
SuccessBlock:(SuccessBlock)success
FailedBlock:(FailedBlock)failed {
// 获取文件的二进制数据 data
NSData *data = [NSData dataWithContentsOfFile:filePathString];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 转码 --> 码文
NSString *base64String = [data base64EncodedStringWithOptions:0];
if (base64String) {
dispatch_async(dispatch_get_main_queue(), ^{
success(base64String);
});
}else {
dispatch_async(dispatch_get_main_queue(), ^{
failed();
});
}
});
}
//外部的调用形式
[ZWBase64EncodeTools base64StringFromString:self.filePathString SuccessBlock:^(NSString *string) {
//转码成功后返回一堆字符串
self.base64StringTextView.text = string;
self.navigationItem.title = [NSString stringWithFormat:@"压缩data的Base64码, %.2fM", string.length / 1024.0 / 1024.0];
} FailedBlock:^{
self.navigationItem.title = @"转码失败了!";
}];
说道这里就顺便说一下图片的base64转码了。下面方法中fileData是图片的二进制文件。
+ (void)base64StringFromData:(NSData *)fileData
SuccessBlock:(SuccessBlock)success
FailedBlock:(FailedBlock)failed {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 转码 --> 码文
NSString *base64String = [fileData base64EncodedStringWithOptions:0];
if (base64String) {
dispatch_async(dispatch_get_main_queue(), ^{
success(base64String);
});
}else {
dispatch_async(dispatch_get_main_queue(), ^{
failed();
});
}
});
}