Народ. Про tag ID3v1 это вы верно говорите что в конце файла (128 байт) а про ID3v2 вы походу забыли или не захотели разбираться. Так вот: часто тег находится в начале файла. он неограничен по размеру, ну и полей больше. Алгоритм там екнутый на словах и не опишеш. Тут примерчик он читает тег ID3v1 если его нет то читает ID3v2.
| Код | Procedure GetMp3Tag(Filename:string;var tagrec:ttagrec); // TTagRec = array [0..11] of string; label exitproc; const MAXBytes = (High(integer) - $F) div sizeof(byte); type TByteArray = array[0..MAXBytes] of byte; PByteArray = ^TByteArray; Tconstmas = array [0..11,0..6] of byte; const ID3v2: array[0..2]of byte =(73,68,51); EndID3:array[0..1]of byte =(255,251);
Constlist:Tconstmas=( (84,69,78,67,0,0,0),(87,88,88,88,0,0,0),(84,67,79,80,0,0,0), (84,79,80,69,0,0,0),(84,67,79,77,0,0,0),(84,67,79,78,0,0,0), (84,82,67,75,0,0,0),(67,79,77,77,0,0,0),(84,89,69,82,0,0,0), (84,65,76,66,0,0,0),(84,80,69,49,0,0,0),(84,73,84,50,0,0,0)); {Encodedby,URL,Copyrigth,Origartist,Composer,Genre, Track,Comment,Year,Album:Artist:Title}
var Buf : PByteArray; FileLength,FileHandle,i,j,k,nseek,lengthtag,SizeBuf,nconstlist: Integer; stn:word; bool:boolean; begin try FileHandle := FileOpen(Filename,2); // Проверка на ID3v2 SizeBuf:=2; GetMem(Buf, SizeBuf*sizeof(byte)); FileSeek(FileHandle,0,0); FileRead(FileHandle,Buf^,3); bool:=true; for i:=0 to 2 do if Buf^[i]<>ID3v2[i] then bool:=false;
//Проверка на ID3v1 if bool=false then begin FreeMem(Buf, SizeBuf*sizeof(byte)); SizeBuf:=128; GetMem(Buf, SizeBuf*sizeof(byte)); FileLength := FileSeek(FileHandle,0,2); FileSeek(FileHandle,filelength-128,0); FileRead(FileHandle,Buf^,128); for i:=0 to 29 do begin TagRec[11]:=tagrec[11]+chr(buf^[i+3]); //title TagRec[10]:=tagrec[10]+chr(buf^[i+33]); //artist TagRec[9]:=tagrec[9]+chr(buf^[i+63]); //album end; for i:=0 to 31 do TagRec[8]:=tagrec[8]+chr(buf^[i+93]); //coment TagRec[7]:=tagrec[7]+inttostr(buf^[126]); //track TagRec[6]:=tagrec[6]+inttostr(buf^[127]); //genre goto exitproc; end;
FreeMem(Buf, SizeBuf*sizeof(byte)); //Поиск конца ID3v2 LengthTag:=0; SizeBuf:=1; GetMem(Buf, SizeBuf*sizeof(byte)); nseek:=0; repeat FileSeek(FileHandle,nseek,0); FileRead(FileHandle,Buf^,2); bool:=true; for i:=0 to 1 do if Buf^[i]<>EndID3[i] then bool:=false; if bool=true then lengthtag:=nseek; inc(nseek); until bool=true; // edit3.text:=inttostr(lengthtag); FreeMem(Buf, SizeBuf*sizeof(byte));
//Считываем Весь Тег SizeBuf:=LengthTag; GetMem(Buf, SizeBuf*sizeof(byte)); nseek:=0; FileSeek(FileHandle,nseek,0); FileRead(FileHandle,Buf^,LengthTag);
//Заносим данные из тега в массив nseek:=10; stn:=0; repeat nconstlist:=0; repeat bool:=true; for j:=nseek to nseek+6 do if buf^[j]<>constlist[nconstlist,j-nseek] then bool:=false; if bool=false then inc(nconstlist); until (nconstlist=12) or (bool=true);
if bool=true then begin for k:=nseek+11 to nseek+11+buf^[nseek+7]-2 do TagRec[nconstlist]:=tagrec[nconstlist]+chr(buf^[k]); end; nseek:=nseek+11+buf^[nseek+7]-1; inc(stn); until stn>=12;
exitproc: if tagrec[1]<>'' then Delete(tagrec[1],1,1);
finally FreeMem(Buf, SizeBuf*sizeof(byte)); FileClose(FileHandle); end; end;
|
|