Get mp3 playtime and write it on webpage

The place for "I can't figure out how to..." questions.
Post Reply
gilibaus
User
Posts: 2
Joined: Thu Feb 08, 2018 3:45 pm
Are you a spambot?: no

Get mp3 playtime and write it on webpage

Post by gilibaus » Thu Feb 08, 2018 3:54 pm

Hi,

I am no PHP expert so please be patient if my question is very simple. I have a podcast page on my website where it is possible to play mp3 files hosted on Podbean. I would like to get mp3 playtime from each one of them and write it besides episode title. Will someone please give me some hints on how to achieve that? The page is http://www.mindfulnessbergamo.net/podcast. Thanks for your help.

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

Re: Get mp3 playtime and write it on webpage

Post by James Heinrich » Thu Feb 08, 2018 8:20 pm

Your linked page already seems to show the duration. Perhaps you are manually typing that part right now.

The problem with your setup is that your MP3 files are not on the same server as your website. That means to analyze the MP3 file for playtime (or any other detail) you would need to copy some or all of the file locally to analyze. That's fine to do once, but you don't want that to happen every time someone loads your page. If you can analyze the file once and store the results in a database then great, but since you said you're new to PHP I assume you don't have a database available.

If all you need is the playtime, and you know that you always encode the MP3s at the same bitrate (128kbps in the example I looked at) then you could get away with calculating the playtime from the filesize (which is relative cheap to get remotely) and known bitrate.
This PHP function will get a remote filesize:

Code: Select all

function filesize_remote($remotefile, $timeout=10, $allow_follow=5) {
	$size = false;
	$url = @parse_url($remotefile);
	if (empty($url['host'])) {
		return false;
	}
	$commandtypes = array('HEAD', 'GET');
	foreach ($commandtypes as $commandtype) {
		// SHOULD work with HEAD, but some servers only send Content-Length with GET
		if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
			$HEADline = $commandtype.' '.@$url['path'].(@$url['query'] ? '?'.$url['query'] : '').' HTTP/1.0'."\r\n".'Host: '.@$url['host']."\r\n\r\n";
			fwrite($fp, $HEADline);
			stream_set_timeout($fp, $timeout);
			$linecounter = 0;
			while (!feof($fp)) {
				$headerline = trim(fgets($fp, 4096));
				if (preg_match('#^HTTP/[0-9\\.]{3} ([0-9]{3}) (.*)$#', $headerline, $matches)) {
					$status_code = intval($matches[1]);
					$status_text =        $matches[2];
					if ($status_code >= 400) {
						fclose($fp);
						return false;
					}
				}
				if (preg_match('#^Content-Length: (.*)#i', $headerline, $matches)) {
					$size = intval($matches[1]);
					fclose($fp);
					break 2;
				}
				if ($allow_follow && preg_match('#^Location: (.*)#i', $headerline, $matches)) {
					fclose($fp);
					return filesize_remote($matches[1], $timeout, $allow_follow - 1);
				}
				if ($linecounter++ > 20) {
					break;
				}
			}
			fclose($fp);
		}
	}
	return $size;
}
Then it's just a matter of calculating the filesize divided by the bitrate (noting that bitrate is in bits-per-second so divide by 8 to get bytes per second) for the total number of seconds. You can use the PlaytimeString function from getid3.lib.php to format it in a more normal hh:mm:ss style if you like:

Code: Select all

$URL = 'https://mindfulnessbergamo.podbean.com/mf/play/kqifhe/episodio_044.mp3';
$bitrate = 128000;
if ($filesize = filesize_remote($URL)) {
	$playtime_seconds = $filesize / ($bitrate / 8);
	echo 'Playtime is '.$playtime_seconds.'s<br>';
// or, for nicer formatting:
	require_once('/path/to/getid3/getid3.lib.php');
	echo 'Playtime is '.getid3_lib::PlaytimeString($playtime_seconds).'<br>';
}
If, on the other hand, you do have access to cache the remote lookup in a database on your server and only query the remote file once the first time it's seen, let me know and I can help you with code to actually use getID3 to analyze the file in detail.

gilibaus
User
Posts: 2
Joined: Thu Feb 08, 2018 3:45 pm
Are you a spambot?: no

Re: Get mp3 playtime and write it on webpage

Post by gilibaus » Fri Feb 09, 2018 8:33 am

James, thank you for your detailed reply. Soon after posting I realized that MP3 files not being on the same server as my website was the problem. I have now momentarily solved the playtime issue adding it manually to each episode as I have to do this anyway for the iTunes feed to be created. In the future though I will need to transfer all mp3s locally and your suggestions will come handy. Thanks again and keep up the awesome work.

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

Re: Get mp3 playtime and write it on webpage

Post by James Heinrich » Fri Feb 09, 2018 1:06 pm

If you have the files on the same server then it's pretty simple to loop through them and extract the information you need. The included demos/demo.simple.php basically shows how to loop through a directory and display information about each file.

Post Reply