View source
<?php
define('GETID3_RECOMMEND_VERSION', '1.8.2');
function getid3_help($section, $arg) {
switch ($section) {
case 'admin/config/media/getid3':
$help = '<p>' . t("To use this module you'll need to <a href='!download-link'>download the library</a> from the <a href='!info-link'>getID3 website</a> and extract the contents into the site's libraries directory. Currently, the recommended version of the getID3 library is %recommended-version.", array(
'!download-link' => url('http://prdownloads.sourceforge.net/getid3'),
'!info-link' => url('http://getid3.org/'),
'%recommended-version' => GETID3_RECOMMEND_VERSION,
)) . '</p>';
return $help;
}
}
function getid3_menu() {
$items['admin/config/media/getid3'] = array(
'title' => 'getID3()',
'description' => 'Configure settings associated with getID3().',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'getid3_admin_settings_form',
NULL,
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'getid3.admin.inc',
);
return $items;
}
function getid3_theme($existing, $type, $theme, $path) {
return array(
'getid3_duration' => array(
'variables' => array(
'duration' => NULL,
),
),
'getid3_sample_rate' => array(
'variables' => array(
'sample_rate' => NULL,
),
),
'getid3_bitrate' => array(
'variables' => array(
'bitrate' => NULL,
),
),
);
}
function getid3_libraries_info() {
$libraries['getid3'] = array(
'name' => 'getID3',
'vendor url' => 'http://www.getid3.org',
'download url' => 'http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.9/getID3-1.9.9-20141218.zip/download',
'path' => 'getid3',
'version' => '1.9.9',
'files' => array(
'php' => array(
'getid3.php',
),
),
);
return $libraries;
}
function getid3_load($display_warning = TRUE) {
$library = libraries_detect('getid3');
if ($library['installed']) {
libraries_load('getid3');
return TRUE;
}
return FALSE;
}
function getid3_instance() {
$id3 = NULL;
if (getid3_load()) {
$id3 = new getID3();
$id3->option_md5_data = FALSE;
$id3->option_md5_data_source = FALSE;
$id3->encoding = 'UTF-8';
}
return $id3;
}
function getid3_calc($word) {
$m = 1;
$int = 0;
for ($i = strlen($word) - 1; $i > -1; $i--) {
$int += $m * ord($word[$i]);
$m = $m * 128;
}
return $int;
}
function getid3_analyze_file($file) {
$getID3 = getid3_instance();
$file_realpath = drupal_realpath($file->uri);
if (file_exists($file_realpath)) {
return $getID3
->analyze($file_realpath);
}
if ($file_realpath === FALSE) {
$file_urlpath = file_create_url($file->uri);
$tmp = file_directory_temp() . '/' . $file->filename;
if (file_put_contents($tmp, file_get_contents($file_urlpath, false, null, 0, 10))) {
$tagLen = getid3_calc(substr($tmp, 6, 4));
unlink($tmp);
file_put_contents($tmp, file_get_contents($file_urlpath, false, null, 0, $tagLen));
return $getID3
->analyze($tmp);
}
}
return NULL;
}
function getid3_file_presave($file) {
if (!in_array($file->type, array(
'video',
'audio',
))) {
return null;
}
$id3_data = getid3_analyze_file($file);
if ($id3_data) {
if (isset($id3_data['playtime_string'])) {
$file->metadata['duration'] = $id3_data['playtime_string'];
}
if (isset($id3_data['video'])) {
$file->metadata['width'] = $id3_data['video']['resolution_x'];
$file->metadata['height'] = $id3_data['video']['resolution_y'];
}
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;
}
}
if (isset($id3_data['tags_html'])) {
foreach ($id3_data['tags_html'] as $type => $values) {
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');
}
}
}
}
}
function getid3_perform_batch_process() {
$batch = array(
'title' => t('Updating existing File Entities with ID3 data'),
'init_message' => t('Preparing to update entities..'),
'progress_message' => t('Processed @current out of @total items.'),
'operations' => array(
array(
'getid3_analyze_existing_files',
array(),
),
),
'finished' => 'getid3_perform_batch_process_complete',
);
batch_set($batch);
}
function getid3_perform_batch_process_complete($success, $results, $operations) {
if ($success) {
drupal_set_message(t('File entity ID3 data updated @count files successfully.', array(
'@count' => count($results),
)));
}
else {
drupal_set_message(theme('item_list', array(
'items' => $results['errors'],
)), 'error');
}
}
function getid3_analyze_existing_files(&$context) {
if (isset($context['sandbox'])) {
$sandbox =& $context['sandbox'];
}
else {
$sandbox =& $context;
}
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_fid'] = 0;
$sandbox['max'] = db_query('SELECT COUNT(fid) FROM {file_managed} f WHERE f.filemime LIKE :video OR f.filemime LIKE :audio', array(
':video' => 'video%',
':audio' => 'audio%',
))
->fetchField();
}
$query = db_select('file_managed', 'f')
->fields('f', array(
'fid',
))
->condition('fid', $sandbox['current_fid'], '>')
->range(0, 10)
->orderBy('fid', 'ASC');
$db_or = db_or();
$db_or
->condition('f.filemime', db_like('video') . '%', 'LIKE');
$db_or
->condition('f.filemime', db_like('audio') . '%', 'LIKE');
$query
->condition($db_or);
$files = $query
->execute()
->fetchAll();
foreach ($files as $file) {
$file = file_load($file->fid);
file_save($file);
$sandbox['progress']++;
$context['results'][] = $file->fid;
$sandbox['current_fid'] = $file->fid;
}
$context['message'] = t('Parsing @current out of @max files', array(
'@current' => count($context['results']),
'@max' => $sandbox['max'],
));
$context['finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
$sandbox['#finished'] = $context['finished'];
}
function getid3_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'getid3') . '/includes',
);
}
function theme_getid3_duration($variables) {
$duration = $variables['duration'];
$seconds = round(($duration / 60 - floor($duration / 60)) * 60);
$minutes = floor($duration / 60);
if ($seconds >= 60) {
$seconds -= 60;
$minutes++;
}
return (int) $minutes . ':' . str_pad($seconds, 2, 0, STR_PAD_LEFT);
}
function theme_getid3_sample_rate($variables) {
return t('@sampleratekHz', array(
'@samplerate' => (int) ($variables['sample_rate'] / 1000),
));
}
function theme_getid3_bitrate($variables) {
return t('@bitrateKbps', array(
'@bitrate' => (int) ($variables['bitrate'] / 1000),
));
}