Initializer

init(mediaType:outputSettings:)

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

Declaration

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

Parameters

mediaType

The media type of the samples to be accepted by the input object. For a list of media types, see AVFoundation Constants.

outputSettings

Specify a dictionary containing the settings used for encoding the media appended to the output. You may pass nil for this parameter if you do not want the appended samples to be re-encoded.

Audio output settings keys are defined in Audio Settings and Formats. Video output settings keys are defined in AVFoundation Constants. Video output settings with keys from CVPixelBuffer are not currently supported.

Return Value

An initialized writer input object that can accept samples of the specified media type and write them to the output file.

Discussion

Each new input accepts data for a new track of the asset writer’s output file. You add an input to an asset writer using the AVAssetWriter method add(_:).

Passing nil for output settings instructs the input to pass through appended samples, doing no processing before they are written to the output file.  This is useful if, for example, you are appending buffers that are already in a desirable compressed format.  However, if not writing to a QuickTime Movie file (i.e. the AVAssetWriter was initialized with a file type other than mov), AVAssetWriter only supports passing through a restricted set of media types and subtypes.  In order to pass through media data to files other than mov, a non-NULL format hint must be provided using init(mediaType:outputSettings:sourceFormatHint:) instead of this method.

When the mediaType parameter is audio, the outputSettings dictionary does not support the AVEncoderAudioQualityKey and AVSampleRateConverterAudioQualityKey keys. When using this method, an audio settings dictionary must be fully specified, meaning that it must contain the AVFormatIDKey, AVSampleRateKey, and AVNumberOfChannelsKey keys. If no other channel layout information is available, a value of 1 for the AVNumberOfChannelsKey key results in mono output and a value of 2 results in stereo output. If the AVNumberOfChannelsKey key specifies a value greater than 2, the dictionary must also specify a value for the AVChannelLayoutKey key. For audio using the kAudioFormatLinearPCM format, include all relevant AVLinearPCM*Key keys. For the kAudioFormatAppleLossless format, include the AVEncoderBitDepthHintKey keys. To avoid specifying values for each of those keys, use the init(mediaType:outputSettings:sourceFormatHint:) method instead.

When the mediaType parameter is video, the outputSettings dictionary must request a compressed video format. This means that the values specified in the dictionary must follow the rules for compressed video output, as described in AVVideoSettings.h. When using this initializer, a video settings dictionary must be fully specified, meaning that it must contain the following keys: AVVideoCodecKey, AVVideoWidthKey, and AVVideoHeightKey. To avoid specifying values for each of those keys, use the init(mediaType:outputSettings:sourceFormatHint:) method.

See Also

Creating an Asset Writer Input Instance

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.