NextLevelSessionExporter
是一个用于 iOS 的导出和转码媒体库,使用 Swift 编写。
该库提供了可自定义的音频和视频编码选项,这与 AVAssetExportSession
不同,并且无需学习 AVFoundation 的复杂性。它是 SDAVAssetExportSession 的移植版本,并从 SCAssetExportSession 中汲取了灵感 – 它们都是很棒的 obj-c 替代方案。
需要不同版本的 Swift?
5.0
- 将您的 Podfile 目标设置为最新版本或 master 分支4.2
- 将您的 Podfile 目标设置为 swift4.2
分支4.0
- 将您的 Podfile 目标设置为 swift4.0
分支# CocoaPods
pod "NextLevelSessionExporter", "~> 0.4.5"
# Carthage
github "nextlevel/NextLevelSessionExporter" ~> 0.4.5
# Swift PM
let package = Package(
dependencies: [
.Package(url: "https://github.com/nextlevel/NextLevelSessionExporter", majorVersion: 0)
]
)
或者,将 源文件 拖放到您的 Xcode 项目中。
只需使用 AVAsset
扩展,或直接创建和使用 NextLevelSessionExporter
的实例。
let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(ProcessInfo().globallyUniqueString)
.appendingPathExtension("mp4")
exporter.outputURL = tmpURL
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 6000000),
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String,
]
let videoOutputConfig = [
AVVideoCodecKey: AVVideoCodec.h264,
AVVideoWidthKey: NSNumber(integerLiteral: 1920),
AVVideoHeightKey: NSNumber(integerLiteral: 1080),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
let audioOutputConfig = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderBitRateKey: NSNumber(integerLiteral: 128000),
AVNumberOfChannelsKey: NSNumber(integerLiteral: 2),
AVSampleRateKey: NSNumber(value: Float(44100))
]
let asset = AVAsset(url: Bundle.main.url(forResource: "TestVideo", withExtension: "mov")!)
asset.nextlevel_export(outputURL: tmpURL, videoOutputConfiguration: videoOutputConfig, audioOutputConfiguration: audioOutputConfig)
或者,您可以直接使用 NextLevelSessionExporter
。
let exporter = NextLevelSessionExporter(withAsset: asset)
exporter.outputFileType = AVFileType.mp4
let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(ProcessInfo().globallyUniqueString)
.appendingPathExtension("mp4")
exporter.outputURL = tmpURL
let compressionDict: [String: Any] = [
AVVideoAverageBitRateKey: NSNumber(integerLiteral: 6000000),
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String,
]
exporter.videoOutputConfiguration = [
AVVideoCodecKey: AVVideoCodec.h264,
AVVideoWidthKey: NSNumber(integerLiteral: 1920),
AVVideoHeightKey: NSNumber(integerLiteral: 1080),
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: compressionDict
]
exporter.audioOutputConfiguration = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderBitRateKey: NSNumber(integerLiteral: 128000),
AVNumberOfChannelsKey: NSNumber(integerLiteral: 2),
AVSampleRateKey: NSNumber(value: Float(44100))
]
exporter.export(progressHandler: { (progress) in
print(progress)
}, completionHandler: { result in
switch result {
case .success(let status):
switch status {
case .completed:
print("NextLevelSessionExporter, export completed, \(exporter.outputURL?.description ?? "")")
break
default:
print("NextLevelSessionExporter, did not complete")
break
}
break
case .failure(let error):
print("NextLevelSessionExporter, failed to export \(error)")
break
}
})
您可以在此处找到文档。文档使用 jazzy 生成,并托管在 GitHub-Pages 上。
NextLevelSessionExporter
在 MIT 许可证下可用,有关更多信息,请参阅 LICENSE 文件。