You are here

function getid3_file_presave in getID3() 7.2

Implements hook_file_presave().

File

./getid3.module, line 147

Code

function getid3_file_presave($file) {

  // Make sure we do not try to extract ID3 data from non valid types.
  if (!in_array($file->type, array(
    'video',
    'audio',
  ))) {
    return null;
  }

  // Load the ID3 data.
  $id3_data = getid3_analyze_file($file);

  // If we have data, attach it to metadata.
  if ($id3_data) {

    // Set the duration
    if (isset($id3_data['playtime_string'])) {
      $file->metadata['duration'] = $id3_data['playtime_string'];
    }

    // Update video specific information.
    if (isset($id3_data['video'])) {
      $file->metadata['width'] = $id3_data['video']['resolution_x'];
      $file->metadata['height'] = $id3_data['video']['resolution_y'];
    }

    // Update audio specific information.
    if (isset($id3_data['audio'])) {
      $file->metadata['audio_format'] = $id3_data['audio']['dataformat'];
      $file->metadata['audio_sample_rate'] = $id3_data['audio']['sample_rate'];
      $file->metadata['audio_channel_mode'] = $id3_data['audio']['channelmode'];
      $file->metadata['audio_bitrate'] = isset($id3_data['audio']['bitrate']) ? $id3_data['audio']['bitrate'] : NULL;
      $file->metadata['audio_bitrate_mode'] = isset($id3_data['audio']['bitrate_mode']) ? $id3_data['audio']['bitrate_mode'] : NULL;
    }
  }

  // @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($id3_data['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 ($id3_data['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->metadata['tags'][$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
        }
      }
    }
  }
}