Swift Export MP4 using AVAssetExportSession



我正在尝试导出文档目录中的mp4文件。我正在从远程 URL 下载该文件,他们尝试修剪它并从现有 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 Code=-11800 "无法完成操作" UserInfo={NSUnderlyingError=0x600003f432a0 {Error Domain=NSOSStatusErrorDomain Code=-16979 "(null("}, NSLocalizedFailureReason=发生未知错误 (-16979(, NSURL=PATH_TO_MY_FILE.mp4, NSLocalizedDescription=无法完成操作}(

从他的评论中浮现出维克多自己的答案:

问题是我没有创建我使用 fileURLWithPath

传入的 URL,如下所示。 let fileURL = URL(fileURLWithPath: filePath(

相关内容

  • 没有找到相关文章

最新更新