What is the simplest way to determine the length (in seconds) of a given mp3 file, without using outside libraries? (python source highly appreciated)
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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
Also take a look at audioread (some linux distros including ubuntu have packages), https://github.com/sampsyo/audioread
For google followers' sake, here are a few more external libs:
mpg321 -t
ffmpeg -i
midentify (mplayer basically) see Using mplayer to determine length of audio/video file
mencoder (pass it invalid params, it will spit out an error message but also give you info on the file in question, ex $ mencoder inputfile.mp3 -o fake)
mediainfo program http://mediainfo.sourceforge.net/en
exiftool
the linux "file" command
mp3info
sox
refs: https://superuser.com/questions/36871/linux-command-line-utility-to-determine-mp3-bitrate
http://www.ruby-forum.com/topic/139468
mp3 length in milliseconds
(making this a wiki for others to add to).
and libs: .net: naudio, java: jlayer, c: libmad
Cheers!
You can use pymad. It's an external library, but don't fall for the Not Invented Here trap. Any particular reason you don't want any external libraries?
Spotted here.
--
If you really don't want to use an external library, have a look here and check out how he's done it. Warning: it's complicated.
simply use
mutagen
use it in python shell:
That sounds like a pretty tall order. I don't know Python, but here's some code I've refactored from another program I once tried to write.
Note: It's in C++ (sorry, it's what I've got). Also, as-is, it'll only handle constant bit rate MPEG 1 Audio Layer 3 files. That should cover most, but I can't make any guarantee as to this working in all situations. Hopefully this does what you want, and hopefully refactoring it into Python is easier than doing it from scratch.