您的位置:首页 > 其它

AAC音频文件时长计算

2016-10-27 11:44 417 查看
1.如何计算音频文件的时长?

大致的思路

如果文件是CBR,则总时长为t=file_size/bitrate

bitrate则是文件的固定码率。对于固定码率的音频文件必定有文件头可以读取到这个消息。file_size为整个音频文件的长度。

如果文件是VBR,则总时长为t=file_size/average_bitrate

average_bitrate为文件的平均码率。

1.2如何计算average_bitrate平均码率?

一般的思路是遍历整个文件,数出有多少帧,然后将每一帧的的bitrate加起来得到总的bitrate,然后用总bitrate除去总帧数。

1.3VBR头

一般VBR音频文件都有一个VBR头部来记录音频文件的总帧数及其采样率及其每一帧的采样数。我们可以根据这些信息来计算总的时长

t=文件总帧数x(每一帧采样数)/(采样率)

2 AAC音频文件时长计算

2.1 AAC音频格式

AAC音频格式有ADIF和ADTS两种格式。

ADIF:Audio Data Interchange Format 音频数据交换格式。ADIF只有一个统一的头,所以必须得到所有的数据后解码,不能在音频数据流中间开始的解码。故这种格式常用在磁盘文件中。

ADTS:Audio Data Transport Stream 音频数据传输流。每一帧都有头信息,可以在任意帧解码。ADTS的数据流就是ADTS帧序列,可以数据流的任意位置开始解码。ADTS特征类似于mp3数据流格式。ADTS格式便于网络传输和处理,比较适合应用于语音实时对讲系统这种对实时性要求较高的场合。

2.2 ADTS 头

ADTS帧结构:

header
body
ADTS帧首部结构:
序号长度(bits)说明
1Syncword12all bits must be 1
2MPEG version10 for MPEG-4, 1 for MPEG-2
3Layer2always 0
4Protection Absent1et to 1 if there is no CRC and 0 if there is CRC
5Profile2the MPEG-4 Audio Object Type minus 1
6MPEG-4 Sampling Frequency Index4MPEG-4 Sampling Frequency Index (15 is forbidden)
7Private Stream1set to 0 when encoding, ignore when decoding
8MPEG-4 Channel Configuration3MPEG-4 Channel Configuration (in the case of 0, the channel configuration is sent via an inband PCE)
9Originality1set to 0 when encoding, ignore when decoding
10Home1set to 0 when encoding, ignore when decoding
11Copyrighted Stream1set to 0 when encoding, ignore when decoding
12Copyrighted Start1set to 0 when encoding, ignore when decoding
13Frame Length13this value must include 7 or 9 bytes of header length: FrameLength = (ProtectionAbsent == 1 ? 7 : 9) + size(AACFrame)
14Buffer Fullness11buffer fullness
15Number of AAC Frames2number of AAC frames (RDBs) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame per ADTS frame
16CRC16CRC if protection absent is 0
 


2.ADTS内容及结构

ADTS 头中相对有用的信息 采样率、声道数、帧长度。想想也是,我要是解码器的话,你给我一堆得AAC音频ES流我也解不出来。每一个带ADTS头信息的AAC流会清晰的告送解码器他需要的这些信息。

一般情况下ADTS的头信息都是7个字节,分为2部分:

adts_fixed_header();

adts_variable_header();








syncword :同步头 总是0xFFF, all bits must be 1,代表着一个ADTS帧的开始

ID:MPEG Version: 0 for MPEG-4, 1 for MPEG-2

Layer:always: '00'

profile:表示使用哪个级别的AAC,有些芯片只支持AAC LC 。在MPEG-2 AAC中定义了3种:



sampling_frequency_index:表示使用的采样率下标,通过这个下标在 Sampling Frequencies[ ]数组中查找得知采样率的值。

There are 13 supported frequencies:
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
13: Reserved
14: Reserved
15: frequency is written explictly
channel_configuration: 表示声道数 

0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
3: 3 channels: front-center, front-left, front-right
4: 4 channels: front-center, front-left, front-right, back-center
5: 5 channels: front-center, front-left, front-right, back-left, back-right
6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel
7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel
8-15: Reserved



frame_length : 一个ADTS帧的长度包括ADTS头和AAC原始流.

ADTS 头中相对有用的信息 采样率、声道数、帧长度。

2.3

采用1.3的方法,

一个AAC原始帧包含一段时间内1024个采样及相关数据。

一个AAC音频帧的播放时间=一个AAC帧对应的采样样本的个数/采样率。
总时间t=总帧数x一个AAC音频帧的播放时间

参考:
http://www.cnblogs.com/caosiyang/archive/2012/07/16/2594029.html http://blog.csdn.net/bsplover/article/details/7426476
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: