【发布时间】:2019-03-21 14:43:00
【问题描述】:
我正在尝试在文档目录中导出一个 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
}
}
}
failed Optional(Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x600003f432a0 {Error Domain=NSOSStatusErrorDomain Code=-16979 "(null)"}, NSLocalizedFailureReason=发生未知错误(-16979), NSURL=PATH_TO_MY_FILE.mp4, NSLocalizedDescription=操作无法完成})
【问题讨论】:
-
你是偶然解决的吗?
-
是的,问题是我没有创建使用 fileURLWithPath 传入的 url,如下所示。让 fileURL = URL(fileURLWithPath: filePath)
标签: swift avassetexportsession