문제
주어진 MP3 파일의 길이 (초)를 결정하는 가장 간단한 방법은 무엇입니까? 외부 라이브러리를 사용하지 않고? (파이썬 소스가 높게 높이 평가됩니다)
다른 팁
Google 팔로워를 위해 다음은 외부 Libs가 더 있습니다.
MPG321 -T
ffmpeg -I
MINGELIFY (기본적으로 MPLAYER)를 참조하십시오 MPLAYER를 사용하여 오디오/비디오 파일의 길이
mencoder (전달 된 매개 변수를 전달하면 오류 메시지를 뱉어 내지 만 해당 파일에 대한 정보를 제공합니다. ex $ mencoder inputfile.mp3 -o fake)
MediaInfo 프로그램 http://mediainfo.sourceforge.net/en
exiftool
Linux "파일"명령
mp3info
삭스
참조 :https://superuser.com/questions/36871/linux-command-line-utility-to-determine-mp3-bitrate
http://www.ruby-forum.com/topic/139468
(다른 사람들이 추가 할 Wiki로 만드는).
및 libs : .net : naudio, java : jlayer, c : libmad
건배!
간단한, Python에서 무언가를 계산하려면 MP3 이진 blob
그것은 꽤 키가 큰 순서처럼 들립니다. 나는 Python을 모르지만 여기에 내가 한 번 작성하려고 한 다른 프로그램에서 리팩트 한 코드가 있습니다.
메모: 그것은 C ++에 있습니다 (죄송합니다. 내가 가진 것입니다). 또한, 상수 비트 속도 MPEG 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;
}
간단히 사용하십시오 mutagen
$pip install mutagen
파이썬 쉘에서 사용하십시오.
from mutagen.mp3 import MP3
audio = MP3(file_path)
print audio.info.length
또한 Audioread (Ubuntu를 포함한 일부 Linux 배포판이 패키지가 있습니다)를 살펴보십시오. https://github.com/sampsyo/audioread
audio = audioread.audio_open('/path/to/mp3')
print audio.channels, audio.samplerate, audio.duration
파일의 프레임 수를 계산할 수 있습니다. 시작 코드의 정확한 값을 회상 할 수는 없지만 각 프레임에는 시작 코드가 있습니다. 각 프레임은 MPEG1 층 II의 경우 약 40ms의 특정 길이를 갖습니다.
이 방법은 CBR 파일 (일정한 비트 속도)에 대해 작동하며 VBR 파일의 작동 방식은 완전히 다른 이야기입니다.
아래 문서에서 :
레이어 I의 경우이 공식을 제출합니다.
framelengthinbytes = (12 * 비트 전송 속도 / 샘플 리테이션 + 패딩) * 4
계층 II 및 III 파일의 경우이 공식을 사용합니다.
framelengthinbytes = 144 * 비트 전송 속도 / 샘플 리테이션 + 패딩