Mega Code Archive

 
Categories / Delphi / Files
 

Read the ID3 Tag from a MP3 File (without using a component)

Title: Read the ID3-Tag from a MP3-File (without using a component) Question: How can I get ID3-Tag informations from a MP3-File? (like song title, artist...) Answer: Every MP3-file contains an ID3-Tag-header, which is used to give informations about artist, title, album, publishing year and genre about a song. The header is always 128 bytes long and is located at very end of the MP3-file. The ID3-Tag structure is described as follows: type TID3Tag = packed record // 128 bytes TAGID: array[0..2] of char; // 3 bytes: Must contain TAG Title: array[0..29] of char; // 30 bytes: Song's title Artist: array[0..29] of char; // 30 bytes: Song's artist Album: array[0..29] of char; // 30 bytes: Song's album Year: array[0..3] of char; // 4 bytes: Publishing year Comment: array[0..29] of char; // 30 bytes: Comment Genre: byte; // 1 byte: Genere-ID end; To read informations about the ID3-Tag and display it in a dialog-box, try this function: procedure TForm1.Button1Click(Sender: TObject); const _mp3file='G:\Mp3\Miscellaneous\ATC - Around The World.mp3'; var id3tag: Tid3tag; mp3file: Tfilestream; begin mp3file:=Tfilestream.create(_mp3file,fmOpenRead); try mp3file.position:=mp3file.size-128; // jump to id3-tag mp3file.Read(id3tag,SizeOf(id3tag)); showmessage(' Title: '+id3tag.title+#13+ ' Artist: '+id3tag.artist+#13+ ' Album: '+id3tag.album+#13+ ' Year: '+id3tag.year+#13+ ' Comment: '+id3tag.comment+#13+ ' Genre-ID: '+inttostr(id3tag.genre) ); finally mp3file.free; end; end; This function simply reads the file described as _mp3file, jumps to the last 128 bytes, reads it and display the informations in a dialog box.