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 AVFileTypeQuickTimeMovie.

#import <AVFoundation/AVFoundation.h>

AVAsset* hevcAsset = // Your source AVAsset movie in HEVC format //
NSURL* outputURL = // URL of your exported output // 
    
// These settings will encode using H.264.
NSString* preset = AVAssetExportPresetHighestQuality;
AVFileType 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 determineCompatibilityOfExportPreset:preset withAsset:hevcAsset outputFileType:outFileType completionHandler:^(BOOL compatible) {
    if (!compatible) {
        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 exportAsynchronouslyWithCompletionHandler:. This method provides a completion block for you to handle the results after the session has finished creating the new movie file.

AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:hevcAsset presetName:preset];

if (exportSession) {
    exportSession.outputFileType = outFileType;
    exportSession.outputURL = outputURL;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        // Handle export results.
    }];
}

See Also

File Export

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.

AVAssetWriter

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

AVAssetWriterInput

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

AVVideoTransferFunction_ITU_R_2100_HLG

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

AVOutputSettingsAssistant

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

AVAssetWriterInputGroup

A group of tracks in a mutually exclusive relationship.

AVAssetWriterInputMetadataAdaptor

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

AVAssetWriterInputPassDescription

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

AVAssetWriterInputPixelBufferAdaptor

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