【发布时间】:2020-04-11 01:56:47
【问题描述】:
我正在构建一个视频修剪器和导出器,并希望根据修剪的时间范围和导出预设值显示视频的估计输出文件大小。我正在测试的源视频约为 31MB。
AVAssetExportPresetLowQuality 和AVAssetExportPresetMediumQuality 预设估计相当准确。他们报告的估计输出大小分别为 1.6MB 和 7.8MB,实际导出的视频基本上是这个大小。
但是,所有其他有效的导出预设质量都非常不准确。例如,AVAssetExportPresetHighestQuality 以 31MB 的原始文件大小导出,但估计导出为 257MB。甚至没有关闭。
有人知道为什么这些值相差这么远吗?这是我的示例导出代码以及估计的和实际的文件大小。
let presets = [
AVAssetExportPresetLowQuality,
AVAssetExportPresetMediumQuality,
AVAssetExportPresetHighestQuality,
AVAssetExportPreset640x480,
AVAssetExportPreset960x540,
AVAssetExportPreset1280x720,
AVAssetExportPreset1920x1080
]
let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useMB, .useKB]
let url = Bundle.main.url(forResource: "big_buck_bunny", withExtension: "mp4")!
let asset = AVURLAsset(url: url)
for preset in presets {
let session = AVAssetExportSession(asset: asset, presetName: preset)!
let output = (NSTemporaryDirectory() as NSString).appendingPathComponent(preset + ".mp4")
session.timeRange = CMTimeRange(start: .zero, end: asset.duration)
session.outputURL = URL(fileURLWithPath: output)
session.outputFileType = .mp4
session.shouldOptimizeForNetworkUse = true
print("Estimated size for \(preset): \(formatter.string(fromByteCount: Int64(session.estimatedOutputFileLength)))")
session.exportAsynchronously {
print(session.outputURL)
}
}
报告的估计输出大小:
Estimated size for AVAssetExportPresetLowQuality: 1.6 MB
Estimated size for AVAssetExportPresetMediumQuality: 7.8 MB
Estimated size for AVAssetExportPresetHighestQuality: 257.6 MB
Estimated size for AVAssetExportPreset640x480: 37 MB
Estimated size for AVAssetExportPreset960x540: 54.9 MB
Estimated size for AVAssetExportPreset1280x720: 109.7 MB
Estimated size for AVAssetExportPreset1920x1080: 155.6 MB
实际输出尺寸:
Actual size for AVAssetExportPresetLowQuality: 1.6 MB
Actual size for AVAssetExportPresetMediumQuality: 7.8 MB
Actual size for AVAssetExportPresetHighestQuality: 30.7 MB
Actual size for AVAssetExportPreset640x480: 27 MB
Actual size for AVAssetExportPreset960x540: 51.5 MB
Actual size for AVAssetExportPreset1280x720: 100.8 MB
Actual size for AVAssetExportPreset1920x1080: 30.7 MB
此外,奇怪的是 960x540 和 1280x720 以大于原始尺寸(分辨率为 1920x1080)导出。
【问题讨论】:
-
我最近学到的一件事是,似乎没有人对iOS视频和图像压缩有太多了解:(
标签: ios video export avfoundation avassetexportsession