Important alert: (current site time 7/15/2013 10:31:39 AM EDT)
 

VB icon

AcidicChip's MP3 ID3v2 Tag Writer 1.0

Email
Submitted on: 8/14/2002 3:40:16 PM
By: AcidicChip  
Level: Beginner
User Rating: By 1 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 14529
author picture
(About the author)
 
     This script will take an MP3 file, with the ID3v2 Tags you want and output them into a single MP3 file to download. This script will *NOT* edit the MP3 file itself.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: AcidicChip's MP3 ID3v2 Tag Writer 1.0
// Description:This script will take an MP3 file, with the ID3v2 Tags you want and output them into a single MP3 file to download. This script will *NOT* edit the MP3 file itself.
// By: AcidicChip
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=710&lngWId=8//for details.//**************************************

<?
	// Script Name: AcidicChip's MP3 ID3v2 Tag Writer 1.0
	//
	// Script Author: Chance O. One (acidicchip@acidicchip.com)
	//
	// Script Description: This script will take an MP3 file, with the ID3v2 Tags you want and output them into 
	// a single MP3 file to download. This script will *NOT* edit the MP3 file itself. If 
	// you wish to make this script write the tags to the physical file, instead of the file 
	// that it outputs to download, you will have to make the changes yourself. Please do 
	// not e-mail me, asking me how to do it. Feel free to e-mail me, and let me know of 
	// any bugs it may have.
	//
	// Author's Note: I tried to make this script as easy as possible, so that even newbies can understand it.
	//But if there is something that you don't understand, go ahead and e-mail me, and I'll 
	//help you understand it the best I can. 
	
	// Change the line below to the path of your MP3 file. eg: $MP3 = "music/mp3/file.mp3";
	$MP3 = "TetrisXP.mp3";
	
	// The function below is a reverse function of bin2hex. It will convert hex to binary.
	function hex2bin($data) {
		$len = strlen($data);
		for($i=0;$i<$len;$i+=2) {
			$newdata .= pack("C",hexdec(substr($data,$i,2)));
		}
		return $newdata;
	}
	
	// Change the variable values below to what you want for the ID3v2 Tags.
	$TrackNum = "01";
	$Title = "This is the title";
	$Artist	= "This is the artist";
	$Album = "This is the album";
	$Year = "2002";
	$Genre = "This is the genre";
	$Comment = "This is the comment";
	$Composer = "This is the composer";
	$OrigArtist = "This is the original artist";
	$Copyright = "This is the copyright";
	$URL = "This is the url";
	$EncodedBy = "This is the encoded by";
	
	// The variables below are converting the values you entered for the tags (above) into hex, in order to 
	// have a better reassurance that your MP3 won't sound funny.
	$TrackNumHEX = bin2hex($TrackNum);
	$TitleHEX = bin2hex($Title);
	$ArtistHEX	= bin2hex($Artist);
	$AlbumHEX = bin2hex($Album);
	$YearHEX = bin2hex($Year);
	$GenreHEX = bin2hex($Genre);
	$CommentHEX = bin2hex($Comment);
	$ComposerHEX = bin2hex($Composer);
	$OrigArtistHEX = bin2hex($OrigArtist);
	$CopyrightHEX = bin2hex($Copyright);
	$URLHEX = bin2hex($URL);
	$EncodedByHEX = bin2hex($EncodedBy);
	
	// The variables below are creating a hex value for the length of each of the values you entered for the 
	// tags (above).
	$TrackNumLenHEX = bin2hex(chr((strlen($TrackNum) + 1)));
	$TitleLenHEX = bin2hex(chr((strlen($Title) + 1)));
	$ArtistLenHEX	= bin2hex(chr((strlen($Artist) + 1)));
	$AlbumLenHEX = bin2hex(chr((strlen($Album) + 1)));;
	$YearLenHEX = bin2hex(chr((strlen($Year) + 1)));;
	$GenreLenHEX = bin2hex(chr((strlen($Genre) + 1)));
	$CommentLenHEX = bin2hex(chr((strlen($Comment) + 5)));
	$ComposerLenHEX = bin2hex(chr((strlen($Composer) + 1)));
	$OrigArtistLenHEX = bin2hex(chr((strlen($OrigArtist) + 1)));
	$CopyrightLenHEX = bin2hex(chr((strlen($Copyright) + 1)));
	$URLLenHEX = bin2hex(chr((strlen($URL) + 2)));
	$EncodedByLenHEX = bin2hex(chr((strlen($EncodedBy) + 1)));
	
	// The lines below is the creation of the ID3v2 tags. The tags are usually in this hex format:
	// XXXXXXXX000000YY0000ZZZZZZZZZZZZZZZZZZZZ
	// X = The marking for the tag, telling the MP3 player that this tag is right here
	// Y = The length of the text inputted for the tag (Usually +1)
	// Z = The content of the tag
	$Data = "49443303000000000E7A";
	$Data .= "5452434B000000" . $TrackNumLenHEX . "000000" . $TrackNumHEX;
	$Data .= "54454E43000000" . $EncodedByLenHEX . "400000" . $EncodedByHEX;
	$Data .= "57585858000000" . $URLLenHEX . "00000000" . $URLHEX;
	$Data .= "54434F50000000" . $CopyrightLenHEX . "000000" . $CopyrightHEX;
	$Data .= "544F5045000000" . $OrigArtistLenHEX . "000000" . $OrigArtistHEX;
	$Data .= "54434F4D000000" . $ComposerLenHEX . "000000" . $ComposerHEX;
	$Data .= "434F4D4D000000" . $CommentLenHEX . "00000000000000" . $CommentHEX;
	$Data .= "54434F4E000000" . $GenreLenHEX . "000000" . $GenreHEX;
	$Data .= "54594552000000" . $YearLenHEX . "000000" . $YearHEX;
	$Data .= "54414C42000000" . $AlbumLenHEX . "000000" . $AlbumHEX;
	$Data .= "54504531000000" . $ArtistLenHEX . "000000" . $ArtistHEX;
	$Data .= "54495432000000" . $TitleLenHEX . "000000" . $TitleHEX;
	
	// The ID3v2 Tags are EXACTLY 2048 bytes, no matter what. The lines below are filling in the remaining 2048 
	// bytes (4096 HEX character).
	$FillIn = (4096 - strlen($Data));
	$Data .= str_repeat("0", $FillIn);
	
	// The lines below are checking to see if an ID3v2 tag already exists in the MP3.
	$MusicFile = fopen($MP3, "r");
	If (fread($MusicFile, 3) == "ID3") {
		// If the ID3v2 tag does exist, this line tells it to start the file read after 2048 bytes (The length 
		// of the ID3v2 tags.)
		fseek($MusicFile, 2048);
	}
	
	// The line below is converting the tags from HEX to binary.
	$FinalOutput = hex2bin($Data);
	
	// The line below is telling your browser that the data that is going to be sent is an MP3 file to download 
	// (or play)
	header("Content-Type: audio/mp3");
	
	// The line below is uploading your ID3v2 tags.
	echo($FinalOutput);
	// The line below is uploading your MP3 file right after the tags (both are being sent as one file)
	echo(fread($MusicFile,FileSize($MP3)));
	
	fclose($MusicFile);
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/29/2003 7:04:55 AM

Could this code be modified to get the current ID2 tag info about a MP3?
Thanks, Martin.
(If this comment was disrespectful, please report it.)

 
5/29/2003 1:48:25 PMAcidicChip

I'm sure it could. I haven't tried it, but it's not very different from writing the tag.
(If this comment was disrespectful, please report it.)

 
4/26/2006 10:52:28 PMmethod

Thanks fo sharing this script. Does it read id3 tags in remote server too? I am looking for such solution.
(If this comment was disrespectful, please report it.)

 
4/30/2006 11:27:21 AMAcidicChip

This script only writes the ID3 tags. It shouldn't be difficult to use this script as a reference to create a tag reader. I'm amazed this script is still up. This was one of the first PHP scripts I made, lol. It can definently use some cleaning up. I'll prolly rewrite it and add a reader function as well. When/If I do, I'll post the link to it here.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.