iOS开发之视频兼容及网页无法播放的解决办法

在做视频上传到阿里云时我碰到了以下几个问题:
1.web端无法播放使用iPhone xs max与iOS12系统录制的视频;
2.10.0的6s与5s没法播放iOS12及以上录制的视频;

解决方法:
由于我们的视频是直接转化为data上传至阿里云的,阿里云以及本地没有对.mov结尾的视频做处理。现得出以下两种解决方法。
1.阿里云开通视频转码服务,此方法需要付费以及后台配置。
2.iOS端将获取到的视频转化为mp4格式。现贴出转化以及上传的代码:

// 上传data格式的视频
- (void)asyncUploadVideoUrl:(NSURL *)url callback:(uploadCallblock)callback {
    
    [self mov2mp4:url completion:^(NSURL *mp4Url) {
    
        if (mp4Url) {
    
           NSData *mp4Data = [NSData dataWithContentsOfFile:mp4Url.absoluteString];
            
            if (mp4Data != nil) {
               
                id credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:OSSKey secretKey:OSSSecret];
                
                OSSClient *client = [[OSSClient alloc] initWithEndpoint:OSSEndpoint credentialProvider:credential];
                
                //任务执行
                OSSPutObjectRequest * put = [OSSPutObjectRequest new];
                
                put.uploadingData = mp4Data;
                
                put.bucketName = OSSBucketName;
                
                NSString *str = [mp4Url absoluteString];
                NSArray *arr = [str componentsSeparatedByString:@"."];
                NSString *latVideo = [arr lastObject]; //视频格式
                
                NSString *imageName = [NSString stringWithFormat:@"%@/ios%@.%@",OSSVideoPath,[self getNowTimeTimestamp],latVideo];
                
                put.objectKey = imageName;
                
                OSSTask * putTask = [client putObject:put];
                
                [putTask waitUntilFinished]; // 阻塞直到上传完成
                
                [putTask continueWithBlock:^id(OSSTask *task) {
                    
                    task = [client presignPublicURLWithBucketName:OSSBucketName
                                                    withObjectKey:imageName];
                    
                    if (!task.error) {
                        
                        if (callback)   callback( YES,  @"上传成功", @[task.result]);
                        
                    } else {
                        
                        NSLog(@"upload object failed, error: %@" , putTask.error);
                        NSLog(@"upload object failed, error: %@" , task.error);
                    }
                    return nil;
                }];
            } else {
                NSLog(@"获取视频出错");
            }
        }
    }];
}
- (void)mov2mp4:(NSURL *)movUrl completion:(void(^)(NSURL *mp4Url))completion
{
    
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:movUrl options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    /**
     AVAssetExportPresetMediumQuality 表示视频的转换质量,
     */
    if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality];
        
        //转换完成保存的文件路径
        NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@%@.mp4",@"cvt",[self getNowTimeTimestamp]];

        exportSession.outputURL = [NSURL fileURLWithPath:resultPath];

        //要转换的格式,这里使用 MP4
        exportSession.outputFileType = AVFileTypeMPEG4;

        //转换的数据是否对网络使用优化
        exportSession.shouldOptimizeForNetworkUse = YES;

        //异步处理开始转换
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void)

         {
             //转换状态监控
             switch (exportSession.status) {
                 case AVAssetExportSessionStatusUnknown:
                     NSLog(@"AVAssetExportSessionStatusUnknown");
                     break;

                 case AVAssetExportSessionStatusWaiting:
                     NSLog(@"AVAssetExportSessionStatusWaiting");
                     break;

                 case AVAssetExportSessionStatusExporting:
                     NSLog(@"AVAssetExportSessionStatusExporting");
                     break;
                 case AVAssetExportSessionStatusFailed:
                     NSLog(@"AVAssetExportSessionStatusFailed");
                     break;
                 case AVAssetExportSessionStatusCancelled:
                     NSLog(@"AVAssetExportSessionStatusCancelled");
                     break;

                 case AVAssetExportSessionStatusCompleted:
                 {
                     //转换完成
                     NSLog(@"AVAssetExportSessionStatusCompleted");

//                     //测试使用,保存在手机相册里面
//                     ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
//                     [assetLibrary writeVideoAtPathToSavedPhotosAlbum:exportSession.outputURL completionBlock:^(NSURL *assetURL, NSError *error){
//                         if (error) {
//                             NSLog(@"%@",error);
//                         }
//                     }];
                     NSLog(@"resultPath ----- %@",resultPath);
                     if (completion) {
                         completion([NSURL URLWithString:resultPath]);
                     }
                     break;
                 }
             }

         }];

    }

}

你可能感兴趣的:(iOS开发,---,随笔篇)