Fopen을 통해 C에서 이진 MP3 헤더에 액세스
문제
파일에서 MP3 헤더를 추출하려고합니다. 이것은 ID3 태그와는 다릅니다. MP3 헤더는 MPEG 버전, 비트 속도, 주파수 등에 대한 정보가 유지되는 곳입니다.
MP3 헤더 구조의 개요를 여기에서 볼 수 있습니다. http://upload.wikimedia.org/wikipedia/commons/0/01/mp3filestructure.svg
내 문제는 파일을로드하고 이제 유효한 (아는 한) 이진 출력을 받음에도 불구하고 예상 값을 보지 못한다는 것입니다. MP3 파일의 첫 12 비트는 MP3 Sync Word의 경우 모든 것이어야합니다. 그러나 나는 처음 8 비트만으로 다른 것을 받고 있습니다. 이것은 나에게 문제를 제안 할 것이다.
참고로, 나는 Fopen을 통해 유효한 MP3 파일이 첨부되어 있습니다.
// Main function
int main (void)
{
// Declare variables
FILE *mp3file;
char requestedFile[255] = "";
unsigned long fileLength;
// Counters
int i;
// Tryout
unsigned char byte; // Read from file
unsigned char mask = 1; // Bit mask
unsigned char bits[8];
// Memory allocation with malloc
// Ignore this at the moment! Will be used in the future
//mp3syncword=(unsigned int *)malloc(20000);
// Let's get the name of the file thats requested
strcpy(requestedFile,"testmp3.mp3"); // lets hardcode this into here for now
// Open the file
mp3file = fopen(requestedFile, "rb"); // open the requested file with mode read, binary
if (!mp3file){
printf("Not found!"); // if we can't find the file, notify the user of the problem
}
// Let's get some header data from the file
fseek(mp3file,0,SEEK_SET);
fread(&byte,sizeof(byte),1,mp3file);
// Extract the bits
for (int i = 0; i < sizeof(bits); i++) {
bits[i] = (byte >> i) & mask;
}
// For debug purposes, lets print the received data
for (int i = 0; i < sizeof(bits); i++) {
printf("Bit: %d\n",bits[i]);
}
해결책
ID3 정보가 먼저 올 수 있습니다. 처음 3 자입니다 ID3
?
다른 팁
ID3V2는 MP3 파일의 첫 번째 비트를 차지합니다. 파일의 처음 세 바이트는 "id3"입니다.
http://www.id3.org/id3v2.4.0-structure
그것을 다루는 두 가지 방법이 있습니다. 첫 번째는 ID3 태그의 존재를 확인한 다음 태그 크기에 대해 10 바이트 헤더를 구문 분석하고 많은 바이트를 건너 뛰는 것입니다.
편집 : 헤더를 구문 분석하는 경우 플래그 필드의 4 번째 비트가 하나로 설정되도록 확인해야합니다. 그렇다면 바닥 글을 지나서 10 바이트를 건너 뛰어야합니다.
또는 동기화 패턴에 부딪 칠 때까지 MP3를 찾을 수 있습니다. ID3V2가 설정되는 방식, 11 개의 비트가 발생하지 않아야합니다. 이는 지원하지 않는 플레이어와의 호환성을 보장합니다.
fseek(mp3file,1,SEEK_SET);
파일의 첫 바이트를 건너 뛰는 이유가 있습니까?
노력하다
fseek(mp3file,0,SEEK_SET)
대신에
fseek(mp3file,1,SEEK_SET).
파일은 바이트 위치에서 시작합니다.
아마 원할 것 같아요
fseek(mp3file,0,SEEK_SET);
fseek(mp3file,1,SEEK_SET);
처음 8 비트를 건너 뛸 수있게되므로 Fread와 함께 얻는 것은 비트 9 ~ 16입니다.