![](/img/trans.png)
[英]Decoding Binary via fget / buffer string (Trying to get mp3 header)
[英]Accessing binary MP3 Header in C via fopen
我正在嘗試從文件中提取 mp3 header。 這與 ID3 標簽不同——mp3 header 是保存有關 MPEG 版本、比特率、頻率等信息的地方。
您可以在此處查看 mp3 header 結構的概述: http://upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg
我的問題是,盡管加載文件並且現在收到有效的(據我所知)二進制 output,但我沒有看到預期值。 mp3 文件的前 12 位應該全為 1,用於 mp3 同步字。 但是,僅前 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]);
}
ID3v2占據了MP3文件的第一位(如果存在)。 文件的前三個字節為“ ID3”:
http://www.id3.org/id3v2.4.0-structure
有兩種處理方法。 第一種是檢查ID3標簽的存在,然后解析10個字節的標頭以獲取標簽大小,然后跳過那么多字節。
編輯:如果解析標頭,則需要檢查將“標志”字段中的第4位設置為1,如果是,則需要跳過另外的10個字節以超過頁腳。
或者,您可以只搜索MP3,直到您點擊同步模式。 ID3v2的設置方式不應連續出現11個1位,以確保與不支持它的播放器兼容。
fseek(mp3file,1,SEEK_SET);
您跳過文件的第一個字節是否有原因?
嘗試
fseek(mp3file,0,SEEK_SET)
代替
fseek(mp3file,1,SEEK_SET).
文件從字節位置0開始。
我想你可能想要
fseek(mp3file,0,SEEK_SET);
ID3信息可能排在最前面。 前3個字符是ID3
嗎?
fseek(mp3file,1,SEEK_SET);
使您跳過前8位,因此fread會得到9至16位
這種方法對我有用。 但是我不得不刪除int i;
在//counter
下
這可能是你需要做的。 打印的所有位 1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.