Class

AVAssetWriterInput

You use an AVAssetWriterInput to append media samples packaged as CMSampleBuffer objects (see CMSampleBuffer), or collections of metadata, to a single track of the output file of an AVAssetWriter object.

Overview

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 for input to be appended.

  • 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 that the value of isReadyForMoreMediaData is calculated appropriately. When expectsMediaDataInRealTime is true, isReadyForMoreMediaData will become false only when the input cannot process media samples as quickly as they are being 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, as previously-supplied media data is processed and written to the output. It is possible for all of an asset writer’s inputs temporarily to return false for isReadyForMoreMediaData.

Topics

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

Key and value constants for defining output image and video formats.

Adding Samples

func append(CMSampleBuffer)

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 that no more buffers will be appended to this input.

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

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

Configuring a Writer Input

var metadata: [AVMetadataItem]

The collection of track-level metadata for association 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

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

Preferred volume level to be stored 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)

Whether an association between the tracks corresponding to a pair of inputs is valid.

Inspecting a Writer Input

var mediaType: AVMediaType

The media type of the samples that can be appended 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 buffers that will be appended.

var sampleReferenceBaseURL: URL?

For file types that support writing sample references, such as QuickTime Movie files, specifies 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?

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

func markCurrentPassAsFinished()

Instructs the receiver to analyze the media data that has been appended and determine whether the results could be improved 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

For file types that support media chunk alignment, such as QuickTime Movie files, specifies the boundary for media chunk alignment in bytes.

var preferredMediaChunkDuration: CMTime

For file types that support media chunk duration, such as QuickTime Movie files, specifies the duration to be 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.

Relationships

Inherits From

Conforms To