iOS:如何从AVAsset或AVAssetTrack获取音频采样率

kcwpcxri  于 2023-05-30  发布在  iOS
关注(0)|答案(4)|浏览(438)

加载AVAsset后,如下所示:

AVAsset *asset = [AVAsset assetWithURL:url];

我想知道音频轨道的采样率是多少。目前,我得到的音频轨道是这样的:

AVAssetTrack *audioTrack = [[asset tracksWithMediaCharacteristic:AVMediaCharacteristicAudible] objectAtIndex:0];

这很管用。但我似乎找不到任何类型的财产,甚至在使用谷歌后也找不到;- ),这就给出了采样率。这通常是如何工作的?这可能吗(我开始越来越怀疑,因为谷歌并没有给我很多信息...)

7lrncoxx

7lrncoxx1#

let asset = AVAsset(url: URL(fileURLWithPath: "asset/p2a2.aif"))
let track = asset.tracks[0]
let desc = track.formatDescriptions[0] as! CMAudioFormatDescription
let basic = CMAudioFormatDescriptionGetStreamBasicDescription(desc)
print(basic?.pointee.mSampleRate)

我使用的是Swift,所以它看起来有点不同,但它仍然可以与Obj-C一起工作。

print(track.naturalTimeScale)

似乎也给予了正确的答案,但我有点担心,因为这个名字。

wwtsj6pe

wwtsj6pe2#

使用Swift和AVFoundation:

let url = Bundle.main.url(forResource: "audio", withExtension: "m4a")!
let asset = AVAsset(url: url)
if let firstTrack = asset.tracks.first {
    print("bitrate: \(firstTrack.estimatedDataRate)")
}

要在元数据中查找更多信息,您还可以参考:https://developer.apple.com/documentation/avfoundation/avassettrackhttps://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/finding_metadata_values

fhg3lkii

fhg3lkii3#

找到了。我正在使用MTAudioProcessingTap,所以在prepare()函数中我可以用途:

void prepare(MTAudioProcessingTapRef tap, CMItemCount maxFrames, const AudioStreamBasicDescription *processingFormat)
{
    sampleRate = processingFormat->mSampleRate;
    NSLog(@"Preparing the Audio Tap Processor");
}
mzillmmw

mzillmmw4#

private func getFormat(localUrl:String)->[String: Any?]{
    let audioUrl = URL.init(string: localUrl)
    let file=try! AVAudioFile(forReading: audioUrl!)
    let format=file.fileFormat
    return [
        "sampleRate":format.sampleRate,
        "channelCount":format.channelCount
    ]
}

相关问题