0

is it possible to generate a 30 second preview of an mp3 using getid3? I'm not looking to create a copy of it and save it on the server just create the preview as the mp3 is requested.

I read this :

http://www.stephenwalcher.com/blog/2012/01/17/how-to-extract-and-play-part-of-an-mp3-in-php/

But I was unable to get it working.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
Belgin Fish
  • 19,187
  • 41
  • 102
  • 131

1 Answers1

2

If you want to avoid using ID3 tag libraries, you could modify it to do something like only play 10% of the song, which would use the following code:

$handle = fopen($filename, 'r');  
$content = fread($handle, filesize($filename));  

$length = strlen($content);  

if (!$session->IsLoggedIn()) {  
    $length = round(strlen($content) / 10); /* Change 10 to be 2 to play 50% of the song, or 5 to play 20% of the song. A value of 10 will play 10% of the song, a decent preview amount */  
    $content = substr($content, $length * .66 /* Start extraction 2/3 of that in; if it does turn out to be 30 seconds, it will start 20 seconds in.*/, $length);  
}  

header("Content-Type: {$id3_info['mime_type']}");  
header("Content-Length: {$length}");  
print $content; 
Alex Coleman
  • 7,216
  • 1
  • 22
  • 31