demo.joinmp3.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // //
  8. // /demo/demo.joinmp3.php - part of getID3() //
  9. // Sample script for splicing two or more MP3s together into //
  10. // one file. Does not attempt to fix VBR header frames. //
  11. // Can also be used to extract portion from single file. //
  12. // Note: all joined MP3s need to be the same sample rate. //
  13. // see readme.txt for more details //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. die('For security reasons, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in demos/'.basename(__FILE__));
  17. // sample usage:
  18. // $FilenameOut = 'combined.mp3';
  19. // $FilenamesIn[] = 'first.mp3'; // filename with no start/length parameters
  20. // $FilenamesIn[] = array('second.mp3', 0, 0); // filename with zero for start/length is the same as not specified (start = beginning, length = full duration)
  21. // $FilenamesIn[] = array('third.mp3', 0, 10); // extract first 10 seconds of audio
  22. // $FilenamesIn[] = array('fourth.mp3', -10, 0); // extract last 10 seconds of audio
  23. // $FilenamesIn[] = array('fifth.mp3', 10, 0); // extract everything except first 10 seconds of audio
  24. // $FilenamesIn[] = array('sixth.mp3', 0, -10); // extract everything except last 10 seconds of audio
  25. // if (CombineMultipleMP3sTo($FilenameOut, $FilenamesIn)) {
  26. // echo 'Successfully copied '.implode(' + ', $FilenamesIn).' to '.$FilenameOut;
  27. // } else {
  28. // echo 'Failed to copy '.implode(' + ', $FilenamesIn).' to '.$FilenameOut;
  29. // }
  30. //
  31. // Could also be called like this to extract portion from single file:
  32. // CombineMultipleMP3sTo('sample.mp3', array(array('input.mp3', 0, 30))); // extract first 30 seconds of audio
  33. function CombineMultipleMP3sTo($FilenameOut, $FilenamesIn) {
  34. foreach ($FilenamesIn as $nextinputfilename) {
  35. if (is_array($nextinputfilename)) {
  36. $nextinputfilename = $nextinputfilename[0];
  37. }
  38. if (!is_readable($nextinputfilename)) {
  39. echo 'Cannot read "'.$nextinputfilename.'"<BR>';
  40. return false;
  41. }
  42. }
  43. if ((file_exists($FilenameOut) && !is_writeable($FilenameOut)) || (!file_exists($FilenameOut) && !is_writeable(dirname($FilenameOut)))) {
  44. echo 'Cannot write "'.$FilenameOut.'"<BR>';
  45. return false;
  46. }
  47. require_once(dirname(__FILE__).'/../getid3/getid3.php');
  48. ob_start();
  49. if ($fp_output = fopen($FilenameOut, 'wb')) {
  50. ob_end_clean();
  51. // Initialize getID3 engine
  52. $getID3 = new getID3;
  53. foreach ($FilenamesIn as $nextinputfilename) {
  54. $startoffset = 0;
  55. $length_seconds = 0;
  56. if (is_array($nextinputfilename)) {
  57. @list($nextinputfilename, $startoffset, $length_seconds) = $nextinputfilename;
  58. }
  59. $CurrentFileInfo = $getID3->analyze($nextinputfilename);
  60. if ($CurrentFileInfo['fileformat'] == 'mp3') {
  61. ob_start();
  62. if ($fp_source = fopen($nextinputfilename, 'rb')) {
  63. ob_end_clean();
  64. $CurrentOutputPosition = ftell($fp_output);
  65. // copy audio data from first file
  66. $start_offset_bytes = $CurrentFileInfo['avdataoffset'];
  67. if ($startoffset > 0) { // start X seconds from start of audio
  68. $start_offset_bytes = $CurrentFileInfo['avdataoffset'] + round(($CurrentFileInfo['bitrate'] / 8) * $startoffset);
  69. } elseif ($startoffset < 0) { // start X seconds from end of audio
  70. $start_offset_bytes = $CurrentFileInfo['avdataend'] + round(($CurrentFileInfo['bitrate'] / 8) * $startoffset);
  71. }
  72. $start_offset_bytes = max($CurrentFileInfo['avdataoffset'], min($CurrentFileInfo['avdataend'], $start_offset_bytes));
  73. $end_offset_bytes = $CurrentFileInfo['avdataend'];
  74. if ($length_seconds > 0) { // set end offset to X seconds from start of audio
  75. $end_offset_bytes = $start_offset_bytes + round(($CurrentFileInfo['bitrate'] / 8) * $length_seconds);
  76. } elseif ($length_seconds < 0) { // set end offset to X seconds from end of audio
  77. $end_offset_bytes = $CurrentFileInfo['avdataend'] + round(($CurrentFileInfo['bitrate'] / 8) * $length_seconds);
  78. }
  79. $end_offset_bytes = max($CurrentFileInfo['avdataoffset'], min($CurrentFileInfo['avdataend'], $end_offset_bytes));
  80. if ($end_offset_bytes <= $start_offset_bytes) {
  81. echo 'failed to copy '.$nextinputfilename.' from '.$startoffset.'-seconds start for '.$length_seconds.'-seconds length (not enough data)';
  82. fclose($fp_source);
  83. fclose($fp_output);
  84. return false;
  85. }
  86. fseek($fp_source, $start_offset_bytes, SEEK_SET);
  87. while (!feof($fp_source) && (ftell($fp_source) < $end_offset_bytes)) {
  88. fwrite($fp_output, fread($fp_source, min(32768, $end_offset_bytes - ftell($fp_source))));
  89. }
  90. fclose($fp_source);
  91. } else {
  92. $errormessage = ob_get_contents();
  93. ob_end_clean();
  94. echo 'failed to open '.$nextinputfilename.' for reading';
  95. fclose($fp_output);
  96. return false;
  97. }
  98. } else {
  99. echo $nextinputfilename.' is not MP3 format';
  100. fclose($fp_output);
  101. return false;
  102. }
  103. }
  104. } else {
  105. $errormessage = ob_get_contents();
  106. ob_end_clean();
  107. echo 'failed to open '.$FilenameOut.' for writing';
  108. return false;
  109. }
  110. fclose($fp_output);
  111. return true;
  112. }