首先我在選擇上傳視頻的時候用到的第三方是?TZImagePickerController
這個第三方非常牛 選擇的圖片 還能選擇視頻非常棒 棒棒噠 手動點贊
上傳流程
1.請求上傳地址加憑證或STS油讯,相關(guān)概念請參見相關(guān)文檔。
2.初始化上傳實例填帽,實例化上傳有兩種方式:上傳地址加憑證和STS方式涝焙。
3.回調(diào)設(shè)置,所有的上傳狀態(tài)包括進度,上傳成功,上傳失敗追迟,憑證過期都在這里進行處理。
4.添加上傳文件進入上傳列表伐坏,目前主要支持視頻文件和圖片文件的上傳怔匣。
5.啟動上傳
6.回調(diào)處理
上傳步驟
?初始化SDK?明文設(shè)置模式(不推薦)握联,2.STS鑒權(quán)模式(推薦)桦沉,3.自簽名模式
上傳方式
VOD(短視頻上傳)每瞒,OSS(文件上傳)
上傳成功的返回數(shù)據(jù)
VOD上傳返回視頻id(字符串),OSS上傳返回文件名(字符串)
?? imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
? ? // 是否顯示可選原圖按鈕
? ? imagePickerVc.allowPickingOriginalPhoto = NO;
? ? // 是否允許顯示視頻
? ? imagePickerVc.allowPickingVideo = YES;
? ? // 是否允許顯示圖片
? ? imagePickerVc.allowPickingImage = NO;
? ? imagePickerVc.videoMaximumDuration = 30;//最長拍攝時間
? ? [self presentViewController:imagePickerVc animated:YES completion:nil];
通過第三方選擇的視頻后其實是mov的不是我們想要的格式所以我們要手動轉(zhuǎn)換成mp4的
這個代理方法里獲取到視頻
-(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)建一個名為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"];//每次啟動后都保存一個新的日志文件中
? ? ? ? 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;
?? ? ? ? ? ? }
?? ? ? ? }];
? ? }
}
還有個縮略圖的方法
? //縮略圖
? ? [[TZImageManager manager] getPhotoWithAsset:asset
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? photoWidth:kScreenWidth
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(UIImage*photo,NSDictionary*info,BOOLisDegraded) {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(!isDegraded) {
}]
其實還有點什么雜七雜八的東西 大家有需要的可以問 我看到了也盡量回答哈
還是那句話大神請繞道 這只是一只菜雞一點想法