首先我在選擇上傳視頻的時(shí)候用到的第三方是?TZImagePickerController
這個(gè)第三方非常牛 選擇的圖片 還能選擇視頻非常棒 棒棒噠 手動(dòng)點(diǎn)贊
上傳流程
1.請(qǐng)求上傳地址加憑證或STS,相關(guān)概念請(qǐng)參見相關(guān)文檔。
2.初始化上傳實(shí)例,實(shí)例化上傳有兩種方式:上傳地址加憑證和STS方式。
3.回調(diào)設(shè)置,所有的上傳狀態(tài)包括進(jìn)度,上傳成功,上傳失敗,憑證過期都在這里進(jìn)行處理。
4.添加上傳文件進(jìn)入上傳列表,目前主要支持視頻文件和圖片文件的上傳。
5.啟動(dòng)上傳
6.回調(diào)處理
上傳步驟
?初始化SDK?明文設(shè)置模式(不推薦),2.STS鑒權(quán)模式(推薦),3.自簽名模式
上傳方式
VOD(短視頻上傳),OSS(文件上傳)
上傳成功的返回?cái)?shù)據(jù)
VOD上傳返回視頻id(字符串),OSS上傳返回文件名(字符串)
?? imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
? ? // 是否顯示可選原圖按鈕
? ? imagePickerVc.allowPickingOriginalPhoto = NO;
? ? // 是否允許顯示視頻
? ? imagePickerVc.allowPickingVideo = YES;
? ? // 是否允許顯示圖片
? ? imagePickerVc.allowPickingImage = NO;
? ? imagePickerVc.videoMaximumDuration = 30;//最長(zhǎng)拍攝時(shí)間
? ? [self presentViewController:imagePickerVc animated:YES completion:nil];
通過第三方選擇的視頻后其實(shí)是mov的不是我們想要的格式所以我們要手動(dòng)轉(zhuǎn)換成mp4的
這個(gè)代理方法里獲取到視頻
-(void)imagePickerController:(TZImagePickerController*)picker
?? ? ? didFinishPickingVideo:(UIImage*)coverImage
? ? ? ? ? ? ? ? sourceAssets:(PHAsset*)asset{
?? //導(dǎo)出視頻
? ? [[TZImageManager manager] getVideoOutputPathWithAsset:asset success:^(NSString *outputPath) {
? ? ? ? _videoUrl= outputPath;
? ? ? ?AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:outputPath] options:nil];
? ? ? ? [self choseVedioCompeletWithVedioAsset:urlAsset
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andAVAudioMix:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andVedioInfo:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andImageSize:CGSizeZero];
? ? }failure:^(NSString*errorMessage,NSError*error) {
? ? ? ? NSLog(@"獲取視頻失敗");
? ? }];
}
然后獲取到他的urlAsset ?vedioInfo?size
- (void)choseVedioCompeletWithVedioAsset:(AVURLAsset*)urlAsset
?? ? ? ? ? ? ? ? ? ? ? ? ? andAVAudioMix:(AVAudioMix*)audioMix
? ? ? ? ? ? ? ? ? ? ? ? ? ? andVedioInfo:(NSDictionary*)vedioInfo
? ? ? ? ? ? ? ? ? ? ? ? ? ? andImageSize:(CGSize)size{
? ? __weak typeof(self) weakSelf = self;
? ? [self convertMovToMp4FromAVURLAsset:urlAsset
? ? ? ? ? ? ? ? ? ? andCompeleteHandler:^(NSURL*_NonnullfileUrl) {
? ? ? ? ? ? ? ? ? ? ? ? [weakSelfuploadVideoWithFileUrl:fileUrl];
? ? ? ? ? ? ? ? ? ? }];
}
獲取到后正式開始轉(zhuǎn)換
- (void)convertMovToMp4FromAVURLAsset:(AVURLAsset*)urlAsset andCompeleteHandler:(void(^)(NSURL*fileUrl))fileUrlHandler{
? ? AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:urlAsset.URL options:nil];
? ? NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
? ? if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) {
? ? ? ? //? 在Documents目錄下創(chuàng)建一個(gè)名為FileData的文件夾
? ? ? ? NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"Cache/VideoData"];
? ? ? ? NSFileManager *fileManager = [NSFileManager defaultManager];
? ? ? ? BOOLisDir =FALSE;
? ? ? ? BOOLisDirExist = [fileManagerfileExistsAtPath:pathisDirectory:&isDir];
? ? ? ? if(!(isDirExist && isDir)) {
? ? ? ? ? ? BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
? ? ? ? ? ? if(!bCreateDir){
?? ? ? ? ? ? ? ?NSLog(@"創(chuàng)建文件夾失??!%@",path);
? ? ? ? ? ? }
? ? ? ? ? ? NSLog(@"創(chuàng)建文件夾成功,文件路徑%@",path);
? ? ? ? ? ? //文件路徑
? ? ? ? ? ? vodeopPath= path;
? ? ? ? }
? ? ? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
? ? ? ? [formattersetLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
? ? ? ? [formattersetDateFormat:@"yyyy_MM_dd_HH_mm_ss"];//每次啟動(dòng)后都保存一個(gè)新的日志文件中
? ? ? ? NSString*dateStr = [formatterstringFromDate:[NSDatedate]];
? ? ? ? NSString*resultPath = [pathstringByAppendingFormat:@"/%@.mp4",dateStr];
? ? ? ? videoName= dateStr;
? ? ? ? NSLog(@"file path:%@",resultPath);
? ? ? ? NSLog(@"resultPath = %@",resultPath);
? ? ? ? AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? presetName:AVAssetExportPresetMediumQuality];
? ? ? ? exportSession.outputURL= [NSURLfileURLWithPath:resultPath];
? ? ? ? exportSession.outputFileType = AVFileTypeMPEG4;
? ? ? ? exportSession.shouldOptimizeForNetworkUse = YES;
? ? ? ?[exportSessionexportAsynchronouslyWithCompletionHandler:^(void)
? ? ? ? {
?? ? ? ? ? ? switch(exportSession.status) {
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusUnknown:
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(exportSession.outputURL);
? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusUnknown --------%@",exportSession.outputURL);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusWaiting:
?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusWaiting");
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusExporting:
?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusExporting");
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusCompleted:
?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusCompleted");
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(exportSession.outputURL);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusFailed:
?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusFailed");
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusCancelled:
?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusCancelled");
?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);
?? ? ? ? ? ? ? ? ? ? break;
?? ? ? ? ? ? }
?? ? ? ? }];
? ? }
}
還有個(gè)縮略圖的方法
? //縮略圖
? ? [[TZImageManager manager] getPhotoWithAsset:asset
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? photoWidth:kScreenWidth
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(UIImage*photo,NSDictionary*info,BOOLisDegraded) {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(!isDegraded) {
}]
其實(shí)還有點(diǎn)什么雜七雜八的東西 大家有需要的可以問 我看到了也盡量回答哈
還是那句話大神請(qǐng)繞道 這只是一只菜雞一點(diǎn)想法
