Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - Duration of an MP3/wav audio file

How do you get the duration (in minutes and seconds) of an MP3/wav audio file in Delphi ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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):

  1. Open the MP3 in a stream

  2. Find the beginning of the first frame header by reading until you find the sync header, which has 11 consecutive bits set to 1. This used to be 12, but it was tweaked to allow for MPEG version 2.5.

  3. Determine the MPEG version ID. For the purposes of finding the bit rate, V2.5 is the same as V2

  4. Determine the layer description

  5. Read the bit rate index

  6. 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

  7. Divide the file size in kilobits ((8 * size in bytes) / 1000) by the bit rate to get the duration in seconds

I couldn't find a Delphi sample, but here is a C# implementation that uses this technique for reference purposes. See the getLengthInSeconds method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...