mp3 文件的时间长度
不使用外部库确定给定 mp3 文件的长度(以秒为单位)的最简单方法是什么? (高度赞赏Python源代码)
What is the simplest way to determine the length (in seconds) of a given mp3 file, without using outside libraries? (python source highly appreciated)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 pymad。 它是一个外部库,但不要陷入“Not Invented Here”的陷阱。 您不需要任何外部库有什么特殊原因吗?
发现此处。
--
如果您确实不想使用外部库,请查看此处并查看他是如何做到的。 警告:这很复杂。
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.
为了 Google 关注者的缘故,这里还有一些外部库:
mpg321 -t
ffmpeg -i
midentify (基本上是 mplayer)请参阅 使用 mplayer确定音频/视频文件的长度
mencoder(向其传递无效参数,它会吐出一条错误消息,但也会为您提供相关文件的信息,例如 $ mencoder inputfile.mp3 -o fake)
mediainfo 程序 http://mediainfo.sourceforge.net/en
exiftool
Linux“文件”命令
mp3info
sox
参考:
https://superuser.com/questions/ 36871/linux命令行实用程序确定mp3比特率
http:// www.ruby-forum.com/topic/139468
mp3 长度(以毫秒为单位)
(使其成为供其他人添加的 wiki)。
和库:.net:naudio,java:jlayer,c:libmad
干杯!
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!
这听起来是一个相当艰巨的任务。 我不懂Python,但这里有一些我从我曾经尝试编写的另一个程序中重构的代码。
注意:它是用 C++ 编写的(抱歉,这就是我所拥有的)。 此外,按原样,它只能处理恒定比特率 MPEG 1 音频第 3 层文件。 这应该涵盖了大部分,但我不能保证它在所有情况下都能工作。 希望这能满足您的需求,并且希望将其重构为 Python 比从头开始更容易。
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.
只需使用
mutagen
在 python shell 中使用它:
simply use
mutagen
use it in python shell:
还要看看audioread(一些linux发行版包括ubuntu都有软件包),https://github.com/sampsyo/audioread
Also take a look at audioread (some linux distros including ubuntu have packages), https://github.com/sampsyo/audioread
您可以计算文件中的帧数。 每个帧都有一个起始码,尽管我无法记住起始码的确切值,而且我也没有 MPEG 规范。 每帧都有一定的长度,对于 MPEG1 第二层来说约为 40ms。
此方法适用于 CBR 文件(恒定比特率),VBR 文件的工作原理则完全不同。
从下面的文档中:
对于 Layer I 文件,我们使用以下公式:
FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
对于 Layer II 和 Layer II 文件, 我们使用以下公式: III 文件使用以下公式:
FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
有关 MPEG 音频帧标头的信息< /a>
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