Dysan Posted November 16, 2007 Share Posted November 16, 2007 Can PHP generate a preview (Shorter Version) of an MP3 file? Quote Link to comment Share on other sites More sharing options...
dingus Posted November 16, 2007 Share Posted November 16, 2007 im assumeing you desire to play the first 1-30 second.... this code should do what you need <? header("Content-type: audio/mpeg"); $f = fopen($mp3_path, "rb"); echo fread($f, 245760); fclose($f); ?> that will play 15 seconds of audio and just so you can edit it the 15 seconds is based on 128kbits sound i arived at 245760 through (128kbits * 1024 / * 15 = 245760 the formula you need to work out the seconds you need in php code would be $seconds_to_play = ($kbits *1024 / 8 ) * number of seconds you wish to play hope that helps Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 Perfect works really well. Can the beginning at end of the song be clipped, so it plays a 1 minute of the middle of the song? Quote Link to comment Share on other sites More sharing options...
Azu Posted November 16, 2007 Share Posted November 16, 2007 echo substr($MP3,(($kbits *1024 / 8 ) * number of seconds you wish to skip)); Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 The following doesn't work, it still plays 15 seconds from the beginning <? header("Content-type: audio/mp3"); $f = fopen("upload/1.mp3", "rb"); echo substr($MP3,((128 *1024 / 8 ) * 36)); echo fread($f, 245760); fclose($f); ?> How do I make a script, where the start and end second can be entered into a form, and this will automatically shorten the mp3 file? What happens if I want to play a preview of a 597kbps or 96kbps mp3 file? Quote Link to comment Share on other sites More sharing options...
dingus Posted November 16, 2007 Share Posted November 16, 2007 ok well you cant quote me directly on this one but i think this SHOULD do what you are asking <?php header("Content-type: audio/mpeg"); $f = fopen($mp3_path, "rb"); #open the file $trash = fread($f, 983040); # read the first minuet in to a guarbage veriable echo fread($f, 245760); # echo the 15 seconds we wish to play $trash = null ; # dump the guarbage veriable (save ram) fclose($f); # close the file ?> basicly that is still reading the file from the start but we are dumping the first minuet of it in to trash and dumping trash latter then we are reading the next 15 seconds and echoing it to the user i arived at 983040 the with the same formula i used to arive at the 15 second before now your question about playing the end of the file.... this is a little more complicated <?php $mp3_path = "path to mp3"; $total = filesize($mp3_path); $seconds_to_play = 245760 ; $seconds_to_drop = $total - $seconds_to_play ; $f = fopen($mp3_path, "rb"); #open the file $trash = fread($f, $seconds_to_drop); # read the first minuet in to a guarbage veriable echo fread($f, $seconds_to_play); # echo the 15 seconds we wish to play $trash = null ; # dump the guarbage veriable (save ram) fclose($f); # close the file ?> now this is probably not the most eligent way of doing it but i hope it works to reprint the formula with out the emote here it is (128kbits * 1024 / 8 ) * 15 = 245760 im interested to know if this works because i dont quite understand the structure of mp3 files and am writeing off the top of my head directly in text box so is untested (also because is untested i may have droped a ; somewhere if you get an error look for basic stuff first Quote Link to comment Share on other sites More sharing options...
dingus Posted November 16, 2007 Share Posted November 16, 2007 as for working out the bitrate if you took user input for how many seconds in to it and how many they want to play it is simple with the code i have given you there is a class here http://www.phpclasses.org/browse/file/588.html that you could use to get the bitrate and calcuate how many seconds to skip over on the fly let me know if im lossing you here Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 For some strange reason, the file generated from the fisrt block of code, can't be read by the mp3 player (Windows Media Player), and the second block of code, returns the following error messages. WHY IS THIS? - I couldn't find any lines without semi-colons, they all have one as far as I can see. Warning: filesize() [function.filesize]: stat failed for upload/PhilCollins-AnotherDayInParadise in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 4 Warning: fopen(upload/PhilCollins-AnotherDayInParadise) [function.fopen]: failed to open stream: No such file or directory in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 8 Warning: fread(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 9 Warning: fread(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 10 Warning: fclose(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 12 Quote Link to comment Share on other sites More sharing options...
dingus Posted November 16, 2007 Share Posted November 16, 2007 ok that error could be potentaly problematic (unable to be read by media player) but the following errors can i see what you set this line to $mp3_path = "path to mp3"; Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 Sure. $mp3_path = "Upload/Caesars Palace - Jerk It Out.mp3"; Quote Link to comment Share on other sites More sharing options...
dingus Posted November 16, 2007 Share Posted November 16, 2007 try putting a ./ or maybe just a / beofer the file name also i think i know why it wont play..... its got to do with the header information and i THINK i can fix it now my basic understanding is that a mp3 header is 32 bits long so if we take that first in code like this <?php header("Content-type: audio/mpeg"); $f = fopen($mp3_path, "rb"); #open the file $header = $trash = fread($f, 32); $trash = fread($f, 983040); # read the first minuet in to a guarbage veriable $data = fread($f, 245760); # echo the 15 seconds we wish to play $trash = null ; # dump the guarbage veriable (save ram) fclose($f); # close the file echo $header ; echo $data ; ?> i hope that will fix it im signing off now so you will get no more replys from me till tomrrow (its 12:45am here) but if it is still un answered tomrrow i will take another lash good luck.... and the errors are definatly being called because it cant find the file Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 16, 2007 Share Posted November 16, 2007 Yes, make sure your path is right. Use an absolute path if necessary to the mp3 file. Example: "d:/uploads/whatever/whatever.mp3" You may need to use a special audio class to avoid clipping the file in the middle of an MP3 frame. This may cause the audio player problems with the beginning of the file. The best thing you can do is test your script in different players. You may also need to set the PHP execution timeout value in your script: set_time_limit(0); // Infinite value @dingus, you must live in Australia then, right? Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 I just tested it, and for some strange reason, it still doesn't work? Why is this? Just to let you know, the following code worked, with the filename I post earlier: "Upload/Caesars Palace - Jerk It Out.mp3" <? header("Content-type: audio/mp3"); $f = fopen("Upload/Caesars Palace - Jerk It Out.mp3", "rb"); echo fread($f, 245760); fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 16, 2007 Share Posted November 16, 2007 For some strange reason, the file generated from the fisrt block of code, can't be read by the mp3 player (Windows Media Player), and the second block of code, returns the following error messages. WHY IS THIS? - I couldn't find any lines without semi-colons, they all have one as far as I can see. Warning: filesize() [function.filesize]: stat failed for upload/PhilCollins-AnotherDayInParadise in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 4 Warning: fopen(upload/PhilCollins-AnotherDayInParadise) [function.fopen]: failed to open stream: No such file or directory in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 8 Warning: fread(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 9 Warning: fread(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 10 Warning: fclose(): supplied argument is not a valid stream resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\Copy of Preview.php on line 12 Maybe you forgot the mp3 extension on the filename??? upload/PhilCollins-AnotherDayInParadise Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 16, 2007 Author Share Posted November 16, 2007 Can PHP retrieve the bitrate of a MP3 file? Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 16, 2007 Share Posted November 16, 2007 http://www.getid3.org/ will retrieve all the ID3 information. Put this script in the root folder of the extracted download. <PRE> <?php $filename = "/path_to_mp3_file.mp3"; require_once('getid3\getid3.php'); $getID3 = new getID3; $fileinfo = $getID3->analyze($filename); echo "Bitrate: {$fileinfo[bitrate]} <HR>"; print_r($fileinfo); ?> </PRE> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.