Exports

  • The ImageExporter is responsible for exporting SessionImage objects to UIImage objects and JPEG files.

    For example, you can export to a UIImage with the following:

    if let image = session.image {
        ImageExporter.shared.export(image: image, completion: { (error, uiImage) in
            if let error = error {
                print("Unable to export image: \(error)")
                return
            }
    
            print("Finished image export with UIImage: \(uiImage!)")
        })
    }
    

    In addition to exporting as a UIImage, the exported image is saved to file as a JPEG at the exportedImageURL variable. After the export has completed, you may use, move or copy this file. Once you are finished with the file, you should call destroy() on the session in order to remove the image from the users drafts and delete its associated files.

    If you do not provide an API key or it is invalid, exported images will include a watermark.

    See more

    Declaration

    Swift

    @objcMembers
    public class ImageExporter
  • 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.

    See more

    Declaration

    Swift

    @objcMembers
    public class VideoExporter