Swiftpack.co - gorastudio/SCNRecorder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
gorastudio/SCNRecorder
The best way to record your AR experience!
.package(url: "https://github.com/gorastudio/SCNRecorder.git", from: "2.7.0")

SCNRecorder

GitHub license Platforms Swift Cocoapods compatible Carthage compatible Swift Package Manager

SCNRecorder allows you to record videos and to capture images from ARSCNView, SCNView and ARView (RealityKit) without sacrificing performance. It gives you an incredible opportunity to share the media content of your augmented reality app or SceneKit based game.

Starting version 2.2.0 SCNRecorder supports Metal only.

Sample

Requirements

  • iOS 12.0+
  • Xcode 12.0+
  • Swift 5.0+

Installation

CocoaPods

pod 'SCNRecorder', '~> 2.7'

Carthage

github "gorastudio/SCNRecorder"

Usage

Import the SCNRecorder module.

import SCNRecorder

Call sceneView.prepareForRecording() at viewDidLoad.

@IBOutlet var sceneView: SCNView!

override func viewDidLoad() {
  super.viewDidLoad()
  sceneView.prepareForRecording()
}

And now you can use new functions to capture videos.

try sceneView.startVideoRecording()
sceneView.finishVideoRecording { (videoRecording) in 
  /* Process the captured video. Main thread. */
  let controller = AVPlayerViewController()
  controller.player = AVPlayer(url: recording.url)
  self.navigationController?.pushViewController(controller, animated: true)
}

To capture an image it is enough to call:

sceneView.takePhoto { (photo: UIImage) in
  /* Your photo is now here. Main thread. */
}

or

sceneView.takePhotoResult { (result: Result<UIImage, Swift.Error>) in
  /* Result is here. Main thread. */
}

Look at the Example project for more details.

Audio capture

ARSCNView

To capture video with audio from ARSCNView enable audio in the ARConfiguration.

let configuration = ARWorldTrackingConfiguration()
configuration.providesAudioData = true
sceneView.session.run(configuration)

SCNView

To capture audio from SCNView you have to implement it by yourself.

var captureSession: AVCaptureSession?

override func viewDidLoad() {
  super.viewDidLoad()
  sceneView.prepareForRecording()
  
  guard let recorder = sceneView.recorder else { return }
  let captureSession = AVCaptureSession()
  
  guard let captureDevice = AVCaptureDevice.default(for: .audio) else { return }
  
  do {
    let captureInput = try AVCaptureDeviceInput(device: captureDevice)
    
    guard captureSession.canAddInput(captureInput) else { return }
    captureSession.addInput(captureInput)
  }
  catch { print("Can't create AVCaptureDeviceInput: \(error)")}
  
  guard captureSession.canAddRecorder(recorder) else { return }
  captureSession.addRecorder(recorder)
  
  captureSession.startRunning()
  self.captureSession = captureSession
}

or, simply

var captureSession: AVCaptureSession?

override func viewDidLoad() {
  super.viewDidLoad()
  sceneView.prepareForRecording()
  
  captureSession = try? .makeAudioForRecorder(sceneView.recorder!)
}

Music Overlay

Instead of capturing audio using microphone you can play music and add it to video at the same time.


let auidoEngine = AudioEngine()

override func viewDidLoad() {
  super.viewDidLoad()
  
  sceneView.prepareForRecording()
  do {
    audioEngine.recorder = sceneView.recorder
    
    // If true, use sound data from audioEngine if any
    // If false, use sound data ARSession/AVCaptureSession if any
    sceneView.recorder?.useAudioEngine = true
    
    let player = try AudioEngine.Player(url: url)
    audioEngine.player = player
    
    player.play()
  }
  catch { 
    print(\(error))
  }
}

RealityKit

To support recording RealityKit, copy ARView+MetalRecordable.swift and ARView+SelfSceneRecordable.swift files to your project. Then look at RealityKitViewController.swift for usage.

That's it!

Look at the Example project for more details.

Author

Thanks to Fedor Prokhorov and Dmitry Yurlov for testing, reviewing and inspiration.

License

This project is licensed under the MIT License - see the LICENSE file for details

GitHub

link
Stars: 104
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

iOS 15 and SPM support
2 weeks ago
  • Move MulticastDelegate to a separate repository and use it as a dependency;
  • Add SMP support;
  • Tested with iOS 15;

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics