PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php-getid3-2.0.0b5/demos/demo.audioinfo.class.php

#
PHP | 189 lines | 81 code | 58 blank | 50 comment | 19 complexity | 4cace29b4cc82e83c8c315c56cb69924 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Placed in public domain by Allan Hansen, 2002. Share and enjoy! |
  6. // +----------------------------------------------------------------------+
  7. // | /demo/demo.audioinfo.class.php |
  8. // | |
  9. // | Example wrapper class to extract information from audio files |
  10. // | through getID3(). |
  11. // | |
  12. // | getID3() returns a lot of information. Much of this information is |
  13. // | not needed for the end-application. It is also possible that some |
  14. // | users want to extract specific info. Modifying getID3() files is a |
  15. // | bad idea, as modifications needs to be done to future versions of |
  16. // | getID3(). |
  17. // | |
  18. // | Modify this wrapper class instead. This example extracts certain |
  19. // | fields only and adds a new root value - encoder_options if possible. |
  20. // | It also checks for mp3 files with wave headers. |
  21. // +----------------------------------------------------------------------+
  22. // | Example code: |
  23. // | require_once('getid3.php'); |
  24. // | require_once('demo.audioinfo.class.php'); |
  25. // | $au = new AudioInfo(); |
  26. // | print_r($au->Info('file.flac'); |
  27. // +----------------------------------------------------------------------+
  28. // | Authors: Allan Hansen <ah?&#x2DC;artemis*dk> |
  29. // +----------------------------------------------------------------------+
  30. //
  31. // $Id: demo.audioinfo.class.php,v 1.1.1.1 2004/08/23 00:01:26 ah Exp $
  32. /**
  33. * Class for extracting information from audio files with getID3().
  34. */
  35. final class AudioInfo
  36. {
  37. // Public variables
  38. public $error;
  39. public $info; // can easily be made private - public for debugging only
  40. // Private variables
  41. private $result;
  42. private $getid3;
  43. // Constructor
  44. public function __construct($md5_data = false, $sha1_data = false) {
  45. // Initialize getID3 engine
  46. $this->getid3 = new getID3;
  47. $this->getid3->option_md5_data = $md5_data;
  48. $this->getid3->option_md5_data_source = true;
  49. $this->getid3->option_sha1_data = $sha1_data;
  50. $this->getid3->encoding = 'UTF-8';
  51. }
  52. // Extract information -
  53. public function Info($file) {
  54. // Reset
  55. $this->error = null;
  56. // Analyze file
  57. try {
  58. $this->info = $this->getid3->analyze($file);
  59. }
  60. catch (Exception $e) {
  61. $this->error = $e->message;
  62. return;
  63. }
  64. // Ignore non-audio files
  65. if (!@$this->info['audio']) {
  66. return;
  67. }
  68. // Init wrapper object
  69. $this->result = new stdClass;
  70. $this->result->format_name = @$this->info['fileformat'].(@$this->info['audio']['dataformat'] && $this->info['audio']['dataformat'] != @$this->info['fileformat'] ? '/'.@$this->info['audio']['dataformat'] : '');
  71. $this->result->encoder_version = @$this->info['audio']['encoder'];
  72. $this->result->encoder_options = @$this->info['audio']['encoder_options'];
  73. $this->result->bitrate_mode = @$this->info['audio']['bitrate_mode'];
  74. $this->result->channels = @$this->info['audio']['channels'];
  75. $this->result->sample_rate = @$this->info['audio']['sample_rate'];
  76. $this->result->bits_per_sample = @$this->info['audio']['bits_per_sample'];
  77. $this->result->playing_time = @$this->info['playtime_seconds'];
  78. $this->result->avg_bit_rate = @$this->info['audio']['bitrate'];
  79. $this->result->tags = @$this->info['tags'];
  80. $this->result->comments = @$this->info['comments'];
  81. $this->result->warning = @$this->info['warning'];
  82. $this->result->md5 = @$this->info['md5_data_source'] ? $this->info['md5_data_source'] : @$this->info['md5_data'];
  83. // Post getID3() data handling based on file format
  84. $method = @$this->info['fileformat'].'Info';
  85. if (@$this->info['fileformat'] && method_exists($this, $method)) {
  86. $this->$method();
  87. }
  88. // No post function defined, make format name upper case
  89. else {
  90. $this->result->format_name = strtoupper($this->result->format_name);
  91. }
  92. return $this->result;
  93. }
  94. // post-getID3() data handling for Wave files.
  95. private function riffInfo() {
  96. if ($this->info['audio']['dataformat'] == 'wav') {
  97. $this->result->format_name = 'Wave';
  98. } else if (!ereg('^mp[1-3]$', $this->info['audio']['dataformat'])) {
  99. $this->result->format_name = 'riff/'.$this->info['audio']['dataformat'];
  100. }
  101. }
  102. // post-getID3() data handling for Monkey's Audio files.
  103. private function macInfo() {
  104. $this->result->format_name = 'Monkey\'s Audio';
  105. }
  106. // post-getID3() data handling for Lossless Audio files.
  107. private function laInfo() {
  108. $this->result->format_name = 'La';
  109. }
  110. // post-getID3() data handling for Ogg Vorbis files.
  111. private function oggInfo() {
  112. if ($this->info['audio']['dataformat'] == 'vorbis') {
  113. $this->result->format_name = 'Ogg Vorbis';
  114. } else if ($this->info['audio']['dataformat'] == 'flac') {
  115. $this->result->format_name = 'Ogg FLAC';
  116. } else if ($this->info['audio']['dataformat'] == 'speex') {
  117. $this->result->format_name = 'Ogg Speex';
  118. } else {
  119. $this->result->format_name = 'Ogg '.$this->info['audio']['dataformat'];
  120. }
  121. }
  122. // post-getID3() data handling for Musepack files.
  123. private function mpcInfo() {
  124. $this->result->format_name = 'Musepack';
  125. }
  126. // post-getID3() data handling for Real files.
  127. private function realInfo() {
  128. $this->result->format_name = 'Real';
  129. }
  130. }
  131. ?>