调取系统录制视频并上传,获取第一帧显示在界面
1.调取系统摄像
self.imagePicker=[[UIImagePickerController alloc]init];
self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;//设置image picker的来源,这里设置为摄像头
self.imagePicker.cameraDevice=UIImagePickerControllerCameraDeviceRear;//设置使用哪个摄像头,这里设置为后置摄像头
self.imagePicker.mediaTypes=@[(NSString *)kUTTypeMovie];
// self.imagePicker.videoQuality=UIImagePickerControllerQualityTypeIFrame1280x720;
self.imagePicker.videoQuality=UIImagePickerControllerQualityTypeLow;
self.imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModeVideo;//设置摄像头模式(拍照,录制视频)
self.imagePicker.allowsEditing=YES;//允许编辑
self.imagePicker.delegate=self;//设置代理,检测操作
[self presentViewController:self.imagePicker animated:YES completion:nil];
2.代理方法
if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//如果是录制视频
NSLog(@"video...");
[self dismissViewControllerAnimated:YES completion:nil];
NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSURL *newVideoUrl ; //一般.mp4
NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;//这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。我建议删除掉,免得占空间。
//把视频从mov转成mp4
[self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];
}
3. 转码并获取第一帧
- (void) convertVideoQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
completeHandler:(void (^)(AVAssetExportSession*))handler
{
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
// NSLog(resultPath);
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:{
NSLog(@"AVAssetExportSessionStatusCompleted");
// UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册
// AVPlayer *player = [AVPlayer playerWithURL:outputURL];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:outputURL];
self.moviePlayer.view.backgroundColor=[UIColor blackColor];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = NO;
float spaceW=(YBScreenBoundsWidth-270)/4;
//
self.moviePlayer.view.frame = CGRectMake(spaceW, 220, 150, 150);
CDLog(@"路径%@",[outputURL path]);
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
// self.moviePlayer.contentURL=outputURL;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
//
// int size=(int)[self getFileSize:[outputURL path]];
// NSLog(@"文件%@的大小%fM",[outputURL path],size/1024.0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image=[self thumbnailImageForVideo:outputURL atTime:1];
self.firstimage=image;
dispatch_async(dispatch_get_main_queue(), ^{
bgimage=[[UIImageView alloc]initWithFrame: CGRectMake(spaceW, 220, 150, 150)];
[bgimage setImage:image];
[self.view addSubview:bgimage];
playBtn=[[UIButton alloc]initWithFrame:CGRectMake(spaceW+50, 270, 50, 50)];
[playBtn setImage:[UIImage imageNamed:@"FCircle_play"] forState:UIControlStateNormal];
[playBtn addTarget:self action:@selector(playBtnDown) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playBtn];
});
});
NSData *videlData = [NSData dataWithContentsOfURL:outputURL];
self.vdata=videlData;
// [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(movieFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
// [self.moviePlayer play];
//
break;
}
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
}
}];
}
- (NSInteger)getFileSize:(NSString *)path
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:path]) {
NSDictionary*attributes = [fileManager attributesOfItemAtPath:path error:nil];
NSNumber*fileSize;
if((fileSize = [attributes objectForKey:NSFileSize])) {
return fileSize.intValue/1024;
}else{
return -1;
}
}else{
return-1;
}
}
4.获取视频的第一帧
#pragma mark----获取视频的某一帧
-(UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] : nil;
return thumbnailImage;
}
5.上传的主要逻辑是把nsdata转成base64string