问题描述
如何在Delphi中获取MP3/wav音频文件的持续时间(以分钟和秒为单位)?
How do you get the duration (in minutes and seconds) of an MP3/wav audio file in Delphi ?
推荐答案
您可以通过将文件的大小除以比特率来计算持续时间.您可以从其中一个帧头获得比特率.当然,这不适用于可变速率的MP3,在这种情况下,您可以为每个帧设置不同的速率.
You can calculate the duration by dividing the size of the file by the bit rate. You can get the bit rate from one of the frame headers. Of course, this won't work for variable rate MP3s, where you can have a different rate for each frame.
使用标题布局(只有四个字节):
Using the Header Layout (it's just four bytes):
-
在流中打开MP3
Open the MP3 in a stream
通过阅读找到第一帧标题的开头,直到查找同步标头,该标头具有设置为1的11个连续位.以前是12,但经过调整后允许使用MPEG 2.5版.
Find the beginning of the first frame header by reading until youfind the sync header, which has 11 consecutive bits set to 1. Thisused to be 12, but it was tweaked to allow for MPEG version 2.5.
确定MPEG版本ID.为了找到比特率,V2.5与V2相同
Determine the MPEG version ID. For the purposes of finding the bit rate, V2.5 is the same as V2
确定图层描述
读取比特率索引
使用MPEG版本,层描述和比特率索引,从链接报头参考中的比特率索引表确定实际比特率
Using the MPEG version, layer description and bit rate index, determine the actual bit rate from the bit rate index table in the linked header reference
将文件大小除以千比特((8 *大小以字节为单位)/1000)除以比特率,得到持续时间(以秒为单位)
Divide the file size in kilobits ((8 * size in bytes) / 1000) by the bit rate to get the duration in seconds
我找不到Delphi示例,但这是一个 C#实现.请参见getLengthInSeconds方法.
I couldn't find a Delphi sample, but here is a C# implementation that uses this technique for reference purposes. See the getLengthInSeconds method.
这篇关于MP3/wav音频文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!