项目作者: NextLevel

项目描述 :
⬆️Swift中的Rad Media捕获
高级语言: Swift
项目地址: git://github.com/NextLevel/NextLevel.git
创建时间: 2015-03-24T22:34:27Z
项目社区:https://github.com/NextLevel/NextLevel

开源协议:MIT License

下载


Next Level

NextLevel is a Swift camera system designed for easy integration, customized media capture, and image streaming in iOS. Integration can optionally leverage AVFoundation or ARKit.

Build Status Pod Version Swift Version GitHub license

Features
🎬 Vine-like” video clip recording and editing
🖼 photo capture (raw, jpeg, and video frame)
👆 customizable gestural interaction and interface
💠 ARKit integration (beta)
📷 dual, wide angle, telephoto, & true depth support
🐢 adjustable frame rate on supported hardware (ie fast/slow motion capture)
🎢 depth data capture support & portrait effects matte support
🔍 video zoom
white balance, focus, and exposure adjustment
🔦 flash and torch support
👯 mirroring support
low light boost
🕶 smooth auto-focus
configurable encoding and compression settings
🛠 simple media capture and editing API
🌀 extensible API for image processing and CV
🐈 animated GIF creator
😎 face recognition; qr- and bar-codes recognition
🐦 Swift 5

Need a different version of Swift?

  • 5.0 - Target your Podfile to the latest release or master
  • 4.2 - Target your Podfile to the swift4.2 branch

Quick Start

  1. # CocoaPods
  2. pod "NextLevel", "~> 0.16.3"
  3. # Carthage
  4. github "nextlevel/NextLevel" ~> 0.16.3
  5. # Swift PM
  6. let package = Package(
  7. dependencies: [
  8. .Package(url: "https://github.com/nextlevel/NextLevel", majorVersion: 0)
  9. ]
  10. )

Alternatively, drop the NextLevel source files or project file into your Xcode project.

Important Configuration Note for ARKit and True Depth

ARKit and the True Depth Camera software features are enabled with the inclusion of the Swift compiler flag USE_ARKIT and USE_TRUE_DEPTH respectively.

Apple will reject apps that link against ARKit or the True Depth Camera API and do not use them.

If you use Cocoapods, you can include -D USE_ARKIT or -D USE_TRUE_DEPTH with the following Podfile addition or by adding it to your Xcode build settings.

  1. installer.pods_project.targets.each do |target|
  2. # setup NextLevel for ARKit use
  3. if target.name == 'NextLevel'
  4. target.build_configurations.each do |config|
  5. config.build_settings['OTHER_SWIFT_FLAGS'] = ['$(inherited)', '-DUSE_ARKIT']
  6. end
  7. end
  8. end

Overview

Before starting, ensure that permission keys have been added to your app’s Info.plist.

  1. <key>NSCameraUsageDescription</key>
  2. <string>Allowing access to the camera lets you take photos and videos.</string>
  3. <key>NSMicrophoneUsageDescription</key>
  4. <string>Allowing access to the microphone lets you record audio.</string>

Recording Video Clips

Import the library.

  1. import NextLevel

Setup the camera preview.

  1. let screenBounds = UIScreen.main.bounds
  2. self.previewView = UIView(frame: screenBounds)
  3. if let previewView = self.previewView {
  4. previewView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  5. previewView.backgroundColor = UIColor.black
  6. NextLevel.shared.previewLayer.frame = previewView.bounds
  7. previewView.layer.addSublayer(NextLevel.shared.previewLayer)
  8. self.view.addSubview(previewView)
  9. }

Configure the capture session.

  1. override func viewDidLoad() {
  2. NextLevel.shared.delegate = self
  3. NextLevel.shared.deviceDelegate = self
  4. NextLevel.shared.videoDelegate = self
  5. NextLevel.shared.photoDelegate = self
  6. // modify .videoConfiguration, .audioConfiguration, .photoConfiguration properties
  7. // Compression, resolution, and maximum recording time options are available
  8. NextLevel.shared.videoConfiguration.maximumCaptureDuration = CMTimeMakeWithSeconds(5, 600)
  9. NextLevel.shared.audioConfiguration.bitRate = 44000
  10. }

