dfi*_*dfi 0 avfoundation avassetexportsession swift avkit ios11
我目前以以下方式导出视频:
let exporter = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPreset1280x720)
exporter?.outputURL = outputPath
exporter?.outputFileType = AVFileType.mp4
exporter?.shouldOptimizeForNetworkUse = true
exporter?.videoComposition = mainCompositionInst
Run Code Online (Sandbox Code Playgroud)
15秒的视频消耗约20MB的数据。与Snapchat的2MB视频相比,这个数字似乎是完全不能接受的。
我已经降低了导出和捕获会话的质量(1280x720)。
该视频在自定义相机上拍摄。UIImagePickerController未使用。
AVAssetExportSession与默认设置一起使用。
有什么办法可以减小视频尺寸?非常感谢!
编辑1: 我尝试使用此库:https : //cocoapods.org/pods/NextLevelSessionExporter
不幸的是,这会造成尺寸问题,并删除了我的音频:
// Creating exporter
let exporter = NextLevelSessionExporter(withAsset: mixComposition)
exporter.outputURL = outputPath
exporter.outputFileType = AVFileType.mp4
exporter.videoComposition = mainCompositionInst
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 2500000),
AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel as String,
]
exporter.videoOutputConfiguration = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: NSNumber(integerLiteral: 1280),
AVVideoHeightKey: NSNumber(integerLiteral: 720),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
exporter.audioOutputConfiguration = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderBitRateKey: NSNumber(integerLiteral: 128000),
AVNumberOfChannelsKey: NSNumber(integerLiteral: 2),
AVSampleRateKey: NSNumber(value: Float(44100))
]
Run Code Online (Sandbox Code Playgroud)
终于破解了这个。
使用exportSession.fileLengthLimit = 1048576 * 10 //10 MB
10MB 是硬编码数。根据您所需的比特率使用。
fileLengthLimit /* 指示会话输出不应超过的文件长度。根据源资源的内容,输出可能会稍微超出文件长度限制。如果您需要在使用输出之前遵守严格的限制,则应测试输出文件的长度。另请参见 maxDuration 和 timeRange。*/
要减少文件大小,请尝试使用以下属性来设置HEVC编解码器(使用cocoa pod NextLevelSessionExporter):
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 2500000), //lower it if you wish
AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel as String,
]
exporter.videoOutputConfiguration = [
AVVideoCodecKey : AVVideoCodecType.hevc,
AVVideoWidthKey : NSNumber(integerLiteral: 1280),
AVVideoHeightKey: NSNumber(integerLiteral: 720),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
Run Code Online (Sandbox Code Playgroud)
您需要升级到macOS High Sierra和iOS 11才能使用HEVC视频编解码器。但是,如果HEVC由于某种原因而无法使用,请使用H.264较低比特率的常规格式。
AVVideoCodecKey : AVVideoCodecType.h264:
Run Code Online (Sandbox Code Playgroud)
另外,请参阅这篇有关iOS中视频比特率的文章。
| 归档时间: |
|
| 查看次数: |
1645 次 |
| 最近记录: |