time length of an mp3 file

前端 未结 6 2123
别那么骄傲
别那么骄傲 2020-12-01 07:11

What is the simplest way to determine the length (in seconds) of a given mp3 file, without using outside libraries? (python source highly appreciated)

6条回答
  •  一整个雨季
    2020-12-01 07:36

    You might count the number of frames in the file. Each frame has a start code, although I can't recollect the exact value of the start code and I don't have MPEG specs laying around. Each frame has a certain length, around 40ms for MPEG1 layer II.

    This method works for CBR-files (Constant Bit Rate), how VBR-files work is a completely different story.

    From the document below:

    For Layer I files us this formula:

    FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4

    For Layer II & III files use this formula:

    FrameLengthInBytes = 144 * BitRate / SampleRate + Padding

    Information about MPEG Audio Frame Header

提交回复
热议问题