VideoExporter
@objcMembers
public class VideoExporter
The VideoExporter is responsible for exporting SessionVideo and SessionVideoSegment objects to video
files.
For example, you can export a video to an mp4 file with the following:
if let video = session.video {
VideoExporter.shared.export(video: video, progress: { progress in
print("Export progress: \(progress)")
}, completion: { error in
if let error = error {
print("Unable to export video: \(error)")
return
}
print("Finished video export at URL: \(video.exportedVideoURL)")
})
}
After the export has completed, you should use, move or copy the file found at the exportedVideoURL. Once you are finished
with the file, you should call destroy() on the session in order to remove the video from the users drafts and delete its associated files.
Frame rate can be customized by setting the frameDuration variable before exporting the video.
You can also change renderSize but we recommend you instead set the PreviewCropController aspectRatio and CameraController aspectRatio. See the square content example. These properties allow you to preserve video quality by delaying any upscaling or downscaling until a later point in your
video processing logic. If you plan on converting your video to HLS on a server that encoder should handle any upscaling or downscaling.
Default Encoding Settings
If you do not provide a fileType, videoEncodingSettings, or audioEncodingSettings the default settings will be an mp4 file with H.264 video encoding and stereo AAC audio encoding:
import AVFoundation
var acl = AudioChannelLayout()
memset(&acl, 0, MemoryLayout<AudioChannelLayout>.size)
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo
let audioEncodingSettings: [String: Any] = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: AVAudioSession.sharedInstance().sampleRate,
AVChannelLayoutKey: NSData(bytes:&acl, length:MemoryLayout<AudioChannelLayout>.size),
AVEncoderBitRateKey: 96000
]
let videoEncodingSettings: [String: Any] = [
AVVideoCompressionPropertiesKey: [
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC],
AVVideoCodecKey: AVVideoCodecType.h264
]
VideoExporter.shared.export(video: session.video!,
fileType: .mp4,
videoEncodingSettings: videoEncodingSettings,
audioEncodingSettings: audioEncodingSettings,
progress: nil,
completion: { error in
})
There are many combinations of encoding settings you can provide. They must conform to the specifications set forth in AVFoundations AVVideoSettings.h and AVAudioSettings.h headers. There is also a list of available codecs. Keep in mind each codec may have different requirements for the settings you provide.
HEVC Encoding Settings
Below is an example of HEVC encoding settings:
import AVFoundation
import VideoToolbox
let videoEncodingSettings: [String: Any] = [
AVVideoCompressionPropertiesKey: [
AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main_AutoLevel],
AVVideoCodecKey: AVVideoCodecType.hevc
]
If you do not provide an API key or it is invalid, exported videos will include a watermark.
-
Use this to access the shared instance of the
VideoExporter. For example to access the export status:print("Is exporting: \(VideoExporter.shared.isExporting)")Declaration
Swift
public static let shared: VideoExporter
-
Indicates if the
VideoExporteris currently exporting anything.Default value:
falseDeclaration
Swift
public var isExporting: Bool { get }
-
Exports a
SessionVideoobject with the provided video and audio settings. If you do not provide any settings the defaults will be used. After the completion closure is called the exported video file will be available at theexportedVideoURLvariable on the video. You can move, copy or delete this file at your own discretion.Attention
When this is called, any other existing video export will be cancelled.
Declaration
Swift
public func export(video: SessionVideo, fileType: AVFileType = .mp4, videoEncodingSettings: [String: Any]? = nil, audioEncodingSettings: [String: Any]? = nil, progress: ((Double) -> Void)? = nil, completion: @escaping (VideoExporterError?) -> Void)Parameters
videoThe video to export.
fileTypeThe export file type.
videoEncodingSettingsThe video encoding settings.
audioEncodingSettingsThe audio encoding settings.
progressThis closure will be called each time the export progress is updated. The parameter will contain the progress of the export in a 0.0 to 1.0 range.
completionThis closure will be called when the export succeeds or fails. If the export failed there will be an error present.
-
Exports multiple
SessionVideoobjects with the provided video and audio settings. If you do not provide any settings the defaults will be used. After the completion closure is called the exported video files will be available at theexportedVideoURLvariable for each respective video. You can move, copy or delete these files at your own discretion.Attention
When this is called, any other existing video export will be cancelled.
Declaration
Swift
public func export(videos: [SessionVideo], fileType: AVFileType = .mp4, videoEncodingSettings: [String: Any]? = nil, audioEncodingSettings: [String: Any]? = nil, progress: ((Double) -> Void)? = nil, completion: @escaping (VideoExporterError?) -> Void)Parameters
videosThe videos to export. You must provide at least one video.
fileTypeThe export file type.
videoEncodingSettingsThe video encoding settings.
audioEncodingSettingsThe audio encoding settings.
progressThis closure will be called each time the export progress is updated. The parameter will contain the progress of the export in a 0.0 to 1.0 range.
completionThis closure will be called when the export succeeds or fails. If the export failed there will be an error present.
-
Exports a
SessionVideoSegmentobject with the provided video and audio settings. If you do not provide any settings the defaults will be used. After the completion closure is called the exported video segment file will be available at theexportedVideoURLvariable on the video segment. You can move, copy or delete this file at your own discretion.Attention
When this is called, any other existing video export will be cancelled.
Declaration
Swift
public func export(segment: SessionVideoSegment, fileType: AVFileType = .mp4, videoEncodingSettings: [String: Any]? = nil, audioEncodingSettings: [String: Any]? = nil, progress: ((Double) -> Void)? = nil, completion: @escaping (VideoExporterError?) -> Void)Parameters
segmentThe video segment to export.
fileTypeThe export file type.
videoEncodingSettingsThe video encoding settings.
audioEncodingSettingsThe audio encoding settings.
progressThis closure will be called each time the export progress is updated. The parameter will contain the progress of the export in a 0.0 to 1.0 range.
completionThis closure will be called when the export succeeds or fails. If the export failed there will be an error present.
-
Exports multiple
SessionVideoSegmentobjects with the provided video and audio settings. If you do not provide any settings the defaults will be used. After the completion closure is called the exported video segment files will be available at theexportedVideoURLvariable for each respective video segment. You can move, copy or delete these files at your own discretion.Attention
When this is called, any other existing video export will be cancelled.
Declaration
Swift
public func export(segments: [SessionVideoSegment], fileType: AVFileType = .mp4, videoEncodingSettings: [String: Any]? = nil, audioEncodingSettings: [String: Any]? = nil, progress: ((Double) -> Void)? = nil, completion: @escaping (VideoExporterError?) -> Void)Parameters
segmentsThe video segments to export. You must provide at least one video segment.
fileTypeThe export file type.
videoEncodingSettingsThe video encoding settings.
audioEncodingSettingsThe audio encoding settings.
progressThis closure will be called each time the export progress is updated. The parameter will contain the progress of the export in a 0.0 to 1.0 range.
completionThis closure will be called when the export succeeds or fails. If the export failed there will be an error present.
-
Cancels any existing video or video segment export.
Declaration
Swift
public func cancelExport(completion: (() -> Void)? = nil)Parameters
completionThis closure will be called after the export has been cancelled. If there is no export, the closure will be called immediately.
View on GitHub
VideoExporter Class Reference