Article

Exporting Video to Alternative Formats

Convert an existing movie file to a different format.

Overview

To convert existing movie files to a format that makes them compatible with other devices, you must generate a new movie file based on the contents of the existing file. You can't change the format of the saved video in place; you must create a second file of the desired format.

This article assumes that you've already saved the movie file in a different format. If your app is saving a movie directly from video capture, it's more efficient to change the default format during capture by following the steps in Capturing Video in Alternative Formats.

Export the New Video into the Desired Format

Convert the movie file by exporting the asset into the desired file type. Pick the type you want for the final video from the list of AVFileType presets that AVFoundation provides. You'll use that type to configure an AVAssetExportSession object, which then manages the export process from your existing type.

For example, to convert a movie file to H.264/MPEG-4 AVC, use an existing preset that encodes to H.264, such as AVAssetExportPresetHighestQuality, and set the output file type to mov.

import AVFoundation

let anAsset = // Your source AVAsset movie in HEVC format //
let outputURL = // URL of your exported output // 
 
// These settings will encode using H.264. 
let preset = AVAssetExportPresetHighestQuality 
let outFileType = AVFileTypeQuickTimeMovie

Next, make sure the movie asset can be converted. Certain assets may not be convertible under the preset conditions, so perform a check to ensure compatibility between the two formats.

AVAssetExportSession.determineCompatibility(ofExportPreset: preset, with: anAsset, outputFileType: outFileType, completionHandler: { (isCompatible) in
    if !isCompatible {
        return
}}) 

Once you've determined compatibility, tell AVAssetExportSession to perform the conversion with the configured preset. Creating a large movie file can be a time-consuming operation, so export the movie asynchronously with exportAsynchronously(completionHandler:). This method provides a completion block for you to handle the results after the session has finished creating the new movie file.

guard let export = AVAssetExportSession(asset: anAsset, presetName: preset) else { 
    return 
} 

export.outputFileType = outFileType 
export.outputURL = outputURL 
export.exportAsynchronously { () -> Void in
   // Handle export results. 
}

See Also

File Export

class AVAssetExportSession

An object that transcodes the contents of an asset source object to create an output of the form described by a specified export preset.

class AVAssetWriter

An object used to write media data to a new file of a specified audiovisual container type.

class AVAssetWriterInput

A writer used to append media samples to a single track of an asset writer's output file.

let AVVideoTransferFunction_ITU_R_2100_HLG: String

The transfer function for the ITU_R BT.2100 color space.

class AVOutputSettingsAssistant

An object that specifies a set of parameters for configuring objects that use output settings dictionaries.

class AVAssetWriterInputGroup

A group of tracks in a mutually exclusive relationship.

class AVAssetWriterInputMetadataAdaptor

An object that defines an interface for writing metadata packaged as timed metadata groups into a single asset writer input.

class AVAssetWriterInputPassDescription

An object that defines an interface for querying about the requirements of the current pass.

class AVAssetWriterInputPixelBufferAdaptor

A buffer used to append video samples packaged as pixel buffers to a single asset writer input.