Skip to main content
Version: iOS 1.0.12

Recording Video Clips

Setup#

Import VideoKitRecorder.

import VideoKitRecorder

Setup the camera preview.

internal var previewView: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .black
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.clipsToBounds = true
view.backgroundColor = .black
return view
}()
func viewDidLoad() {
super.viewDidLoad()
VKRecorder.shared.previewLayer.frame = previewView.bounds
previewView.layer.addSublayer(VKRecorder.shared.previewLayer)
}
func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
VKRecorder.shared.previewLayer.frame = previewView.bounds
}

Setup and configure a capture session.

func startCamera() {
if VKRecorder.authorizationStatus(forMediaType: AVMediaType.video) == .authorized && VKRecorder.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
try VKRecorder.shared.startSession()
} else {
VKRecorder.requestAuthorization(forMediaType: .video) { (mediaType, status) in
print("VKRecorder, authorization updated for media \(mediaType) status \(status)")
if VKRecorder.authorizationStatus(forMediaType: AVMediaType.video) == .authorized &&
VKRecorder.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
self.startCamera()
}
}
VKRecorder.requestAuthorization(forMediaType: AVMediaType.audio) { (mediaType, status) in
print("VKRecorder, authorization updated for media \(mediaType) status \(status)")
if VKRecorder.authorizationStatus(forMediaType: AVMediaType.video) == .authorized &&
VKRecorder.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
self.startCamera()
}
}
}
}
func viewDidLoad() {
let vkRecorder = VKRecorder.shared
vkRecorder.delegate = self
vkRecorder.videoDelegate = self
vkRecorder.flashDelegate = self
vkRecorder.deviceDelegate = self
vkRecorder.frameRate = 30
// Custom context rendering enables you to change every video frame (e.g. add a sticker, watermark, etc. in realtime)
vkRecorder.isVideoCustomContextRenderingEnabled = false
vkRecorder.videoConfiguration.maximumCaptureDuration = CMTime(seconds: recordingTime, preferredTimescale: 600)
vkRecorder.videoConfiguration.bitRate = 6000000
vkRecorder.videoConfiguration.maxKeyFrameInterval = 30
vkRecorder.videoConfiguration.aspectRatio = .instagram
vkRecorder.videoConfiguration.dimensions = CGSize(width: 1080, height: 1920)
vkRecorder.videoConfiguration.scalingMode = AVVideoScalingModeResizeAspect
vkRecorder.videoConfiguration.codec = AVVideoCodecType.h264
vkRecorder.videoConfiguration.preset = .high
vkRecorder.audioConfiguration.preset = .high
}
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if VKRecorder.shared.canCaptureVideo {
startCamera()
} else {
print("Video capturing is not available right now. Please stop any other capturing session.")
}
}

Start Session#

Start/stop the session when appropriate. These methods create a new "session" instance for 'VKRecorder.shared.session' when called.

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
VKRecorder.shared.startSession()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
VKRecorder.shared.endSession()
}

Record#

Our VideoKit recorder offers two different modes to record your video

  • Multi-clip and
  • Single video

Multi-clip mode is used to record several clips that can later on be rearranged, trimmed or deleted individually. This mode is very flexible since it enables you to offer editing options for all recordings of your session. We would not recommend using multi-clip recording if you just need one quick recording and you do not want to allow rearranging or editing of single clips (e.g. in a messaging app). We help minimizing waiting times for the user by offering auto-merging but still there might be waiting times involved for putting the clips together.

Single video mode is recording all clips into one big video file automatically. With this mode, you can pause and start your recording and all subsequent videos are automatically stitched to the end of the previous recording. There is no merging required at the end and this mode also provides our Upload While Recording functionality for instantaeous availability of recordings.

Let's get into the basics of these two modes:

Multi-clip mode#

This mode lets you record one clip at a time, clips are then available for you to edit. Start off with enabling multi clip mode:

You can put this code in your viewDidLoad, right next to your other videoConfiguration settings. Please note that you can only enable this mode before you start your video session (VKRecorder.shared.startSession()).

// This setting enables you to record multiple clips that you can rearrange later on
vkRecorder.videoConfiguration.recordMultipleClips = true

Record and Pause#

// Append three recordings as new clips
VKRecorder.shared.record()
VKRecorder.shared.pause()
VKRecorder.shared.record()
VKRecorder.shared.pause()
VKRecorder.shared.record()
VKRecorder.shared.pause()

Your Session now holds three clips

if let session = VKRecorder.shared.session {
// session.clips.count == 3
}

Undo Last Clip Recording#

if let session = VKRecorder.shared.session {
// Undo
session.removeLastClip()
}

Remove a Specific Clip#

Since our multi-clip mode writes multiple video clips into the video session, you are able to remove and rearrange clips.

Remove second clip:

session.remove(clipAt: 1, removeFile: true)

Retrieve Thumbnail Images#

We offer a way to retrieve thumbnail images for each clip which will make it easy for you to display your recordings:

session.clips.forEach { (clip) in
// clip.thumbnailImage
// clip.duration
}

Rearrange a Clip#

Reposition second clip to appear as first:

session.reposition(fromIndex: 1, to: 0)

Merging/Stitching Recorded Clips#

Once you finished rearranging and editing your clips, you might want to merge all clips together into a single video file:

session.mergeClips(usingPreset: AVAssetExportPresetHighestQuality, completionHandler: { (url: URL?, error: Error?) in
if let url = url {
// url is the final video url of all your merged clips
}
})

url is your final video file that you can then upload to our backend:

VKUploads.shared.upload(usingFileUrl: url, withTags: [], withMetadata: [:]) { (video, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let video = video {
// Your video is uploaded
}
}

Auto Merge#

Next to the manual merging process, we also offer automatic merging, which is triggered after a recording is finished (After VKRecorder.shared.record()VKRecorder.shared.pause()).

Auto merging is disabled by default, but you can activate it through VKRecorderVideoConfiguration.autoMergeClipsAfterRecording.

vkRecorder.videoConfiguration.autoMergeClipsAfterRecording = true
vkRecorder.record()
vkRecorder.pause()
vkRecorder.record()
vkRecorder.pause()

To get notified once an auto merging process is finished, implement the following merge delegates:

extension YourRecorderViewController: VKRecorderDelegate {
public func vkRecorderDidFinishAutoMerge(_ url: URL, _ session: VKRecorderSession) {
// url is the final video url of all your merged clips
}
public func vkRecorderDidFinishAutoMergeWithError(_ error: Error, _ session: VKRecorderSession) {
}
}

Single video mode#

This mode automatically stitches together all recordings made using VKRecorder.shared.record() and VKRecorder.shared.pause().

vkRecorder.videoConfiguration.recordMultipleClips = false

Record and Pause#

If you now start and pause three video recordings, your session will only hold one video clip with all three videos automatically stiteched together. This reduces the time it needs to merge clips together but most importantly, is required for our Upload While Recording process.

VKRecorder.shared.record()
VKRecorder.shared.pause()
VKRecorder.shared.record()
VKRecorder.shared.pause()
VKRecorder.shared.record()
VKRecorder.shared.pause()

Your Session now holds one clip, but all three recordings are automatically added at the end.

if let session = VKRecorder.shared.session {
// session.clips.count == 1
}

Using our single video mode, there is no need to merge any clips together, for uploading the video all you need to do is grab the url of the first clip in your recording session.

if let session = VKRecorder.shared.session, url = session.clips.first {
VKUploads.shared.upload(usingFileUrl: url, withTags: [], withMetadata: [:]) { (video, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let video = video {
// Your video is uploaded
}
}
}
Last updated on by Nikita Gaer