iOS-阿里云集成上传视频的准备工作

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

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

上传流程

1.请求上传地址加凭证或STS,相关概念请参见相关文档。

2.初始化上传实例,实例化上传有两种方式:上传地址加凭证和STS方式。

3.回调设置,所有的上传状态包括进度,上传成功,上传失败,凭证过期都在这里进行处理。

4.添加上传文件进入上传列表,目前主要支持视频文件和图片文件的上传。

5.启动上传

6.回调处理

上传步骤

\bullet  初始化SDK 明文设置模式(不推荐),2.STS鉴权模式(推荐),3.自签名模式

\bullet 上传方式

VOD(短视频上传),OSS(文件上传)

上传成功的返回数据

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

}]


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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容