什麼是最簡單的方法來確定給定的MP3文件的長度(以秒爲單位),而不使用外部庫? (蟒蛇源高度讚賞)mp3文件的時間長度
回答
您可以使用pymad。這是一個外部圖書館,但不要落入這個不發明的陷阱。任何特定的原因你不想要任何外部庫?
import mad
mf = mad.MadFile("foo.mp3")
track_length_in_milliseconds = mf.total_time()
發現here。
-
如果你真的不想使用外部庫,看看here,並檢查了他是怎麼做到的。警告:這很複雜。
我同意推薦外部圖書館。我沒有用過它(或者Python)。但我曾經試着用C++寫一個可以簡單播放MP3的程序。這並沒有出現,但我確定了足夠的時間來確定文件的持續時間。我想過重構該代碼... – 2008-09-23 06:50:32
...在這裏發佈,但它是相當複雜的。 (而在C++中,不是Python)。甚至不是很簡單。 – 2008-09-23 06:52:21
只是擡頭,它看起來像它只能在某些平臺上工作。最新的版本在安裝時崩潰,因爲它缺少自己的安裝文件之一,它建議我通過運行第二個帶有linux命令的文件來生成它。 – 2015-03-15 00:35:53
您可以計算文件中的幀數。每個幀都有一個起始碼,儘管我無法回憶起始碼的確切值,也沒有MPEG規格。每個幀都有一定的長度,對於MPEG1 II層大約爲40ms。
這種方法適用於CBR文件(恆定比特率),VBR文件如何工作是一個完全不同的故事。
從下面的文件:
對於層I文件我們這個公式:
FrameLengthInBytes =(12 *比特率/採樣率+填充)* 4
用於層II & III文件使用這式:
FrameLengthInBytes = 144 *碼率/採樣率+填充
我相信這個長度是26ms。 – 2009-02-27 21:36:34
聽起來很熟悉。 – 2009-03-05 12:43:22
簡單,分析MP3二進制BLOB計算的東西,在Python
這聽起來像一個非常艱鉅的任務。我不知道Python,但是這裏有一些代碼是我從另一個我曾經寫過的程序中重構而來的。
注:這是在C++(對不起,這是我的)。而且,它現在只能處理恆定比特率的MPEG 1音頻層3文件。 應該是,但我無法保證在任何情況下都能正常工作。希望這可以做到你想要的,並且希望將它重構爲Python比從頭開始更容易。
// determines the duration, in seconds, of an MP3;
// assumes MPEG 1 (not 2 or 2.5) Audio Layer 3 (not 1 or 2)
// constant bit rate (not variable)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//Bitrates, assuming MPEG 1 Audio Layer 3
const int bitrates[16] = {
0, 32000, 40000, 48000, 56000, 64000, 80000, 96000,
112000, 128000, 160000, 192000, 224000, 256000, 320000, 0
};
//Intel processors are little-endian;
//search Google or see: http://en.wikipedia.org/wiki/Endian
int reverse(int i)
{
int toReturn = 0;
toReturn |= ((i & 0x000000FF) << 24);
toReturn |= ((i & 0x0000FF00) << 8);
toReturn |= ((i & 0x00FF0000) >> 8);
toReturn |= ((i & 0xFF000000) >> 24);
return toReturn;
}
//In short, data in ID3v2 tags are stored as
//"syncsafe integers". This is so the tag info
//isn't mistaken for audio data, and attempted to
//be "played". For more info, have fun Googling it.
int syncsafe(int i)
{
int toReturn = 0;
toReturn |= ((i & 0x7F000000) >> 24);
toReturn |= ((i & 0x007F0000) >> 9);
toReturn |= ((i & 0x00007F00) << 6);
toReturn |= ((i & 0x0000007F) << 21);
return toReturn;
}
//How much room does ID3 version 1 tag info
//take up at the end of this file (if any)?
int id3v1size(ifstream& infile)
{
streampos savePos = infile.tellg();
//get to 128 bytes from file end
infile.seekg(0, ios::end);
streampos length = infile.tellg() - (streampos)128;
infile.seekg(length);
int size;
char buffer[3] = {0};
infile.read(buffer, 3);
if(buffer[0] == 'T' && buffer[1] == 'A' && buffer[2] == 'G')
size = 128; //found tag data
else
size = 0; //nothing there
infile.seekg(savePos);
return size;
}
//how much room does ID3 version 2 tag info
//take up at the beginning of this file (if any)
int id3v2size(ifstream& infile)
{
streampos savePos = infile.tellg();
infile.seekg(0, ios::beg);
char buffer[6] = {0};
infile.read(buffer, 6);
if(buffer[0] != 'I' || buffer[1] != 'D' || buffer[2] != '3')
{
//no tag data
infile.seekg(savePos);
return 0;
}
int size = 0;
infile.read(reinterpret_cast<char*>(&size), sizeof(size));
size = syncsafe(size);
infile.seekg(savePos);
//"size" doesn't include the 10 byte ID3v2 header
return size + 10;
}
int main(int argCount, char* argValues[])
{
//you'll have to change this
ifstream infile("C:/Music/Bush - Comedown.mp3", ios::binary);
if(!infile.is_open())
{
infile.close();
cout << "Error opening file" << endl;
system("PAUSE");
return 0;
}
//determine beginning and end of primary frame data (not ID3 tags)
infile.seekg(0, ios::end);
streampos dataEnd = infile.tellg();
infile.seekg(0, ios::beg);
streampos dataBegin = 0;
dataEnd -= id3v1size(infile);
dataBegin += id3v2size(infile);
infile.seekg(dataBegin,ios::beg);
//determine bitrate based on header for first frame of audio data
int headerBytes = 0;
infile.read(reinterpret_cast<char*>(&headerBytes),sizeof(headerBytes));
headerBytes = reverse(headerBytes);
int bitrate = bitrates[(int)((headerBytes >> 12) & 0xF)];
//calculate duration, in seconds
int duration = (dataEnd - dataBegin)/(bitrate/8);
infile.close();
//print duration in minutes : seconds
cout << duration/60 << ":" << duration%60 << endl;
system("PAUSE");
return 0;
}
對於谷歌的追隨者的緣故,這裏有一些更多的外部庫:
mpg321 -t
的ffmpeg -i
midentify(mplayer的基本)看到Using mplayer to determine length of audio/video file
mencoder(通過它無效的參數,它會吐出一個錯誤消息,但也給你的信息在問題上的文件,前$ mencoder inputfile.mp3 -o假)
的MediaInfo程序http://mediainfo.sourceforge.net/en
exiftool
在linux 「文件」 命令
mp3info
SOX
參: https://superuser.com/questions/36871/linux-command-line-utility-to-determine-mp3-bitrate
http://www.ruby-forum.com/topic/139468
(使這個爲他人維基添加到)。
和庫:.NET:n音訊,JAVA:jlayer,C:的libmad
乾杯!
而且看看audioread(一些Linux發行版包括Ubuntu有包),https://github.com/sampsyo/audioread
audio = audioread.audio_open('/path/to/mp3')
print audio.channels, audio.samplerate, audio.duration
只需使用mutagen
$pip install mutagen
使用它在Python Shell:
from mutagen.mp3 import MP3
audio = MP3(file_path)
print audio.info.length
- 1. 更改多個mp3文件的長度
- 2. 得到的mp3文件長度
- 3. 獲取MP3文件的長度
- 4. 在Java中讀出Mp3歌曲的時間/長度/持續時間
- 5. .wav與.mp3文件的加載時間
- 6. 找到一個MP3的長度的文件
- 7. 如何在Perl中獲取MP3文件的長度?
- 8. 確定VB.net中mp3文件的長度(以毫秒爲單位)
- 9. 如何確定mp3文件頭的長度?
- 10. 長時間保存文件
- 11. 來自unix命令行的音頻文件的時間長度
- 12. 查找C#中的MP3長度
- 13. 以毫秒爲單位的mp3長度
- 14. 計算MP3長度的相應旋轉
- 15. 用不同的時間座標長度打開多個文件
- 16. 如何計算midi文件的時間長度
- 17. javascript長度持續時間
- 18. 調度程序時間線日期長時間捕捉事件
- 19. 受文本字段長度影響的補間持續時間
- 20. .caf文件長度不同,甚至持續時間相同
- 21. 使用pygame時,我的mp3文件的速度發生變化
- 22. 音頻文件的長度
- 23. XML文件的長度
- 24. 如何獲得laravel中的mp3文件的持續時間5.5
- 25. 如何獲得MP3文件的時間與cocos2d的
- 26. 獲取MP3的時間。
- 27. 視頻文件的時間長度,同時上傳到春季或J2EE
- 28. 使用Git來確定一行文本在文件中的時間長度
- 29. Node.js遠程mp3文件的持續時間和比特率
- 30. MP3/wav音頻文件的持續時間
在資源管理器中打開包含文件夾調出播放時間欄,屏幕顯示t,ORC,文本搜索....提交給每日跆拳道/笑話 – BCS 2008-09-23 06:35:41