小问题

集成上传视频的准备工作

2019-01-17  本文已影响22人  默默码字的我

首先我在选择上传视频的时候用到的第三方是 TZImagePickerController

这个第三方非常牛 选择的图片 还能选择视频非常棒 棒棒哒 手动点赞

   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的不是我们想要的格式所以我们要手动转换成mp4的

这个代理方法里获取到视频

-(void)imagePickerController:(TZImagePickerController*)picker

       didFinishPickingVideo:(UIImage*)coverImage

                sourceAssets:(PHAsset*)asset{

   //导出视频

    [[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];

                    }];

}

获取到后正式开始转换

- (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目录下创建一个名为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(@"创建文件夹失败!%@",path);

            }

            NSLog(@"创建文件夹成功,文件路径%@",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) {

}]

其实还有点什么杂七杂八的东西 大家有需要的可以问 我看到了也尽量回答哈

还是那句话大神请绕道 这只是一只菜鸡一点想法

上一篇 下一篇

猜你喜欢

热点阅读