May 08, 2011

PHP function to edit mp3 tags

This is a PHP function that can be used for altering meta data ( tags ) of a mp3 file.

It makes use of getid3 so make sure that it's available in same folder.



<?php
/*
 * Author : Techno
 * Posted at technoslab.blogspot.com
 * Visit for more.
*/


/* The function for mp3 tag processing */
function process_tags($mp3_file,$data=""){
    if(!
is_file($mp3_file))return false;
    
$mp3_tagformat 'UTF-8';
    require_once(
'getid3/getid3.php');
    
$mp3_handler = new getID3;
    
$mp3_handler->setOption(array('encoding'=>$mp3_tagformat));
    require_once(
'getid3/write.php');
    
$mp3_writter = new getid3_writetags;
    
$mp3_writter->filename       $mp3_file;
    
$mp3_writter->tagformats     = array('id3v1''id3v2.3');
    
$mp3_writter->overwrite_tags true;
    
$mp3_writter->tag_encoding   $mp3_tagformat;
    
$mp3_writter->remove_other_tags false;

    
$mp3_data['title'][]   = $data['mp3_songname'];
    
$mp3_data['artist'][]  = $data['mp3_artist'];
    
$mp3_data['album'][]   = $data['mp3_album'];
    
$mp3_data['year'][]    = $data['mp3_year'];
    
$mp3_data['genre'][]   = $data['mp3_genre'];
    
$mp3_data['comment'][] = $data['mp3_comment'];

        
$mp3_data['attached_picture'][0]['data'] = file_get_contents($data['mp3_image']['path']);
        
$mp3_data['attached_picture'][0]['picturetypeid'] = $data['mp3_image']['type'];
        
$mp3_data['attached_picture'][0]['description'] = $data['mp3_image']['name'];
        
$mp3_data['attached_picture'][0]['mime'] = $data['mp3_image']['type'];

        
$mp3_writter->tag_data $mp3_data;
        
$mp3_writter->WriteTags();
}
/* End of function */






/* Prepare an array that holds tag info */


$data = array(

"mp3_songname" => "technoslab.blogspot.com"// Song's name. 
"mp3_artist" => "technoslab",// Artist's name. 
"mp3_album" => "technoslab"// Album's name. 
"mp3_year" => "2011"// Song's year. 
"mp3_genre" => "technoslab"// Genre. 
"mp3_comment" => "Visit technoslab.blogspot.com "// Some comment on song.
"mp3_image" => array(
    
"path" => "../albumart.jpg"// Albumart image must be present at this path.
    
"type" => "image/jpeg"// Albumart image's mime type.
    
"name" => "albumart.jpg" // Albumart image's name.
    
)
);

/* Call the function */
process_tags("../my_song.mp3",$data);
?>


Follow the script. There's an usage example included.

7 comments:

  1. there are more tags in a mp3..can we write them also??

    ReplyDelete
  2. doesn't work at all. please help. Program runs but nothing happens.

    ReplyDelete
  3. help plz how to use
    it's not working at all

    ReplyDelete
  4. Of course it will not work: there is the library require_once('getid3/getid3.php');require_once('getid3/write.php'); ?
    People that want to test it: first search on Google these libs then run the code, so it will work.

    ReplyDelete
  5. Thanks for the aricle, i'll try this on my new mp3 song download blog!

    ReplyDelete