Start/stop the session when appropriate. These methods create a new “session” instance for ‘NextLevel.shared.session’ when called.

  1. override func viewWillAppear(_ animated: Bool) {
  2. super.viewWillAppear(animated)
  3. NextLevel.shared.start()
  4. // …
  5. }
  1. override func viewWillDisappear(_ animated: Bool) {
  2. super.viewWillDisappear(animated)
  3. NextLevel.shared.stop()
  4. // …
  5. }

Video record/pause.

  1. // record
  2. NextLevel.shared.record()
  3. // pause
  4. NextLevel.shared.pause()

Editing Recorded Clips

Editing and finalizing the recorded session.

  1. if let session = NextLevel.shared.session {
  2. //..
  3. // undo
  4. session.removeLastClip()
  5. // various editing operations can be done using the NextLevelSession methods
  6. // export
  7. session.mergeClips(usingPreset: AVAssetExportPresetHighestQuality, completionHandler: { (url: URL?, error: Error?) in
  8. if let _ = url {
  9. //
  10. } else if let _ = error {
  11. //
  12. }
  13. })
  14. //..
  15. }

Videos can also be processed using the NextLevelSessionExporter, a media transcoding library in Swift.

Custom Buffer Rendering

‘NextLevel’ was designed for sample buffer analysis and custom modification in real-time along side a rich set of camera features.

Just to note, modifications performed on a buffer and provided back to NextLevel may potentially effect frame rate.

Enable custom rendering.

  1. NextLevel.shared.isVideoCustomContextRenderingEnabled = true

Optional hook that allows reading sampleBuffer for analysis.

  1. extension CameraViewController: NextLevelVideoDelegate {
  2. // ...
  3. // video frame processing
  4. public func nextLevel(_ nextLevel: NextLevel, willProcessRawVideoSampleBuffer sampleBuffer: CMSampleBuffer) {
  5. // Use the sampleBuffer parameter in your system for continual analysis
  6. }

Another optional hook for reading buffers for modification, imageBuffer. This is also the recommended place to provide the buffer back to NextLevel for recording.

  1. extension CameraViewController: NextLevelVideoDelegate {
  2. // ...
  3. // enabled by isCustomContextVideoRenderingEnabled
  4. public func nextLevel(_ nextLevel: NextLevel, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue) {
  5. // provide the frame back to NextLevel for recording
  6. if let frame = self._availableFrameBuffer {
  7. nextLevel.videoCustomContextImageBuffer = frame
  8. }
  9. }

NextLevel will check this property when writing buffers to a destination file. This works for both video and photos with capturePhotoFromVideo.

  1. nextLevel.videoCustomContextImageBuffer = modifiedFrame

About

NextLevel was initally a weekend project that has now grown into a open community of camera platform enthusists. The software provides foundational components for managing media recording, camera interface customization, gestural interaction customization, and image streaming on iOS. The same capabilities can also be found in apps such as Snapchat, Instagram, and Vine.

The goal is to continue to provide a good foundation for quick integration (enabling projects to be taken to the next level) – allowing focus to placed on functionality that matters most whether it’s realtime image processing, computer vision methods, augmented reality, or computational photography.

ARKit

NextLevel provides components for capturing ARKit video and photo. This enables a variety of new camera features while leveraging the existing recording capabilities and media management of NextLevel.

If you are trying to capture frames from SceneKit for ARKit recording, check out the examples project.

Documentation

You can find the docs here. Documentation is generated with jazzy and hosted on GitHub-Pages.

Stickers

If you found this project to be helpful, check out the Next Level stickers.

Project

NextLevel is a community – contributions and discussions are welcome!

  • Feature idea? Open an issue.
  • Found a bug? Open an issue.
  • Need help? Use Stack Overflow with the tag ’nextlevel’.
  • Questions? Use Stack Overflow with the tag ‘nextlevel’.
  • Want to contribute? Submit a pull request.

Resources

License

NextLevel is available under the MIT license, see the LICENSE file for more information.