2008-09-23 19 views
16

किसी दिए गए एमपी 3 फ़ाइल की लंबाई (सेकेंड में) निर्धारित करने का सबसे आसान तरीका क्या है, बाहरी पुस्तकालयों का उपयोग किए बिना? (पायथन स्रोत अत्यधिक सराहना की)एमपी 3 फ़ाइल की समय लंबाई

+5

एक्सप्लोरर में युक्त फ़ोल्डर खोलें प्ले टाइम कॉलम, स्क्रीन शू लाएं टी, ओआरसी, पाठ खोज .... दैनिक डब्ल्यूटीएफ/मजाक – BCS

उत्तर

24

आप pymad का उपयोग कर सकते हैं। यह एक बाहरी पुस्तकालय है, लेकिन यहां जाल की खोज के लिए मत आना। कोई विशेष कारण आप किसी बाहरी पुस्तकालय नहीं चाहते हैं?

import mad 

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

देखा गया here

-

तुम सच में एक बाहरी पुस्तकालय का उपयोग नहीं करना चाहते हैं, एक नज़र here है और बाहर की जाँच कैसे वह यह किया है। चेतावनी: यह जटिल है।

+0

पर जमा करें मैं बाहरी पुस्तकालय की सिफारिश करने के साथ सहमत हूं। मैंने इसका इस्तेमाल नहीं किया है (या उस मामले के लिए पायथन)। लेकिन मैंने एक बार लिखने की कोशिश की, सी ++ में एक प्रोग्राम जो बस एमपी 3 चला सकता था। वह बाहर नहीं था, लेकिन फ़ाइल की अवधि निर्धारित करने के लिए मैं काफी दूर था। मैंने उस कोड को दोबारा प्रतिक्रिया देने के बारे में सोचा ... –

+0

... यहां पोस्ट करें, लेकिन यह बहुत सुस्त है। (और सी ++ में, पायथन नहीं)। दूरस्थ रूप से भी सरल नहीं है। –

+1

बस एक सिर ऊपर, यह मुझे लगता है जैसे यह केवल कुछ प्लेटफार्मों पर काम करता है। सबसे हालिया संस्करण स्थापित करने पर क्रैश हो जाता है क्योंकि इसमें अपनी स्वयं की सेटअप फ़ाइलों में से एक की कमी है, जो यह अनुशंसा करता है कि मैं दूसरी फ़ाइल को लिनक्स कमांड के साथ चलाकर उत्पन्न करता हूं। –

0

आप फ़ाइल में फ्रेम की संख्या गिन सकते हैं। प्रत्येक फ्रेम में एक प्रारंभ कोड होता है, हालांकि मैं स्टार्ट कोड के सटीक मूल्य को याद नहीं कर सकता हूं और मेरे पास एमपीईजी चश्मा चारों ओर बिछा नहीं है। प्रत्येक फ्रेम में एक निश्चित लंबाई होती है, एमपीईजी 1 परत II के लिए लगभग 40 मिमी।

यह विधि सीबीआर-फाइलों (कॉन्सटेंट बिट दर) के लिए काम करती है, कैसे वीबीआर-फाइल काम एक पूरी तरह से अलग कहानी है।

नीचे दस्तावेज़ से:

लेयर के लिए मैं हमें इस सूत्र फ़ाइलें:

FrameLengthInBytes = (12 * बिटरेट/sampleRate + पैडिंग) * इस 4

परत के लिए द्वितीय & तृतीय फ़ाइलों का उपयोग सूत्र:

FrameLengthInBytes = 144 * बिटरेट/sampleRate + पैडिंग

Information about MPEG Audio Frame Header

+0

मेरा मानना ​​है कि लंबाई 26 मिमी है। –

+0

यह परिचित लगता है। –

8

सरल, कुछ गणना करने के लिए एमपी 3 बाइनरी ब्लॉब पार्स अजगर

वह एक सुंदर लंबा आदेश की तरह लगता है में। मुझे पायथन नहीं पता, लेकिन यहां कुछ कोड है जिसे मैंने एक और प्रोग्राम से दोबारा प्रतिक्रिया दी है जिसे मैंने लिखने की कोशिश की थी।

नोट: यह सी ++ में है (क्षमा करें, यह मुझे मिला है)। साथ ही, जैसा कि है, यह केवल स्थिर बिट दर एमपीईजी 1 ऑडियो लेयर 3 फाइलों को संभालेगा। सबसे अधिक कवर होना चाहिए, लेकिन मैं सभी स्थितियों में काम करने के लिए कोई गारंटी नहीं दे सकता। उम्मीद है कि यह वही करता है जो आप चाहते हैं, और उम्मीद है कि इसे पायथन में रीफैक्टर करना इसे स्क्रैच से करना आसान है।

// 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; 
} 
+1

मुझे नहीं लगता कि यह वीबीआर फाइलों के लिए काम करेगा। –

+1

यह अभी कुछ ऐसी चीज़ों के लिए काम करता है जो मैं अभी बना रहा हूं, क्योंकि मुझे वीबीआर समर्थन की आवश्यकता नहीं है। मुझे बदलने की जरूरत थी बिटरेट था क्योंकि यह 56k मान रहा था जब फाइलें 32k थीं (एलएएम से आउटपुट)। – alxp

+1

मुझे पता है, मैंने कहा कि यह स्थिर बिट दर फ़ाइलों के लिए केवल अच्छा था।इन ffmpeg और mpg321 के –

9

गूगल अनुयायियों 'के लिए, यहाँ कुछ और बाहरी libs हैं:

mpg321 आयकर

ffmpeg मैं

midentify (एम प्लेयर मूल रूप से) को देखने के Using mplayer to determine length of audio/video file

मेनकोडर (इसे अमान्य पैरा पास करें, यह एक त्रुटि संदेश थूक देगा लेकिन आपको फ़ाइल में जानकारी भी देगा, पूर्व $ mencoder inputfile.mp3 -o नकली)

MediaInfo कार्यक्रम http://mediainfo.sourceforge.net/en

exiftool

linux "फाइल" कमांड

mp3info

सॉक्स

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

(यह दूसरों के लिए एक विकि में जोड़ने के लिए कर रही है)।

और libs: .net: naudio, जावा: jlayer, c: libmad

चीयर्स!

+0

फ़ाइल फ़ाइलों के रूप में http लिंक को भी संभालते हैं, दुर्भाग्य से उत्तरार्द्ध स्वचालित रूप से फ़ाइल चलाता है, लेकिन ffmpeg के साथ पूरी तरह से खुश हूं :) – Kami

6

(सहित कुछ Linux distros ubuntu संकुल है) audioread पर एक नज़र डालें, बस का उपयोग mutagen

$pip install mutagen 

अजगर खोल में इसका इस्तेमाल करते हैं:

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

सर्वोत्तम उत्तर। यह पीआईपी के साथ आसानी से स्थापित है, कोई अन्य आवश्यकता नहीं है, और यह फ़ाइल की मेटाडेटा में संग्रहीत जानकारी देता है – Zvika

संबंधित मुद्दे