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 AVFoundation API set. Send Media data to AVAssetWriter for writing in the form of CMSampleBuffer. Sequences of sample data appended to the asset writer inputs 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 while writing. 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. You must use a new instance of AVAssetWriter every time you write to a file.


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)

Creates 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 you can add inputs to.

Writing Data

func startWriting() -> Bool

Tells the writer to start writing its output.

func finishWriting() -> Bool

Completes the writing of the output file.

func finishWriting(completionHandler: () -> Void)

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

func cancelWriting()

Tells the writer to cancel writing.

var outputURL: URL

The URL you direct output to.

var outputFileType: AVFileType

The file format of the writer’s output.

var error: Error?

The description of the error that caused the failure.

var status: AVAssetWriter.Status

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

enum AVAssetWriter.Status

The statuses indicating whether a writer can successfully write samples to its output file.

var directoryForTemporaryFiles: URL?

A suitable directory 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 the receiver can add a given input.

Managing Session Time

func startSession(atSourceTime: CMTime)

Initiates a sample-writing session for the output asset.

func endSession(atSourceTime: CMTime)

Concludes a sample-writing session.

Configuring Output

func canApply(outputSettings: [String : Any]?, forMediaType: AVMediaType) -> Bool

Returns a Boolean value that indicates whether the specified output settings support a specified media type.

var metadata: [AVMetadataItem]

The collection of metadata written to the receiver's output file.

var movieFragmentInterval: CMTime

The frequency at which to write movie fragments

var overallDurationHint: CMTime

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

var movieTimeScale: CMTimeScale

The asset-level time scale to use.

var shouldOptimizeForNetworkUse: Bool

A Boolean value that indicates whether to write the output file to make 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 the receiver can add an input group.

var inputGroups: [AVAssetWriterInputGroup]

An array containing the asset writer input groups added to the asset writer.


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