1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Stinger Media Parser package. |
4
|
|
|
* |
5
|
|
|
* (c) Oliver Kotte <[email protected]> |
6
|
|
|
* (c) Florian Meyer <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace StingerSoft\MediaParsingBundle\Parser\Types; |
12
|
|
|
|
13
|
|
|
use StingerSoft\MediaParsingBundle\Parser\MediaParserInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
15
|
|
|
use StingerSoft\MediaParsingBundle\Parser\Information\SongInformation; |
16
|
|
|
use GetId3\GetId3Core; |
17
|
|
|
|
18
|
|
|
class Mp3Parser implements MediaParserInterface { |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
CONST ALBUM = 'album'; |
22
|
|
|
|
23
|
|
|
CONST ARTIST = 'artist'; |
24
|
|
|
|
25
|
|
|
CONST TITLE = 'title'; |
26
|
|
|
|
27
|
|
|
CONST PART_OF_A_SET = 'part_of_a_set'; |
28
|
|
|
|
29
|
|
|
CONST TRACK_NUMBER = 'track_number'; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/* (non-PHPdoc) |
33
|
|
|
* @see \StingerSoft\MediaParsingBundle\Parser\AudioParserInterface::parseFile() |
34
|
|
|
*/ |
35
|
|
|
public function parseFile(File $file){ |
36
|
|
|
$getId3 = new GetId3Core(); |
37
|
|
|
$getId3->option_md5_data = true; |
38
|
|
|
$getId3->option_md5_data_source = true; |
39
|
|
|
|
40
|
|
|
$audio = $getId3->analyze($file->getRealPath()); |
41
|
|
|
if(isset($audio['error'])){ |
42
|
|
|
throw new \RuntimeException('Error at reading audio properties with GetId3 : ' . $file->getRealPath()." ". print_r($audio['error'], true)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
//No id3v2 tag found |
46
|
|
|
if(!array_key_exists('tags', $audio) || !array_key_exists('id3v2', $audio['tags'])){ |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$information = new SongInformation(); |
51
|
|
|
|
52
|
|
|
$information->setAlbum($this->getAttribute($audio, self::ALBUM)); |
53
|
|
|
$information->setArtist($this->getAttribute($audio, self::ARTIST)); |
54
|
|
|
$information->setMimeType('audio/mp3'); |
55
|
|
|
$information->setTitle($this->getAttribute($audio, self::TITLE)); |
56
|
|
|
|
57
|
|
|
$partOfASet = $this->getAttribute($audio, self::PART_OF_A_SET); |
58
|
|
|
if(\strpos($partOfASet, "/") !== false){ |
59
|
|
|
$setnumber = explode("/", $partOfASet); |
60
|
|
|
$information->setSetNumber($setnumber[0]); |
61
|
|
|
$information->setTotalSetNumbers($setnumber[1]); |
62
|
|
|
}else{ |
63
|
|
|
$information->setSetNumber($partOfASet); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$trackNumberArr = $this->getAttribute($audio, self::PART_OF_A_SET); |
67
|
|
|
if(\strpos($trackNumberArr, "/") !== false){ |
68
|
|
|
$trackNumber = explode("/", $trackNumberArr); |
69
|
|
|
$information->setTrackNumber($trackNumber[0]); |
70
|
|
|
$information->setTotalTrackNumbers($trackNumber[1]); |
71
|
|
|
}else{ |
72
|
|
|
$information->setTrackNumber($trackNumberArr); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $information; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getAttribute($audio, $property){ |
79
|
|
|
if(array_key_exists($property, $audio['tags']['id3v2'])){ |
80
|
|
|
$values = $audio['tags']['id3v2'][$property]; |
81
|
|
|
if(is_array($values)){ |
82
|
|
|
if(count($values)>0){ |
83
|
|
|
return $values[0]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return $values; |
87
|
|
|
} |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* (non-PHPdoc) |
92
|
|
|
* @see \StingerSoft\MediaParsingBundle\Parser\AudioParserInterface::canHandle() |
93
|
|
|
*/ |
94
|
|
|
public function canHandle(File $file){ |
95
|
|
|
return $file->getExtension() == "mp3"; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|