那里有一个很好的图书馆,将用PHP拆分一个mp3

So i have an application takes a uploaded song and allows the user to purchase it. I would like to also create a 30 second clip that the user can preview, but rather then having the user upload two files I want the application to create the 30 second mp3 file based on the original mp3. I found this library which i am using like this

    $oSplit=new CMP3Split(DIR_DOWNLOAD ."Made_To_Love.mp3",243,DIR_DOWNLOAD ."clips/{$rndString}.mp3",30,0);

but it seems to not always produce a 30 second clip

and i also found this question which is using ffmpeg which i dont know how to use in a php setting.

Any ideas or suggestions

Try out the ffmpeg solution from the command line, if you get it working just call it at the command line from within PHP. shell_exec() can do this. If you're allowing the user to specify a file name you will want to escape the shell arguments.

Since this will take a moment, it may be wise to look into passing this work off to a queue system like gearman.

That library is largely useless: it doesn't actually even read the MP3's bitrate to figure out how much of the file to output.

Take a look at this: http://www.sourcerally.net/Scripts/20-PHP-MP3-Class

That class should be able to extract with something like this:

  header('Content-type: audio/mp3');
  header('Content-disposition: attachment; filename="preview.mp3"');
  header('Cache-Control: no-cache');
  include_once('mp3.php');
  $path = '<filename to path>';
  $mp3 = new mp3($path);
  $mp3_1 = $mp3->extract(0,30);
  header('Content-length: ' . sizeof($mp3_1->str));
  echo $mp3_1->str;

You can expect it to be slow, and consume a lot of memory: you're probably better off using something like ffmpeg to do the chop.