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


class AVAssetWriter : NSObject


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. AVAssetWriter automatically supports interleaving of media data for multiple concurrent tracks.

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: AVFileType)

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: AVFileType)

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

var availableMediaTypes: [AVMediaType]

The media types for which inputs can be added.

Writing Data

func startWriting() -> Bool

Tells the writer to start writing its output.

func finishWriting(completionHandler: () -> 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: AVFileType

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: AVAssetWriter.Status

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) -> Bool

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: AVMediaType) -> Bool

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

The frequency at which movie fragments should be written.

var overallDurationHint: CMTime

A hint of the final duration of the file to be written for file types that support movie fragments.

var movieTimeScale: CMTimeScale

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) -> Bool

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.


enum AVAssetWriter.Status

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


Inherits From

Conforms To

See Also

File Export

Exporting Video to Alternative Formats

Convert an existing movie file to a different format.

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 AVAssetWriterInput

A writer used to append media samples to a single track of the output file of an AVAssetWriter object.

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; for example, the time ranges of media data to append.

class AVAssetWriterInputPixelBufferAdaptor

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