我正在尝试在documents目录中导出mp4文件。我正在从远程URL下载该文件,他们尝试对其进行修剪,并从现有文件中创建新的mp4。下面是我正在使用的代码的异常。这种方法适用于.mov文件和远程视频,但不适用于mp4。
func trimVideo(sourceURL: URL, startTime: Double, endTime: Double) {
let fileManager = FileManager.default
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let asset = AVAsset(url: sourceURL)
let fileName = UUID().uuidString + ".mp4"
var outputURL = documentDirectory.appendingPathComponent("output")
do {
try fileManager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
outputURL = outputURL.appendingPathComponent(fileName)
}catch let error {
print(error)
}
//Remove existing file
try? fileManager.removeItem(at: outputURL)
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset640x480) else { return }
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileType.mp4
let timeRange = CMTimeRange(start: CMTime(seconds: startTime, preferredTimescale: 1000),
end: CMTime(seconds: endTime, preferredTimescale: 1000))
exportSession.timeRange = timeRange
exportSession.exportAsynchronously {
switch exportSession.status {
case .completed:
print(outputURL.absoluteString)
case .failed:
print("failed \(exportSession.error.debugDescription)")
case .cancelled:
print("cancelled \(exportSession.error.debugDescription)")
default: break
}
}
}
失败可选(错误域= AVFoundationErrorDomain代码= -11800“操作无法完成” UserInfo = {NSUnderlyingError = 0x600003f432a0 {Error Domain = NSOSStatusErrorDomain Code = -16979“(null)”}),NSLocalizedFailureReason =发生未知错误(-16979),NSURL = PATH_TO_MY_FILE.mp4,NSLocalizedDescription =操作无法完成})
答案 0 :(得分:0)
从Victor的评论中得出自己的答案:
问题是我没有按以下方式创建通过fileURLWithPath传递的URL。让fileURL = URL(fileURLWithPath:filePath)