You use an AVAssetWriter object to write media data to a new file of a specified audiovisual container type, such as a QuickTime movie file or an MPEG-4 file, with support for automatic interleaving of media data for multiple concurrent tracks.


You can get the media data for one or more assets from instances of AVAssetReader or even from outside the AV Foundation API set. Media data is presented to AVAssetWriter for writing in the form of CMSampleBuffers (see CMSampleBuffer). Sequences of sample data appended to the asset writer inputs are considered to fall within “sample-writing sessions.” You must call startSession(atSourceTime:) to begin one of these sessions.

Using AVAssetWriter, you can optionally re-encode media samples as they are written. You can also optionally write metadata collections to the output file.

You can only use a given instance of AVAssetWriter once to write to a single file. If you want to write to files multiple times, you must use a new instance of AVAssetWriter each time.


Creating an Asset Writer

init(url: URL, fileType: String)

Returns an asset writer for writing to the file identified by a given URL in a format specified by a given UTI.

init(outputURL: URL, fileType: String)

Initializes an asset writer for writing to the file identified by a given URL in a format specified by a given UTI.

var availableMediaTypes: [String]

The media types for which inputs can be added

Writing Data

func startWriting()

Tells the writer to start writing its output.

func finishWriting(completionHandler: @escaping () -> Void)

Marks all unfinished inputs as finished and completes the writing of the output file.

func cancelWriting()

Instructs the writer to cancel writing.

var outputURL: URL

The URL to which output is directed.

var outputFileType: String

The file format of the writer’s output.

var error: Error?

If the receiver's status is AVAssetWriterStatusFailed, describes the error that caused the failure.

var status: AVAssetWriterStatus

The status of writing samples to the receiver's output file.

var directoryForTemporaryFiles: URL?

Specifies a directory that is suitable for containing temporary files generated during the export process.

Managing Inputs

var inputs: [AVAssetWriterInput]

The asset writer inputs associated with the asset writer.

func add(AVAssetWriterInput)

Adds an input to the receiver.

func canAdd(AVAssetWriterInput)

Returns a Boolean value that indicates whether a given input can be added to the receiver.

Managing Session Time

func startSession(atSourceTime: CMTime)

Initiates a sample-writing session for the output asset.

func endSession(atSourceTime: CMTime)

Concludes an explicit sample-writing session.

Configuring Output

func canApply(outputSettings: [String : Any]?, forMediaType: String)

Returns a Boolean value that indicates whether the specified output settings are supported for a specified media type.

var metadata: [AVMetadataItem]

The collection of metadata for association with the asset and for carriage in the output file.

var movieFragmentInterval: CMTime

Specifies the frequency at which movie fragments should be written.

var movieTimeScale: CMTimeScale

Specifies the asset-level time scale to be used.

var shouldOptimizeForNetworkUse: Bool

A Boolean value that indicates whether the output file should be written in way that makes it more suitable for playback over a network.

Managing Asset Writer Input Groups

func add(AVAssetWriterInputGroup)

Adds an asset writer input group instance to the asset writer.

func canAdd(AVAssetWriterInputGroup)

Returns whether an input group can be added to the receiver.

var inputGroups: [AVAssetWriterInputGroup]

An array of asset writer input groups that have been added to the asset writer.



These constants are returned by the status property to indicate whether it can successfully write samples to its output file.


Inherits From

Conforms To