1
0
Fork 0
oldhaven/php-bak/core/classes/VkontakteMP3.class.php

119 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*****************************************************************
Пример использования:
$files = VkontakteMP3::check($files);
*****************************************************************/
/**
* Класс для постобработки массива треков от контакта
* Проверяет файлы, рассчитывает битрейт и релевантность
*
* @package classes
* @author chez
**/
class VkontakteMP3 {
private static $_bitrates = array( // Массив стандартных битрейтов
8, 16, 24, 32, 40, 48, 56,
64, 80, 96, 112, 128, 144,
160, 192, 224, 256, 320
);
/**
* Рассчитывает средний битрейт файла
* Приводит полученый битрейт к одному из стандартных
*
* @param int $size Размер файла в байтах
* @param int $length Длина трека в секундах
* @return int Битрейт в килобитах в секунду
* @author chez
**/
public static function calculateBitrate($size, $length) {
$br = round($size * 8 / 1024 / $length);
$found_br = 0;
for ($i = 1; $i < count(self::$_bitrates); $i++) {
if (self::$_bitrates[$i] > $br && ! $found_br) {
$delta = self::$_bitrates[$i] - $br;
$br_delta = self::$_bitrates[$i] - self::$_bitrates[$i-1];
if (round($br_delta / 3) > $delta) {
$found_br = self::$_bitrates[$i];
} else {
$found_br = self::$_bitrates[$i-1];
}
} elseif ($i == count(self::$_bitrates) - 1 && self::$_bitrates[$i] < $br && ! $found_br) {
$delta = $br - self::$_bitrates[$i];
if ($delta < 32) {
$found_br = self::$_bitrates[$i];
} else {
$found_br = $br;
}
}
}
return $found_br;
}
/**
* Проверяет файлы, рассчитывает битрейт и релевантность
*
* @param array $files Массив файлов для обработки
* @return array Обработанный массив файлов
* @author chez
**/
public static function check($files, $artist_name, $track_name, $track_length) {
$urls = array();
foreach($files as $i => $file) {
$urls[$i] = $file['url'];
}
$headers = RemoteFile::getHeadersMT($urls);
foreach ($files as $i => $file) {
if (!isset($headers[$i]['http']) || $headers[$i]['http'] != 200) {
unset($files[$i]);
} else {
$files[$i]['size'] = trim($headers[$i]['content_length']);
}
}
$m = new TrackWeight();
$m->setTrackData($artist_name, $track_name, $track_length);
$m->setFiles($files);
$m->calculateWeight();
$files = $m->getFiles();
uasort($files, function($a, $b){
return $a['weight'] < $b['weight'];
});
foreach ($files as $i => $file) {
$files[$i]['bitrate'] = self::calculateBitrate($file['size'], $files[$i]['duration']);
}
return $files;
}
/**
* Чистит строку для максимально точного сравнения
*
* @param string $str Необработанная строка
* @return string Чистая строка
* @author chez
**/
public static function prepareQuery($str) {
$str = trim($str);
while(strpos($str, ' ') !== false) {
$str = str_replace(' ', ' ', $str);
}
return $str;
}
/**
* Преобразует длину из формата мм:сс в ссс
*
* @param string $duration Читабельная длина
* @return int Длина в секундах
* @author chez
**/
public static function convertDuration($duration) {
$tmp = explode(':', $duration);
return ($tmp[0] * 60 + $tmp[1]);
}
}