getid3 component for cakephp

What is getID3?

getID3() is an open-source, cross-platform software library for the PHP language written by James Heinrich and Allan Hansen. getID3() can extract information from multimedia file formats: audio, video and images in various formats. Information extracted includes playtime, metadata, bitrate, sample rate, number of channels, and much more. getID3() can also write metadata (tags) to several formats.

Component for CakePHP

Its very straight forward to use getid3() in cakephp and this article describes how i used it in my cakephp application. I downloaded getid3() library, extracted it to vendors directory and created a component named getid3. Then i used it in my controller class to extract the media file information.

Although i have extracted only the necessary file information for my system to work, you may extract additional information by making a few changes in component’s extract() function (Hint: try printing $getid3->info array for available information). Anyone having problem implementing may through a comment here and maybe i could help you.

To download getid3() library click here.

Source code for app/controllers/components/getid3.php component:

class Getid3Component extends Object
{
	
	var $controller;
	var $result = array();
	
	function __construct()	{
		set_time_limit(20*3600);
		ignore_user_abort(false);
		
		vendor('getid3/getid3');
		vendor('getid3/extras/abstraction');
	}

	function info($filepath)	{

		$getid3 = new getID3;
		$getid3->encoding = 'UTF-8';
		
		// Tell browser to use UTF-8 encoding as well.
		header('Content-Type: text/html; charset=UTF-8');
		try {
		
			$getid3->Analyze($filepath);
			return $getid3->info;
		
		}
		catch (Exception $e) {			
			return 'An error occured: ' .  $e->message;
		}		
	}	

	function extract($full_name)	{
	
		$getid3 = new getID3;
		$getid3->encoding = 'UTF-8';
		
		if (!is_file($full_name)) {
			return "ERROR: file ($full_name) does not exists";
		}
		
		$g = new xml_gen;
		
		try {
		
			$time = getmicrotime();

			$getid3->Analyze($full_name);//die;
			$time = getmicrotime() - $time;

			$this->result['filename'] = basename($getid3->filename);
			$this->result['filesize'] = @$getid3->info['filesize'];
			$this->result['fileformat'] = @$getid3->info['fileformat'];
			
			if (@$getid3->info['audio']['dataformat'] && $getid3->info['audio']['dataformat'] != $getid3->info['fileformat']) {
				$this->result['audio']['dataformat'] = @$getid3->info['fileformat'];
			}
			
			if (@$getid3->info['video']['dataformat'] && $getid3->info['video']['dataformat'] != $getid3->info['fileformat'] && $getid3->info['video']['dataformat'] != @$getid3->info['audio']['dataformat']) {
				$this->result['video']['dataformat'] = @$getid3->info['fileformat'];
			}
			
			$this->result['length'] = @$getid3->info['playtime_string'];
	
			$this->result['bitrate'] = (@$getid3->info['bitrate'] ? number_format($getid3->info['bitrate']/1000) . 'k' : '');
			
			$this->result['audio']['sample_rate'] = @$getid3->info['audio']['sample_rate'] ? number_format($getid3->info['audio']['sample_rate']) . '/' .  (@$getid3->info['audio']['bits_per_sample'] ? $getid3->info['audio']['bits_per_sample'] . '/' : '') .  @$getid3->info['audio']['channels'] : '';
		
			$this->result['artist'] = $this->result['title'] = $this->result['album'] = '';
			
			if (@$getid3->info['tags']) {
				foreach ($getid3->info['tags'] as $tag => $tag_info) {
					if (@$getid3->info['tags'][$tag]['artist'] || @$getid3->info['tags'][$tag]['title'] || @$getid3->info['tags'][$tag]['album'] || @$getid3->info['tags'][$tag]['genre']) {
						$this->result['artist'] = @implode('', @$getid3->info['tags'][$tag]['artist']);
						$this->result['title']  = @implode('', @$getid3->info['tags'][$tag]['title']);
						$this->result['album']  = @implode('', @$getid3->info['tags'][$tag]['album']);
						$this->result['genre']  = @implode('', @$getid3->info['tags'][$tag]['genre']);
						break;
					}
				}
			}
		
			$this->result['tags'] = @implode(", ", @array_keys(@$getid3->info['tags']));
		
			$this->result['warning'] = @implode("
", @$getid3->info['warning']); return $this->result; } catch (Exception $e) { return 'ERROR: ' . $e->message; } } }

Source code for app/controllers/albums_controller:

class AlbumsController extends AppController{
	................
	var $components = array ('...','...','Getid3');//loading getid3 component along with other components
	................
	................
	function getInfo()	{	

		$info = $this->Getid3->extract("D:/path/to/mediafile/Backdrifts.m4a");	

		if(!is_array($info))	{
			__('No meta data found for this song! Can not continue.. exiting...');
			exit;
		}

		pr($info);
	}
}

I hope this helps someone.

10 thoughts on “getid3 component for cakephp

  1. Tanks, now i got this error Fatal error: Call to undefined method getid3::setOption() in /opt/lampp/htdocs/selva/task/strapp/app/Controller/Component/Getid3Component.php on line 77

    Thanks you very much.

  2. Hi, i have used this component in CakePHP 1.3.x versions it work’s well. Now am trying to use this component in 2.x versions it’s giving me error like this “Warning: Invalid argument supplied for foreach() [CORE/Cake/Utility/ObjectCollection.php, line 314]” AND “Fatal error: Call to a member function allow() on a non-object in /app/Controller/AppController.php on line 55”

    1. Venkatesh,

      I didn’t get a chance yet to test it with CakePHP 1.3. I think you should be able to make it working after following this CakePHP migration guide. Check the section pertaining to Components on this page, where you find “Component is now the required base class for all components”. I hope that helps.

  3. The vendor lines should be rewritten as such (for those that are in a rush to get something deployed):

    App::import(‘Vendor’,’getid3/getid3′);
    App::import(‘Vendor’,’getid3/extras/abstraction’);

  4. hi,

    I was trying to create a getid3 component of my own, but there was a problem in including getid3.lib.php.

    I resolved that by changing the filename to getid3lib.php and did an App::import.

    But another problem is that it couldn’t include other module files such as module.tag.id3v1.php.

    I hope you can help me resolve this.

    Thanks you very much.

    1. @fikri, As you have mentioned that you are using your own component, i am not sure how you write it there. And for the changing of file name i am not sure how did it help. I would have help you, but for that i have to see the code you are trying to run. You may post related controller and library code here so i could see it.

    1. No. I haven’t tried write tag in the controller. This component was built to read tags. Anyway thanks for your suggestion. I ‘ll add write method to it.

    1. @Éber Freitas Dias

      I am using 1.2 already but when the related project started there was cakephp 1.1 around.

      One should change vendor() to App:import of course in 1.2. Thanks Éber Freitas Dias for pointing this out.

Leave a Reply