demo.audioinfo.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.audioinfo.class.php - part of getID3() //
  9. // ///
  10. /////////////////////////////////////////////////////////////////
  11. // +----------------------------------------------------------------------+
  12. // | PHP version 4.1.0 |
  13. // +----------------------------------------------------------------------+
  14. // | Placed in public domain by Allan Hansen, 2002. Share and enjoy! |
  15. // +----------------------------------------------------------------------+
  16. // | /demo/demo.audioinfo.class.php |
  17. // | |
  18. // | Example wrapper class to extract information from audio files |
  19. // | through getID3(). |
  20. // | |
  21. // | getID3() returns a lot of information. Much of this information is |
  22. // | not needed for the end-application. It is also possible that some |
  23. // | users want to extract specific info. Modifying getID3() files is a |
  24. // | bad idea, as modifications needs to be done to future versions of |
  25. // | getID3(). |
  26. // | |
  27. // | Modify this wrapper class instead. This example extracts certain |
  28. // | fields only and adds a new root value - encoder_options if possible. |
  29. // | It also checks for mp3 files with wave headers. |
  30. // +----------------------------------------------------------------------+
  31. // | Example code: |
  32. // | $au = new AudioInfo(); |
  33. // | print_r($au->Info('file.flac'); |
  34. // +----------------------------------------------------------------------+
  35. // | Authors: Allan Hansen <ahØartemis*dk> |
  36. // +----------------------------------------------------------------------+
  37. /**
  38. * getID3() settings
  39. */
  40. require_once('../getid3/getid3.php');
  41. /**
  42. * Class for extracting information from audio files with getID3().
  43. */
  44. class AudioInfo {
  45. /**
  46. * Private variables
  47. */
  48. private $result;
  49. private $info;
  50. private $getID3;
  51. /**
  52. * Constructor
  53. */
  54. function __construct() {
  55. // Initialize getID3 engine
  56. $this->getID3 = new getID3;
  57. $this->getID3->option_md5_data = true;
  58. $this->getID3->option_md5_data_source = true;
  59. $this->getID3->encoding = 'UTF-8';
  60. }
  61. /**
  62. * Extract information - only public function
  63. *
  64. * @param string $file Audio file to extract info from.
  65. *
  66. * @return array
  67. */
  68. public function Info($file) {
  69. // Analyze file
  70. $this->info = $this->getID3->analyze($file);
  71. // Exit here on error
  72. if (isset($this->info['error'])) {
  73. return array ('error' => $this->info['error']);
  74. }
  75. // Init wrapper object
  76. $this->result = array();
  77. $this->result['format_name'] = (isset($this->info['fileformat']) ? $this->info['fileformat'] : '').'/'.(isset($this->info['audio']['dataformat']) ? $this->info['audio']['dataformat'] : '').(isset($this->info['video']['dataformat']) ? '/'.$this->info['video']['dataformat'] : '');
  78. $this->result['encoder_version'] = (isset($this->info['audio']['encoder']) ? $this->info['audio']['encoder'] : '');
  79. $this->result['encoder_options'] = (isset($this->info['audio']['encoder_options']) ? $this->info['audio']['encoder_options'] : '');
  80. $this->result['bitrate_mode'] = (isset($this->info['audio']['bitrate_mode']) ? $this->info['audio']['bitrate_mode'] : '');
  81. $this->result['channels'] = (isset($this->info['audio']['channels']) ? $this->info['audio']['channels'] : '');
  82. $this->result['sample_rate'] = (isset($this->info['audio']['sample_rate']) ? $this->info['audio']['sample_rate'] : '');
  83. $this->result['bits_per_sample'] = (isset($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : '');
  84. $this->result['playing_time'] = (isset($this->info['playtime_seconds']) ? $this->info['playtime_seconds'] : '');
  85. $this->result['avg_bit_rate'] = (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : '');
  86. $this->result['tags'] = (isset($this->info['tags']) ? $this->info['tags'] : '');
  87. $this->result['comments'] = (isset($this->info['comments']) ? $this->info['comments'] : '');
  88. $this->result['warning'] = (isset($this->info['warning']) ? $this->info['warning'] : '');
  89. $this->result['md5'] = (isset($this->info['md5_data']) ? $this->info['md5_data'] : '');
  90. // Post getID3() data handling based on file format
  91. $method = (isset($this->info['fileformat']) ? $this->info['fileformat'] : '').'Info';
  92. if ($method && method_exists($this, $method)) {
  93. $this->$method();
  94. }
  95. return $this->result;
  96. }
  97. /**
  98. * post-getID3() data handling for AAC files.
  99. *
  100. */
  101. private function aacInfo() {
  102. $this->result['format_name'] = 'AAC';
  103. }
  104. /**
  105. * post-getID3() data handling for Wave files.
  106. *
  107. */
  108. private function riffInfo() {
  109. if ($this->info['audio']['dataformat'] == 'wav') {
  110. $this->result['format_name'] = 'Wave';
  111. } elseif (preg_match('#^mp[1-3]$#', $this->info['audio']['dataformat'])) {
  112. $this->result['format_name'] = strtoupper($this->info['audio']['dataformat']);
  113. } else {
  114. $this->result['format_name'] = 'riff/'.$this->info['audio']['dataformat'];
  115. }
  116. }
  117. /**
  118. * * post-getID3() data handling for FLAC files.
  119. *
  120. */
  121. private function flacInfo() {
  122. $this->result['format_name'] = 'FLAC';
  123. }
  124. /**
  125. * post-getID3() data handling for Monkey's Audio files.
  126. *
  127. */
  128. private function macInfo() {
  129. $this->result['format_name'] = 'Monkey\'s Audio';
  130. }
  131. /**
  132. * post-getID3() data handling for Lossless Audio files.
  133. */
  134. private function laInfo() {
  135. $this->result['format_name'] = 'La';
  136. }
  137. /**
  138. * post-getID3() data handling for Ogg Vorbis files.
  139. *
  140. * @access private
  141. */
  142. function oggInfo() {
  143. if ($this->info['audio']['dataformat'] == 'vorbis') {
  144. $this->result['format_name'] = 'Ogg Vorbis';
  145. } else if ($this->info['audio']['dataformat'] == 'flac') {
  146. $this->result['format_name'] = 'Ogg FLAC';
  147. } else if ($this->info['audio']['dataformat'] == 'speex') {
  148. $this->result['format_name'] = 'Ogg Speex';
  149. } else {
  150. $this->result['format_name'] = 'Ogg '.$this->info['audio']['dataformat'];
  151. }
  152. }
  153. /**
  154. * post-getID3() data handling for Musepack files.
  155. *
  156. * @access private
  157. */
  158. function mpcInfo() {
  159. $this->result['format_name'] = 'Musepack';
  160. }
  161. /**
  162. * post-getID3() data handling for MPEG files.
  163. *
  164. * @access private
  165. */
  166. function mp3Info() {
  167. $this->result['format_name'] = 'MP3';
  168. }
  169. /**
  170. * post-getID3() data handling for MPEG files.
  171. *
  172. * @access private
  173. */
  174. function mp2Info() {
  175. $this->result['format_name'] = 'MP2';
  176. }
  177. /**
  178. * post-getID3() data handling for MPEG files.
  179. *
  180. * @access private
  181. */
  182. function mp1Info() {
  183. $this->result['format_name'] = 'MP1';
  184. }
  185. /**
  186. * post-getID3() data handling for WMA files.
  187. *
  188. * @access private
  189. */
  190. function asfInfo() {
  191. $this->result['format_name'] = strtoupper($this->info['audio']['dataformat']);
  192. }
  193. /**
  194. * post-getID3() data handling for Real files.
  195. *
  196. * @access private
  197. */
  198. function realInfo() {
  199. $this->result['format_name'] = 'Real';
  200. }
  201. /**
  202. * post-getID3() data handling for VQF files.
  203. *
  204. * @access private
  205. */
  206. function vqfInfo() {
  207. $this->result['format_name'] = 'VQF';
  208. }
  209. }