AVAssetExportSession 将视频导出到照片库

问题描述

我正在尝试 1) 从照片库中导入视频,2) 对其进行一些处理,然后 3) 以特定分辨率将其作为新视频导出回照片库。

我有第 1 步和第 2 步,但我不知道如何使用特定的 exportPreset 将视频导出回照片库。我尝试跟随 this,但我不确定我是否完全理解如何设置它。我没有任何错误,但即使我的 exportPreset 小于原始分辨率,视频仍以原始分辨率导出。

func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey: Any]) {
        dismiss(animated: true,completion: nil)
        
        guard
            let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,mediaType == (kUTTypeMovie as String),let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL
        else { return }
        
        let asset = AVAsset.init(url: url)
        let outputURL = url
        let outFileType = AVFileType.mov
        
        let newRes = resolutionForLocalVideo(url: url)
        
        AVAssetExportSession.determineCompatibility(ofExportPreset: exportPreset,with: asset,outputFileType: outFileType) { isCompatible in
            guard isCompatible else { return }
            guard let exportSession = AVAssetExportSession(asset: asset,presetName: self.exportPreset) else { return }
            exportSession.outputFileType = outFileType
            exportSession.outputURL = outputURL
            exportSession.exportAsynchronously {
                //https://stackoverflow.com/questions/35479903/swift-download-a-video-from-distant-url-and-save-it-in-an-photo-album#35486290
                PHPhotoLibrary.shared().performChanges({
                                                        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)}) {
                    saved,error in
                    //https://stackoverflow.com/questions/52808945/dispatchqueue-cannot-be-called-with-ascopy-no-on-non-main-thread
                    dispatchQueue.main.async {
                        let title = (error == nil) ? "Success" : "Error"
                        var message = ""
                        if (self.exportPreset == AVAssetExportPresetHEVC3840x2160 || self.exportPreset == AVAssetExportPresetHEVC1920x1080) {
                            message = (error == nil) ? "Video resized to \(newRes!) HDR" : "Video Failed to resize"
                        } else {
                            message = (error == nil) ? "Video resized to \(newRes!)" : "Video Failed to resize"
                        }
                        self.displayAlert(title: title,message: message)
                    }
                }
            }
        }
    }

解决方法

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

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

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