調(diào)取系統(tǒng)錄制視頻并上傳串结,獲取第一幀顯示在界面
1.調(diào)取系統(tǒng)攝像
self.imagePicker=[[UIImagePickerController alloc]init];
self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;//設(shè)置image picker的來(lái)源簸喂,這里設(shè)置為攝像頭
self.imagePicker.cameraDevice=UIImagePickerControllerCameraDeviceRear;//設(shè)置使用哪個(gè)攝像頭,這里設(shè)置為后置攝像頭
self.imagePicker.mediaTypes=@[(NSString *)kUTTypeMovie];
//? ? ? ? self.imagePicker.videoQuality=UIImagePickerControllerQualityTypeIFrame1280x720;
self.imagePicker.videoQuality=UIImagePickerControllerQualityTypeLow;
self.imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModeVideo;//設(shè)置攝像頭模式(拍照贡茅,錄制視頻)
self.imagePicker.allowsEditing=YES;//允許編輯
self.imagePicker.delegate=self;//設(shè)置代理,檢測(cè)操作
[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];//用時(shí)間給文件全名,以免重復(fù)赊锚,在測(cè)試的時(shí)候其實(shí)可以判斷文件是否存在若存在治筒,則刪除,重新生成文件即可
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;//這個(gè)是保存在app自己的沙盒路徑里舷蒲,后面可以選擇是否在上傳后刪除掉耸袜。我建議刪除掉,免得占空間牲平。
//把視頻從mov轉(zhuǎn)成mp4
[self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];
}
3. 轉(zhuǎn)碼并獲取第一幀
- (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);//這個(gè)是保存到手機(jī)相冊(cè)
//? ? ? ? ? ? ? ? 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轉(zhuǎn)成base64string