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


class AVAssetWriterInput : NSObject


An AVAssetWriterInput object can append media samples packaged as CMSampleBuffer objects, or collections of metadata, to a single track of the output file of an AVAssetWriter object.

When there are multiple inputs, AVAssetWriter tries to write media data in an ideal interleaving pattern for efficiency in storage and playback. Each of its inputs signals its readiness to receive media data for writing according to that pattern via the value of isReadyForMoreMediaData. If isReadyForMoreMediaData is true, an input can accept additional media data while maintaining appropriate interleaving. You can only append media data to an input while its isReadyForMoreMediaData property is true.

  • If you’re writing media data from a non-real-time source, such as an instance of AVAssetReader, you should hold off on generating or obtaining more media data to append to an input when the value of isReadyForMoreMediaData is false. To help with control of the supply of non-real-time media data, you can use requestMediaDataWhenReady(on:using:) to specify a block that the input should invoke whenever it’s ready to append input.

  • If you’re writing media data from a real-time source such as an AVCaptureOutput object, you should set the input’s expectsMediaDataInRealTime property to true to ensure to calculate the value of isReadyForMoreMediaData appropriately. When expectsMediaDataInRealTime is true, isReadyForMoreMediaData will become false only when the input cannot process media samples as quickly as they are provided by the client. If isReadyForMoreMediaData becomes false for a real-time source, the client may need to drop samples or consider reducing the data rate of appended samples.

The value of isReadyForMoreMediaData will often change from false to true asynchronously while processing and writing previously-supplied media data to the output. It is possible for all of an asset writer’s inputs temporarily to return false for isReadyForMoreMediaData.


Creating an Asset Writer Input Instance

init(mediaType: AVMediaType, outputSettings: [String : Any]?)

Initialize a writer input object with the specified media type and output settings.

init(mediaType: AVMediaType, outputSettings: [String : Any]?, sourceFormatHint: CMFormatDescription?)

Initialize a writer input object with the specified media type, output settings, and source hint.

Video Settings Dictionaries

Define output image and video formats by using the key and value constants.

Adding Media Samples

func append(CMSampleBuffer) -> Bool

Appends samples to the receiver.

var isReadyForMoreMediaData: Bool

A Boolean value that indicates the readiness of the input to accept more media data.

func markAsFinished()

Tells the writer it can't append more buffers to this input.

func requestMediaDataWhenReady(on: DispatchQueue, using: () -> Void)

Instructs the receiver to invoke a block repeatedly, at its convenience, to gather media data for writing to the output.

Configuring a Writer Input

var metadata: [AVMetadataItem]

The collection of track-level metadata to associate with the asset and for carriage in the output file.

var transform: CGAffineTransform

The transform specified in the output file as the preferred transformation of the visual media data for display purposes.

var mediaTimeScale: CMTimeScale

Specifies the media time scale to be used.

var expectsMediaDataInRealTime: Bool

A Boolean value that indicates whether the input should tailor its processing of media data for real-time sources.

var marksOutputTrackAsEnabled: Bool

Returns whether the receiver’s track is enabled.

var naturalSize: CGSize

Size specified in the output file as the natural dimensions of the visual media data for display.

var preferredVolume: Float

The preferred volume level for storing in the output file.

Managing Track Associations

func addTrackAssociation(withTrackOf: AVAssetWriterInput, type: String)

Associates the track corresponding to the specified input with the track corresponding with the receiver.

func canAddTrackAssociation(withTrackOf: AVAssetWriterInput, type: String) -> Bool

Validates the association between the tracks and their corresponding pair of inputs.

Inspecting a Writer Input

var mediaType: AVMediaType

The media type of the samples that are appendable to the input.

var outputSettings: [String : Any]?

The settings used for encoding the media appended to the output.

var sourceFormatHint: CMFormatDescription?

A hint about the format of the buffers to appended.

var sampleReferenceBaseURL: URL?

The base URL sample references are relative to.

Managing Language Settings

var extendedLanguageTag: String?

Language tag to associate with the track corresponding to the receiver

var languageCode: String?

Language to associate with the track corresponding to the receiver.

Performing Multiple-Pass Encoding

var canPerformMultiplePasses: Bool

A Boolean value that indicates whether the input might perform multiple passes over appended media data.

var currentPassDescription: AVAssetWriterInputPassDescription?

An object that describes the requirements, such as source time ranges to append or re-append, to the current pass.

func markCurrentPassAsFinished()

Instructs the receiver to analyze the appended media data and determine whether to improve the results by re-encoding certain segments.

var performsMultiPassEncodingIfSupported: Bool

A Boolean value that indicates whether the input should attempt to encode the source media data using multiple passes.

var preferredMediaChunkAlignment: Int

The boundary, in bytes, for aligning media chunks.

var preferredMediaChunkDuration: CMTime

The duration used for each chunk of sample data in the output file.

func respondToEachPassDescription(on: DispatchQueue, using: () -> Void)

Instructs the receiver to invoke a client-supplied block whenever a new pass has begun.

Specifying Media Data Layout

var mediaDataLocation: AVAssetWriterInput.MediaDataLocation

Specifies where to lay out the media data and whether to interleave the media data as the main media data.

struct AVAssetWriterInput.MediaDataLocation

A value indicating how to lay out different media data and whether to interleave that media data.


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 AVAssetWriter

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

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.