Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Extracting metadata info from a mp3 file using getID3 in Cake PHP?

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 675
    Comment on it

    getID3 is an open source PHP library which can extract information from multimedia file like audio, video and images in various formats.

    For detailed information about the types of files please view this link:

    http://www.getid3.org/demo/ .

    To download the library and view its demo please visit this link:

    http://getid3.sourceforge.net/ .

    But here i am only using getID3 to extract information from mp3 files.

    After downloading the file please copy the folder in app/Vendor folder. The only file you need to concerned is with getId3.php .

    Here is the controller code how to use getID3:

    <?php 
    	public function extractFile($filename) {
    		App::import('Vendor', 'getID3', array('file'=>'getid3/getid3.php'));
    		$this->getID3 = new getID3;
    		// Analyze file and store returned data in $ThisFileInfo
    		$ThisFileInfo = $this->getID3->analyze($filename);		
    		return $ThisFileInfo;
    	}
    ?>

    In the above function i.e. extractFile() i have included the getID3 library. The analyze() function analyzes the file and returns an array as an output. 

    <?php
         public function index() {
        	$this->layout=null;
        	$arr = array();
    		$dir = new Folder(WWW_ROOT . 'audio');
    		$files = $dir->find('.*\.mp3', true);
    		$result = $this->extractFile(WWW_ROOT . "audio/Khaab.mp3");
    		$tmp = $this->checkVal($result['tags'],$value);
    		$arr[] = array(
    					"Song_name" => $value,
    					"album"=>$tmp["album"][0],
    					"artist"=>$tmp["artist"][0],
    					"title"=>$tmp["title"][0],
    					"year"=>$tmp["year"][0],
    					"genre"=>$tmp["genre"][0]
    				);
    		print_r($arr);
        }
    ?>

    When the index.php is loaded i have called extractFile() function and passed a argument. The argument is the name of the file along with the path. In this case it is "http://localhost/mediaPlayer/audio/Khaab.mp3".

    Here is the a part of the output of the audio file returned by the extractFile():

    Array(
         [tags] => Array
            (
                [id3v2] => Array
                    (
                        [title] => Array
                            (
                                [0] => Khaab
                            )
    
                        [artist] => Array
                            (
                                [0] => Akhil
                            )
    
                        [album] => Array
                            (
                                [0] => Khaab
                            )
    
                        [genre] => Array
                            (
                                [0] => Punjabi
                            )
    
                        [year] => Array
                            (
                                [0] => 2016
                            )
    
                    )
    
            )
    )

    If you want view what is the whole output you can just print the $result variable.

    ?<php
            public function checkVal($val,$name){
    		if (!isset($val["id3v2"]['title']) && empty($val["id3v2"]['title'])) {
    			$val['id3v2']['title'][0] = $name;
    		}
    		if (!isset($val["id3v2"]['artist']) && empty($val["id3v2"]['artist'])) {
    			$val['id3v2']['artist'][0] = "unknown";
    		}
    		if (!isset($val["id3v2"]['album']) && empty($val["id3v2"]['album'])) {
    			$val['id3v2']['album'][0] = "unknown";
    		}
    		if (!isset($val["id3v2"]['year']) && empty($val["id3v2"]['year'])){ 
    			$val['id3v2']['year'][0] = "unknown";
    		}
    		if (!isset($val["id3v2"]['genre']) && empty($val["id3v2"]['genre'])) {
    			$val['id3v2']['genre'][0] = "Unknown";
    		}
    		return $val['id3v2'];
    	}
    ?>

    In the index()  function i have also called a function checkVal() for checking the whether the any tag is empty or not. If it is empty then pass the value unknown to it.

    Thats it thats all you need to to do extract the information like title, album, year etc from a single audio file.

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: