2011-07-22 08:30:52 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - media plugin
|
|
|
|
*
|
|
|
|
* @author Robin Appelman
|
|
|
|
* @copyright 2010 Robin Appelman icewind1991@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('getID3/getid3/getid3.php');
|
|
|
|
|
|
|
|
//class for scanning directories for music
|
|
|
|
class OC_MEDIA_SCANNER{
|
|
|
|
static private $getID3=false;
|
|
|
|
|
|
|
|
//these are used to store which artists and albums we found, it can save a lot of addArtist/addAlbum calls
|
|
|
|
static private $artists=array();
|
|
|
|
static private $albums=array();//stored as "$artist/$album" to allow albums with the same name from different artists
|
2011-08-01 02:07:46 +04:00
|
|
|
static private $useMp3Info=null;
|
2011-07-22 08:30:52 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* scan a folder for music
|
|
|
|
* @param string $path
|
|
|
|
* @return int the number of songs found
|
|
|
|
*/
|
|
|
|
public static function scanFolder($path){
|
2011-07-29 23:36:03 +04:00
|
|
|
if (OC_Filesystem::is_dir($path)) {
|
2011-07-22 08:30:52 +04:00
|
|
|
$songs=0;
|
2011-07-29 23:36:03 +04:00
|
|
|
if ($dh = OC_Filesystem::opendir($path)) {
|
2011-07-22 08:30:52 +04:00
|
|
|
while (($filename = readdir($dh)) !== false) {
|
|
|
|
if($filename<>'.' and $filename<>'..' and substr($filename,0,1)!='.'){
|
|
|
|
$file=$path.'/'.$filename;
|
2011-07-29 23:36:03 +04:00
|
|
|
if(OC_Filesystem::is_dir($file)){
|
2011-07-22 08:30:52 +04:00
|
|
|
$songs+=self::scanFolder($file);
|
2011-07-29 23:36:03 +04:00
|
|
|
}elseif(OC_Filesystem::is_file($file)){
|
2011-08-01 02:07:46 +04:00
|
|
|
$data=self::scanFile($file);
|
|
|
|
if($data){
|
2011-07-22 08:30:52 +04:00
|
|
|
$songs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-29 23:36:03 +04:00
|
|
|
}elseif(OC_Filesystem::is_file($path)){
|
2011-07-22 08:30:52 +04:00
|
|
|
$songs=1;
|
|
|
|
self::scanFile($path);
|
|
|
|
}else{
|
|
|
|
$songs=0;
|
|
|
|
}
|
|
|
|
return $songs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scan a file for music
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function scanFile($path){
|
2011-08-01 02:07:46 +04:00
|
|
|
if(is_null(self::$useMp3Info)){
|
|
|
|
self::$useMp3Info=OC_Helper::canExecute("mp3info");
|
|
|
|
}
|
2011-07-29 23:36:03 +04:00
|
|
|
$file=OC_Filesystem::getLocalFile($path);
|
2011-08-01 02:07:46 +04:00
|
|
|
if(substr($path,-3)=='mp3' and self::$useMp3Info){//use the command line tool id3info if possible
|
2011-07-28 22:10:58 +04:00
|
|
|
$output=array();
|
|
|
|
$size=filesize($file);
|
2011-08-01 02:07:46 +04:00
|
|
|
exec('mp3info -p "%a\n%l\n%t\n%n\n%S" "'.$file.'"',$output);
|
|
|
|
if(count($output)>4){
|
|
|
|
$artist=$output[0];
|
|
|
|
$album=$output[1];
|
|
|
|
$title=$output[2];
|
|
|
|
$track=$output[3];
|
|
|
|
$length=$output[4];
|
|
|
|
}else{
|
|
|
|
return; //invalid mp3 file
|
2011-07-28 22:10:58 +04:00
|
|
|
}
|
2011-07-22 08:30:52 +04:00
|
|
|
}else{
|
2011-08-02 01:53:01 +04:00
|
|
|
if(!self::isMusic($path)){
|
2011-08-01 02:07:46 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-07-28 22:10:58 +04:00
|
|
|
if(!self::$getID3){
|
|
|
|
self::$getID3=@new getID3();
|
2011-10-24 16:55:05 +04:00
|
|
|
self::$getID3->encoding='UTF-8';
|
2011-07-28 22:10:58 +04:00
|
|
|
}
|
|
|
|
$data=@self::$getID3->analyze($file);
|
|
|
|
getid3_lib::CopyTagsToComments($data);
|
|
|
|
if(!isset($data['comments'])){
|
2011-10-16 23:42:24 +04:00
|
|
|
OC_Log::write('media',"error reading id3 tags in '$file'",OC_Log::WARN);
|
2011-07-28 22:10:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!isset($data['comments']['artist'])){
|
2011-10-16 23:42:24 +04:00
|
|
|
OC_Log::write('media',"error reading artist tag in '$file'",OC_Log::WARN);
|
2011-07-28 22:10:58 +04:00
|
|
|
$artist='unknown';
|
|
|
|
}else{
|
|
|
|
$artist=stripslashes($data['comments']['artist'][0]);
|
|
|
|
}
|
|
|
|
if(!isset($data['comments']['album'])){
|
2011-10-16 23:42:24 +04:00
|
|
|
OC_Log::write('media',"error reading album tag in '$file'",OC_Log::WARN);
|
2011-07-28 22:10:58 +04:00
|
|
|
$album='unknown';
|
|
|
|
}else{
|
|
|
|
$album=stripslashes($data['comments']['album'][0]);
|
|
|
|
}
|
|
|
|
if(!isset($data['comments']['title'])){
|
2011-10-16 23:42:24 +04:00
|
|
|
OC_Log::write('media',"error reading title tag in '$file'",OC_Log::WARN);
|
2011-07-28 22:10:58 +04:00
|
|
|
$title='unknown';
|
|
|
|
}else{
|
|
|
|
$title=stripslashes($data['comments']['title'][0]);
|
|
|
|
}
|
|
|
|
$size=$data['filesize'];
|
2012-01-06 14:29:15 +04:00
|
|
|
if (isset($data['comments']['track']))
|
|
|
|
{
|
|
|
|
$track = $data['comments']['track'][0];
|
|
|
|
}
|
|
|
|
else if (isset($data['comments']['track_number']))
|
|
|
|
{
|
|
|
|
$track = $data['comments']['track_number'][0];
|
|
|
|
$track = explode('/',$track);
|
|
|
|
$track = $track[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$track = 0;
|
|
|
|
}
|
2011-07-29 01:06:20 +04:00
|
|
|
$length=isset($data['playtime_seconds'])?round($data['playtime_seconds']):0;
|
2011-07-22 08:30:52 +04:00
|
|
|
}
|
|
|
|
if(!isset(self::$artists[$artist])){
|
|
|
|
$artistId=OC_MEDIA_COLLECTION::addArtist($artist);
|
|
|
|
self::$artists[$artist]=$artistId;
|
|
|
|
}else{
|
|
|
|
$artistId=self::$artists[$artist];
|
|
|
|
}
|
|
|
|
if(!isset(self::$albums[$artist.'/'.$album])){
|
|
|
|
$albumId=OC_MEDIA_COLLECTION::addAlbum($album,$artistId);
|
|
|
|
self::$albums[$artist.'/'.$album]=$albumId;
|
|
|
|
}else{
|
|
|
|
$albumId=self::$albums[$artist.'/'.$album];
|
|
|
|
}
|
|
|
|
$songId=OC_MEDIA_COLLECTION::addSong($title,$path,$artistId,$albumId,$length,$track,$size);
|
2011-08-01 02:32:44 +04:00
|
|
|
return (!($title=='unkown' && $artist=='unkown' && $album=='unkown'))?$songId:0;
|
2011-07-22 08:30:52 +04:00
|
|
|
}
|
2011-08-02 01:53:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* quick check if a song is a music file by checking the extention, not as good as a proper mimetype check but way faster
|
|
|
|
* @param string $filename
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isMusic($filename){
|
|
|
|
$ext=substr($filename,strrpos($filename,'.')+1);
|
|
|
|
return $ext=='mp3' || $ext=='flac' || $ext=='m4a' || $ext=='ogg' || $ext=='oga';
|
|
|
|
}
|
2011-10-24 16:55:05 +04:00
|
|
|
}
|