如何创建用于 AVAssetWriterInput 实例的 outputSettings Dictionary<String. Any>?

How to create an outputSettings Dictionary<String. Any> for use with an AVAssetWriterInput instance?

我正在尝试将 MPMediaItem 实例转换为 caf 格式的音频文件。我一直在关注 Chris Adamson 和他的 post 在 From iPod Library to PCM Samples in Far Fewer Steps Than Were Previously Necessary

上的工作

当我在 swift 中四处寻找如何做到这一点时,我遇到了 Abel Domingues github FileConverter.swift 在 Swift.

中做到了这一点

然后我着手转换为 Swift 3 作为协议的扩展。 一切顺利,直到我尝试 运行 吧。它在创建 assetWriterInput 对象时崩溃,似乎与 outputSettings 变量有关。

        var outputSettings = [
            AVFormatIDKey: kAudioFormatLinearPCM,
            AVSampleRateKey: 44100,
            AVNumberOfChannelsKey: 2,
            AVChannelLayoutKey: NSData(bytes:&channelLayout, length:MemoryLayout<AudioChannelLayout>.size),
            AVLinearPCMBitDepthKey: 16,
            AVLinearPCMIsNonInterleaved: false,
            AVLinearPCMIsFloatKey: false,
            AVLinearPCMIsBigEndianKey: false
        ] as [String : Any]

        // create an asset writer input
        let assetWriterInput = AVAssetWriterInput(mediaType:AVMediaTypeAudio, outputSettings:outputSettings as NSDictionary as! [String : Any])

我收到的错误信息如下:

-[_SwiftValue unsignedIntValue]: unrecognized selector sent to instance 0x1704407b0 2016-10-13 18:34:52.032784 Testie[3098:1535938] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntValue]: unrecognized selector sent to instance 0x1704407b0'

我已经搜索过这方面的示例,但 post 必须在 Objective-C and/or 中与设置视频词典相关。

这是来自 AVAssetWriterInput 源的与音频相关的文档:

For AVMediaTypeAudio the following keys are not currently supported in the outputSettings dictionary: AVEncoderAudioQualityKey and AVSampleRateConverterAudioQualityKey. When using this initializer, an audio settings dictionary must be fully specified, meaning that it must contain AVFormatIDKey, AVSampleRateKey, and AVNumberOfChannelsKey. If no other channel layout information is available, a value of 1 for AVNumberOfChannelsKey will result in mono output and a value of 2 will result in stereo output. If AVNumberOfChannelsKey specifies a channel count greater than 2, the dictionary must also specify a value for AVChannelLayoutKey. For kAudioFormatLinearPCM, all relevant AVLinearPCM*Key keys must be included, and for kAudioFormatAppleLossless, AVEncoderBitDepthHintKey keys must be included. See -initWithMediaType:outputSettings:sourceFormatHint: for a way to avoid having to specify a value for each of those keys.

那么字典中是什么导致了错误?

在Swift 3中,kAudioFormatLinearPCM被导入为UInt32(又名AudioFormatID),Swift 3.0.0无法将其转换为合适的输入 [String: Any].

时输入(在本例中为 NSNumber

试试这个:

    var outputSettings = [
        AVFormatIDKey: UInt(kAudioFormatLinearPCM),
        AVSampleRateKey: 44100,
        AVNumberOfChannelsKey: 2,
        AVChannelLayoutKey: NSData(bytes:&channelLayout, length:MemoryLayout<AudioChannelLayout>.size),
        AVLinearPCMBitDepthKey: 16,
        AVLinearPCMIsNonInterleaved: false,
        AVLinearPCMIsFloatKey: false,
        AVLinearPCMIsBigEndianKey: false
    ] as [String : Any]

或者等到 Xcode 8.1/Swift 3.0.1,这应该可以解决你的问题。