【发布时间】:2020-01-26 12:03:18
【问题描述】:
我有一个使用 AVAssetWriter 捕获音频和视频的应用程序。它对音频运行快速傅立叶变换 (FFT),以实时创建捕获音频的可视频谱。
直到 iPhone11 发布,这一切都很好。但是,使用 iPhone 11 的用户报告说根本没有捕获音频。我设法缩小了问题的范围 - captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) 返回的样本数量是 940 或 941 - 在以前的手机型号上,这始终是 1024 个样本。我使用CMSampleBufferGetNumSamples 来获取样本数。我的 FFT 计算依赖于样本数是 2 的幂,因此它会丢弃新型号 iPhone 上的所有帧。
谁能解释为什么新款 iPhone11 会返回异常数量的样品?这是我配置AVAssetWriter的方法:
self.videoWriter = try AVAssetWriter(outputURL: self.outputURL, fileType: AVFileType.mp4)
var videoSettings: [String : Any]
if #available(iOS 11.0, *) {
videoSettings = [
AVVideoCodecKey : AVVideoCodecType.h264,
AVVideoWidthKey : Constants.VIDEO_WIDTH,
AVVideoHeightKey : Constants.VIDEO_HEIGHT,
]
} else {
videoSettings = [
AVVideoCodecKey : AVVideoCodecH264,
AVVideoWidthKey : Constants.VIDEO_WIDTH,
AVVideoHeightKey : Constants.VIDEO_HEIGHT,
]
}
//Video Input
videoWriterVideoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoSettings)
videoWriterVideoInput?.expectsMediaDataInRealTime = true;
if (videoWriter?.canAdd(videoWriterVideoInput!))!
{
videoWriter?.add(videoWriterVideoInput!)
}
//Audio Settings
let audioSettings : [String : Any] = [
AVFormatIDKey : kAudioFormatMPEG4AAC,
AVSampleRateKey : Constants.AUDIO_SAMPLE_RATE, //Float(44100.0)
AVEncoderBitRateKey : Constants.AUDIO_BIT_RATE, //64000
AVNumberOfChannelsKey: Constants.AUDIO_NUMBER_CHANNELS //1
]
//Audio Input
videoWriterAudioInput = AVAssetWriterInput(mediaType: AVMediaType.audio, outputSettings: audioSettings)
videoWriterAudioInput?.expectsMediaDataInRealTime = true;
if (videoWriter?.canAdd(videoWriterAudioInput!))!
{
videoWriter?.add(videoWriterAudioInput!)
}
【问题讨论】:
-
你解决了吗?我也面临同样的问题。
-
@Jelly Sort of.. 将很快发布答案。
标签: iphone ios11 avassetwriter cmsamplebuffer