You are here

function getid3_analyze_file in getID3() 7

Same name and namespace in other branches
  1. 7.2 getid3.module \getid3_analyze_file()

Analyze a file object and popupate its getid3 property.

2 calls to getid3_analyze_file()
getid3_file_insert in ./getid3.module
Implements hook_file_insert().
getid3_update_7102 in ./getid3.install
Add the meta data to files already created.

File

./getid3.module, line 114

Code

function getid3_analyze_file($file) {
  $info = getid3_analyze(drupal_realpath($file->uri));
  $file->getid3 = array(
    'width' => 0,
    'height' => 0,
    'duration' => 0,
    'audio_format' => '',
    'audio_sample_rate' => 0,
    'audio_channel_mode' => '',
    'audio_bitrate' => NULL,
    'audio_bitrate_mode' => NULL,
  );
  if (isset($info['video']['resolution_x'])) {
    $file->getid3['width'] = (int) $info['video']['resolution_x'];
    $file->getid3['height'] = (int) $info['video']['resolution_y'];
  }
  else {
    if (isset($info['video']['streams'])) {
      foreach ($info['video']['streams'] as $stream) {
        $file->getid3['width'] = max($file->getid3['width'], (int) $stream['resolution_x']);
        $file->getid3['height'] = max($file->getid3['height'], (int) $stream['resolution_y']);
      }
    }
  }
  if (isset($info['playtime_seconds'])) {
    $file->getid3['duration'] = (int) $info['playtime_seconds'];
  }
  if (isset($info['audio'])) {
    $file->getid3['audio_format'] = $info['audio']['dataformat'];

    //e.g. mp3
    $file->getid3['audio_sample_rate'] = $info['audio']['sample_rate'];

    //e.g. 44100
    $file->getid3['audio_channel_mode'] = $info['audio']['channelmode'];

    // e.g. mono
    $file->getid3['audio_bitrate'] = isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : NULL;

    //e.g. 64000
    $file->getid3['audio_bitrate_mode'] = isset($info['audio']['bitrate_mode']) ? $info['audio']['bitrate_mode'] : NULL;

    //e.g. cbr
  }

  // @TODO: Kill off the audio module by allowing users to edit id3 tags on
  // files. Explore a sub-module that allows tags to be mapped to CCK fields and
  // allows reading and writing of the tags.
  // Add in arbitrary ID3 tags.
  if (isset($info['tags_html'])) {

    // We use tags_html instead of tags because it is the most reliable data
    // source for pulling in non-UTF-8 characters according to getID3 docs.
    foreach ($info['tags_html'] as $type => $values) {

      // Typically $type may be IDv2 (for MP3s) or quicktime (for AAC).
      foreach ($values as $key => $value) {
        $value = isset($value[0]) ? (string) $value[0] : (string) $value;
        if (!empty($value) && $key != 'coverart') {
          $file->data['tags'][$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
        }
      }
    }
  }
}