NextLevel
是一个专门为 iOS 设计的简单集成、定制化媒体抓取和图像流式的 Swift 相机系统。集成可以选择利用 AVFoundation
或 ARKit
。
功能 | |
---|---|
类似 Vine 的视频剪辑录制和编辑 | |
拍照功能(原始图像、jpeg 和视频帧) | |
自定义手势交互和界面 | |
ARKit 集成(测试版) | |
双摄、广角、长焦和真深度支持 | |
在支持的硬件上调节帧率(例如高速/慢动作捕获) | |
支持捕捉深度数据及真Portrait效果蒙板 | |
视频缩放 | |
自动白平衡、焦点和曝光调整 | |
支持闪光灯和手电筒 | |
镜像支持 | |
低光增强 | |
平滑自动对焦 | |
可配置的编码和压缩设置 | |
简单的媒体捕捉和编辑 API | |
可扩展的图像处理和计算机视觉 API | |
动态 GIF 创建器 | |
面部识别;二维码和条形码识别 | |
Swift 5 |
需要不同的 Swift 版本?
5.0
- 将您的 Podfile 指向最新版本或 master 分支4.2
- 将您的 Podfile 指向swift4.2
分支
快速开始
# CocoaPods
pod "NextLevel", "~> 0.16.3"
# Carthage
github "nextlevel/NextLevel" ~> 0.16.3
# Swift PM
let package = Package(
dependencies: [
.Package(url: "https://github.com/nextlevel/NextLevel", majorVersion: 0)
]
)
或者,将 NextLevel 源代码或项目文件拖入您的 Xcode 项目。
ARKit 和真深度重要配置说明
通过在 Swift 编译器中使用标志 USE_ARKIT
和 USE_TRUE_DEPTH
分别启用 ARKit 和True Depth摄像头软件功能。
苹果将拒绝使用 ARKit 或 True Depth Camera API 但未使用它们的应用程序。
如果您使用 Cocoapods,您可以通过以下方法包含 -D USE_ARKIT
或 -D USE_TRUE_DEPTH
:添加到 Podfile
或将其添加到您的 Xcode 构建设置中。
installer.pods_project.targets.each do |target|
# setup NextLevel for ARKit use
if target.name == 'NextLevel'
target.build_configurations.each do |config|
config.build_settings['OTHER_SWIFT_FLAGS'] = ['$(inherited)', '-DUSE_ARKIT']
end
end
end
概述
开始之前,请确保已在您的应用程序的 Info.plist
中添加了权限密钥。
<key>NSCameraUsageDescription</key>
<string>Allowing access to the camera lets you take photos and videos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Allowing access to the microphone lets you record audio.</string>
录制视频片段
导入库。
import NextLevel
设置相机预览。
let screenBounds = UIScreen.main.bounds
self.previewView = UIView(frame: screenBounds)
if let previewView = self.previewView {
previewView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
previewView.backgroundColor = UIColor.black
NextLevel.shared.previewLayer.frame = previewView.bounds
previewView.layer.addSublayer(NextLevel.shared.previewLayer)
self.view.addSubview(previewView)
}
配置捕获会话。
override func viewDidLoad() {
NextLevel.shared.delegate = self
NextLevel.shared.deviceDelegate = self
NextLevel.shared.videoDelegate = self
NextLevel.shared.photoDelegate = self
// modify .videoConfiguration, .audioConfiguration, .photoConfiguration properties
// Compression, resolution, and maximum recording time options are available
NextLevel.shared.videoConfiguration.maximumCaptureDuration = CMTimeMakeWithSeconds(5, 600)
NextLevel.shared.audioConfiguration.bitRate = 44000
}
在适当的时候开始/停止会话。这些方法在调用时为 'NextLevel.shared.session' 创建一个新的 "会话" 实例。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NextLevel.shared.start()
// …
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NextLevel.shared.stop()
// …
}
视频录制/暂停。
// record
NextLevel.shared.record()
// pause
NextLevel.shared.pause()
编辑已录制剪辑
编辑并最终确定已录制会话。
if let session = NextLevel.shared.session {
//..
// undo
session.removeLastClip()
// various editing operations can be done using the NextLevelSession methods
// export
session.mergeClips(usingPreset: AVAssetExportPresetHighestQuality, completionHandler: { (url: URL?, error: Error?) in
if let _ = url {
//
} else if let _ = error {
//
}
})
//..
}
可以使用 NextLevelSessionExporter 对视频进行处理,这是一个 Swift 中的媒体转码库。
自定义缓冲区渲染
“NextLevel”是为在实时中进行样本缓冲区分析和自定义修改以及丰富的相机功能而设计的。
需要注意的是,对缓冲区进行的修改并返回给NextLevel可能会影响帧率。
启用自定义渲染。
NextLevel.shared.isVideoCustomContextRenderingEnabled = true
可选钩子,允许读取 sampleBuffer
进行分析。
extension CameraViewController: NextLevelVideoDelegate {
// ...
// video frame processing
public func nextLevel(_ nextLevel: NextLevel, willProcessRawVideoSampleBuffer sampleBuffer: CMSampleBuffer) {
// Use the sampleBuffer parameter in your system for continual analysis
}
另一个可选钩子用于读取缓冲区以进行修改,imageBuffer
。这也是向NextLevel提供缓冲区进行录制的推荐位置。
extension CameraViewController: NextLevelVideoDelegate {
// ...
// enabled by isCustomContextVideoRenderingEnabled
public func nextLevel(_ nextLevel: NextLevel, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue) {
// provide the frame back to NextLevel for recording
if let frame = self._availableFrameBuffer {
nextLevel.videoCustomContextImageBuffer = frame
}
}
NextLevel在将缓冲区写入目标文件时会检查这个属性。对于使用capturePhotoFromVideo
的视频和照片都适用。
nextLevel.videoCustomContextImageBuffer = modifiedFrame
关于
NextLevel最初是一个周末项目,现在已经发展成为一个开放的相机平台爱好者社区。该软件为iOS平台上媒体录制、相机界面定制、手势交互定制和图像流提供了基础组件。同样的功能也可以在Snapchat、Instagram和Vine等应用程序中找到。
目标是继续提供良好的基础,以实现快速集成(使项目提升到一个新的水平),专注于最重要的功能,无论它是实时图像处理、计算机视觉方法、增强现实还是计算摄影。
ARKit
NextLevel提供了捕获ARKit视频和照片的组件。这允许利用现有记录能力和NextLevel媒体管理功能来实现多种新的相机功能。
如果您正在尝试从SceneKit为ARKit录制捕获帧,请查看示例项目。
文档
您可以在这里找到文档。文档使用jazzy生成,并在GitHub-Pages上托管。
贴纸
如果您觉得这个项目很有帮助,请查看Next Level贴纸。
项目
NextLevel 是一个社区 —— 欢迎贡献和讨论!
- 有功能想法?请打开问题。
- 发现bug?请打开问题。
- 需要帮助?使用带有标签 'nextlevel' 的 Stack Overflow。
- 有问题?使用带有标签 'nextlevel' 的 Stack Overflow。
- 想贡献力量?提交一个拉取请求。
相关项目
- Player (Swift),Swift编写的视频播放器
- PBJVideoPlayer (obj-c),Objective-C编写的视频播放器
- NextLevelSessionExporter,Swift编写的媒体转码器
- GPUImage3,图像处理库
- SCRecorder,Objective-C捕获库
- PBJVision,Objective-C捕获库
资源
- iOS 设备摄像头总结
- AV Foundation 编程指南
- AV Foundation 框架参考
- ARKit 框架参考
- Swift Evolution
- objc.io 摄像机和照片
- objc.io 视频
- objc.io 核心图像和视频
- 摄像头、电子商务和机器学习
- 再次强调,iPhone 的摄像头是默认的摄像头
许可证
NextLevel 使用MIT许可,更多信息请参阅LICENSE文件。