Trim 使用 AVAssetExportPresetPassthrough 时视频总是失败
Trim video always fail when use AVAssetExportPresetPassthrough
我的剪辑视频代码:
func trim(createNew: Bool, with name: String?, file: File, startTime: Double, endTime: Double, completion: @escaping ResultCompletion<File>) {
guard checkFreeSpace(with: file) else {
return handleFailure(error: .fullStorage, completion: completion)
}
guard let originUrl = file.localUrl(),
let originName = file.name.components(separatedBy: ".").first,
let originExtension = file.name.components(separatedBy: ".").last else {
return handleFailure(error: .mediaSavingError, completion: completion)
}
let asset = AVAsset(url: originUrl)
let timeZoneOffset = TimeInterval(TimeZone.current.secondsFromGMT())
let newDate = Date().addingTimeInterval(timeZoneOffset)
let temporaryFileId = "\(file.device ?? "Unknown")-\(newDate.milliseconds)-0-\(originName)_trim.\(originExtension)"
let outputUrl = file.buildLocalUrl(id: temporaryFileId)
AVAssetExportSession.determineCompatibility(
ofExportPreset: AVAssetExportPresetPassthrough,
with: asset,
outputFileType: .mp4) { isCompatible in
print("Can Trim MP4: ", isCompatible)
}
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else {
return handleFailure(error: .mediaSavingError, completion: completion)
}
exportSession.outputURL = outputUrl
exportSession.outputFileType = .mp4
let timeRange = CMTimeRange(start: CMTime(seconds: startTime, preferredTimescale: 900),
end: CMTime(seconds: endTime, preferredTimescale: 900))
exportSession.timeRange = timeRange
exportSession.exportAsynchronously { [weak self] in
guard let self = self else {
return
}
exportProgressTimer.invalidate()
switch exportSession.status {
case .completed:
completion(.success(file))
case .failed:
self.handleFailure(error: .mediaSavingError, completion: completion)
case .cancelled:
completion(.failure(FileError.mediaExportCanceled))
default:
break
}
}
}
确定兼容性return 正确
当使用 AVAssetExportSession.exportPresets(compatibleWith: item.asset) 中包含的另一个预设时,一切都很棒
也许有人可以解释哪里出了问题,我该如何解决?我需要与原始视频相同的分辨率
或者如何使用 AVAssetWriter
和 AVAssetReader
进行转换?
我在另一个问题写答案,他们有同样的问题,但方式不同。我就是这样解决问题的
我的剪辑视频代码:
func trim(createNew: Bool, with name: String?, file: File, startTime: Double, endTime: Double, completion: @escaping ResultCompletion<File>) {
guard checkFreeSpace(with: file) else {
return handleFailure(error: .fullStorage, completion: completion)
}
guard let originUrl = file.localUrl(),
let originName = file.name.components(separatedBy: ".").first,
let originExtension = file.name.components(separatedBy: ".").last else {
return handleFailure(error: .mediaSavingError, completion: completion)
}
let asset = AVAsset(url: originUrl)
let timeZoneOffset = TimeInterval(TimeZone.current.secondsFromGMT())
let newDate = Date().addingTimeInterval(timeZoneOffset)
let temporaryFileId = "\(file.device ?? "Unknown")-\(newDate.milliseconds)-0-\(originName)_trim.\(originExtension)"
let outputUrl = file.buildLocalUrl(id: temporaryFileId)
AVAssetExportSession.determineCompatibility(
ofExportPreset: AVAssetExportPresetPassthrough,
with: asset,
outputFileType: .mp4) { isCompatible in
print("Can Trim MP4: ", isCompatible)
}
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else {
return handleFailure(error: .mediaSavingError, completion: completion)
}
exportSession.outputURL = outputUrl
exportSession.outputFileType = .mp4
let timeRange = CMTimeRange(start: CMTime(seconds: startTime, preferredTimescale: 900),
end: CMTime(seconds: endTime, preferredTimescale: 900))
exportSession.timeRange = timeRange
exportSession.exportAsynchronously { [weak self] in
guard let self = self else {
return
}
exportProgressTimer.invalidate()
switch exportSession.status {
case .completed:
completion(.success(file))
case .failed:
self.handleFailure(error: .mediaSavingError, completion: completion)
case .cancelled:
completion(.failure(FileError.mediaExportCanceled))
default:
break
}
}
}
确定兼容性return 正确
当使用 AVAssetExportSession.exportPresets(compatibleWith: item.asset) 中包含的另一个预设时,一切都很棒
也许有人可以解释哪里出了问题,我该如何解决?我需要与原始视频相同的分辨率
或者如何使用 AVAssetWriter
和 AVAssetReader
进行转换?
我在另一个问题写答案,他们有同样的问题,但方式不同。我就是这样解决问题的