2008-09-23 114 views
16

什麼是最簡單的方法來確定給定的MP3文件的長度(以秒爲單位),而不使用外部庫? (蟒蛇源高度讚賞)mp3文件的時間長度

+5

在資源管理器中打開包含文件夾調出播放時間欄,屏幕顯示t,ORC,文本搜索....提交給每日跆拳道/笑話 – BCS 2008-09-23 06:35:41

回答

24

您可以使用pymad。這是一個外部圖書館,但不要落入這個不發明的陷阱。任何特定的原因你不想要任何外部庫?

import mad 

mf = mad.MadFile("foo.mp3") 
track_length_in_milliseconds = mf.total_time()  

發現here

-

如果你真的不想使用外部庫,看看here,並檢查了他是怎麼做到的。警告:這很複雜。

+0

我同意推薦外部圖書館。我沒有用過它(或者Python)。但我曾經試着用C++寫一個可以簡單播放MP3的程序。這並沒有出現,但我確定了足夠的時間來確定文件的持續時間。我想過重構該代碼... – 2008-09-23 06:50:32

+0

...在這裏發佈,但它是相當複雜的。 (而在C++中,不是Python)。甚至不是很簡單。 – 2008-09-23 06:52:21

+1

只是擡頭,它看起來像它只能在某些平臺上工作。最新的版本在安裝時崩潰,因爲它缺少自己的安裝文件之一,它建議我通過運行第二個帶有linux命令的文件來生成它。 – 2015-03-15 00:35:53

0

您可以計算文件中的幀數。每個幀都有一個起始碼,儘管我無法回憶起始碼的確切值,也沒有MPEG規格。每個幀都有一定的長度,對於MPEG1 II層大約爲40ms。

這種方法適用於CBR文件(恆定比特率),VBR文件如何工作是一個完全不同的故事。

從下面的文件:

對於層I文件我們這個公式:

FrameLengthInBytes =(12 *比特率/採樣率+填充)* 4

用於層II & III文件使用這式:

FrameLengthInBytes = 144 *碼率/採樣率+填充

Information about MPEG Audio Frame Header

+0

我相信這個長度是26ms。 – 2009-02-27 21:36:34

+0

聽起來很熟悉。 – 2009-03-05 12:43:22

8

簡單,分析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; 
} 
9

對於谷歌的追隨者的緣故,這裏有一些更多的外部庫:

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

mp3 length in milliseconds

(使這個爲他人維基添加到)。

和庫:.NET:n音訊,JAVA:jlayer,C:的libmad

乾杯!

3

而且看看audioread(一些Linux發行版包括Ubuntu有包),https://github.com/sampsyo/audioread

audio = audioread.audio_open('/path/to/mp3') 
print audio.channels, audio.samplerate, audio.duration 
6

只需使用mutagen

$pip install mutagen 

使用它在Python Shell:

from mutagen.mp3 import MP3 
audio = MP3(file_path) 
print audio.info.length