gpt4 book ai didi

swift - AVAssetExportSession 给我 AVFoundationErrorDomain Code=-11800

转载 作者:行者123 更新时间:2023-12-02 11:05:01 29 4
gpt4 key购买 nike

我在真实设备中的 ios 13.3 中遇到了同样的问题,它在 ios 13.2 模拟器中工作,但给出了以下错误。

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-17508), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x2816d11d0 {Error Domain=NSOSStatusErrorDomain Code=-17508 "(null)"}}

这是我想要将 .mov 文件转换为 mp4 的代码。

class func encodeVideo(at videoURL: String, completionHandler: ((URL?, Error?) -> Void)?)  {  
let avAsset = AVURLAsset(url: URL.init(fileURLWithPath: videoURL), options: nil)

let startDate = Date()

//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}

//Creating temp path to save the converted video
let filename = "Video_\(Date().timeIntervalSince1970).mp4"
// Below Folder Path used tor getting directory path
let strfilePath = (FolderPath.temporaryDirectory.getDirectoryPath as NSString).appendingPathComponent(filename)
let filePath = URL.init(fileURLWithPath: strfilePath)

//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}

exportSession.outputURL = filePath
exportSession.outputFileType = AVFileType.mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range

exportSession.exportAsynchronously(completionHandler: {() -> Void in
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
//Video conversion finished
let endDate = Date()

let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)

default: break
}

})
}

最佳答案

let strfilePath = (FolderPath.temporaryDirectory.getDirectoryPath as NSString).appendingPathComponent(filename)  

您不能直接存储在该文件夹中,但您需要将文件存储在子文件夹中,例如像这样:

let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL

let strfilePath = documentDirectoryURL.appendingPathComponent("Subfolder/filename.mp4") as URL

您还可以阅读此article

关于swift - AVAssetExportSession 给我 AVFoundationErrorDomain Code=-11800,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59339411/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com