iOS 錄制視頻并上傳

調取系統錄制視頻并上傳,獲取第一幀顯示在界面

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容