iOS 14 从 iCloud 共享相册导出视频到 mp4 失败

问题描述

作为开发人员,我想将 iCloud 共享相册中的视频导出到 mp4,以便我可以在我的 Messenger 应用中发送视频。

实际上,以下 exportAsynchronouslyWithCompletionHandler 方法总是失败并给出 AVAssetExportSessionStatusFailed

常规方法 requestExportSessionForVideorequestAVAssetForVideo 都呈现相同的错误。请告诉我们处理和解决这个困难的方法

 PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
                            options.networkAccessAllowed = YES;
                            options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
                            options.version = PHVideoRequestOptionsversionCurrent;
                        
                        [[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession,NSDictionary * _Nullable info) {
                            if (exportSession && [exportSession.asset isKindOfClass:[AVURLAsset class]]) {
                                AVAssetTrack *videoTrack = nil;
                                
                                NSArray *videoTracks = [exportSession.asset tracksWithMediaType:AVMediaTypeVideo];
                                videoTrack = videoTracks.firstObject;
                                
                                __block CGSize videoSize = CGSizeApplyAffineTransform(videoTrack.naturalSize,videoTrack.preferredTransform);
                                
                                __block CMTime duration = exportSession.asset.duration;
                                
                                NSMutableDictionary *videoInfo = [[UIImage saveVideoInDocumentDirectory] mutablecopy];
                                [videoInfo re_setobject:caption forKey:kInfoKeyMediaCaption];
                                [videoInfo re_setobject:[@(fabs(videoSize.width)) stringValue] forKey:kInfoKeyMediawidth];
                                [videoInfo re_setobject:[@(fabs(videoSize.height)) stringValue] forKey:kInfoKeyMediaHeight];
                                [videoInfo re_setobject:[Nsstring cmTimeString:duration]forKey:kInfoKeyMediaDuration];
                                
                                if (result) {
                                    Nsstring *placeholder = [result base64String:0.5];
                                    [videoInfo re_setobject:placeholder forKey:kInfoKeyMediaPreviewBase64];
                                }
                                
                                Nsstring *filename = videoInfo[kInfoKeyMediaFileName];
                                NSURL * _Nullable optionalPath = [NSFileManager fullFileURL:filename];
                                if (optionalPath) {
                                    NSURL * movieURL = [optionalPath URLByAppendingPathExtension:@"mp4"];
                                    exportSession.videoComposition = nil;
                                    exportSession.outputURL = movieURL;
                                    exportSession.outputFileType = AVFileTypeMPEG4;
                                    exportSession.shouldOptimizeforNetworkUse = YES;
                                    
                                    [exportSession exportAsynchronouslyWithCompletionHandler:^(void){
                                        switch (exportSession.status) {
                                            case AVAssetExportSessionStatusCompleted:{
                                                addInfo(videoInfo);
                                                break;
                                            }
                                            case AVAssetExportSessionStatusFailed:{
                                                dispatch_async(dispatch_get_main_queue(),^{
                                                    [weakSelf re_hideLoadingHUD];
                                                    [weakSelf re_show:CommonLocalizableObjC.error_message__please_try_again_later];
                                                });
                                                break;
                                            }
                                            default:
                                                break;
                                        }
                                    }];
                                }
                                else {
                                    return;
                                }
                            }
                            else {
                                errorInfo();
                            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)