How to use getid3, if content will be in MySQL?

The place for "I can't figure out how to..." questions.
Post Reply
Posts: 1
Joined: Sun Oct 27, 2013 8:30 am
Are you a spambot?: no

How to use getid3, if content will be in MySQL?

Post by ERET1K » Sun Oct 27, 2013 8:40 am

I want to use getid3, but my audio content will be in data base (MySQL).
How i can to use getid3 in this situation?

James Heinrich
getID3() v1 developer
Posts: 1427
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada

Re: How to use getid3, if content will be in MySQL?

Post by James Heinrich » Tue Oct 29, 2013 1:51 pm

getID3 is very much file-based, so the easiest way would be to pull the audio data out of your database into a temporary file, analyze it, and I would recommend storing the analysis results back in the same database table so you won't have to analyze one row more than once, and/or you can clear the analysis field if the audio content is changed.

For example:

Code: Select all

$result = mysql_query("SELECT `audiodata`, `getid3_analysis` FROM `audiotable` WHERE `id` = ".intval($_REQUEST['id']);
if ($row = mysql_fetch_assoc($result)) {
	if (is_null($row['getid3_analysis'])) {
		// only call analyze if no previous analysis is available
		$getID3 = new getID3;
		$temp_filename = tempnam(GETID3_TEMP_DIR, 'getID3');
		file_put_contents($temp_filename, $row['audiodata']);
		$ThisFileInfo = $getID3->analyze($temp_filename);
		// store analysis results so next time this row is accessed getID3 does not need to be called again
		mysql_query("UPDATE `audiotable` SET `getid3_analysis` = '".mysql_real_escape_string(serialize($ThisFileInfo))."' WHERE `id` = ".intval($_REQUEST['id']);
	} else {
		$ThisFileInfo = unserialize($row['getid3_analysis']);
	echo 'File is '.htmlspecialchars($ThisFileInfo['dataformat']).' format and '.htmlspecialchars($ThisFileInfo['playtime_string']).' long.';
} else {
	echo 'Failed to find file #'.intval($_REQUEST['id']);

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest