track.php
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Media;
interface Extractable {
public function getArtist();
public function getAlbum();
public function getTitle();
public function getTrackNumber();
public function getFileSize();
public function getLength();
}
class Track implements Extractable {
/**
* Path to the track
*/
protected $trackPath;
/**
* Array with raw getid3 data
*/
protected $trackData;
/**
* Preferred tags
*/
protected $tagType = 'id3v2';
/**
* Constructor
*
* @param $trackData array
* @param $trackPath string
*/
public function __construct($trackPath) {
$this->trackPath = $trackPath;
$extractor = new Extractor_GetID3();
$this->trackData = $extractor->extract($trackPath);
}
/**
* Get all tags as array
*
* @param none
* @return array
*/
public function getTags() {
$tags = array(
'artist' => $this->getArtist(),
'album' => $this->getAlbum(),
'title' => $this->getTitle(),
'size' => $this->getFileSize(),
'track' => $this->getTrackNumber(),
'length' => $this->getLength(),
);
return $tags;
}
/**
* Get artist title
*
* @param none
* @return string
*/
public function getArtist() {
$value = $this->getTagValue('artist');
if (is_array($value)) {
$value = $value[0];
}
if (!$value) {
$value = 'Unknown';
}
return stripslashes($value);
}
/**
* Get album title
*
* @param none
* @return string
*/
public function getAlbum(){
$value = $this->getTagValue('album');
if (is_array($value)) {
$value = $value[0];
}
if (!$value) {
$value = 'Unknown';
}
return stripslashes($value);
}
/**
* Get track title
*
* @param none
* @return string
*/
public function getTitle(){
$value = $this->getTagValue('title');
if (is_array($value)) {
$value = $value[0];
}
if (!$value) {
$value = basename($this->trackPath);
}
return stripslashes($value);
}
/**
* Get track number
*
* @param none
* @return int
*/
public function getTrackNumber(){
$value = $this->getTagValue('track');
if (is_array($value)) {
$value = $value[0];
}
if ($value !== false) {
$value = $this->getTagValue('track_number');
if (is_array($value)) {
$value = $value[0];
}
if ($value !== false && preg_match('|\d+/|', $value)) {
$value = preg_replace('|/.*$|', '', $value);
}
}
return (int) $value;
}
/**
* Get file size
*
* @param none
* @return int
*/
public function getFileSize(){
$value = (int) $this->getTagValue('filesize');
return $value;
}
/**
* Get track length in seconds
*
* @param none
* @return int
*/
public function getLength(){
$value = (int) $this->getTagValue('playtime_seconds');
return round($value);
}
/**
* Get tag value by it's name
*
* @param string
* @return string
*/
protected function getTagValue($tagname) {
$value = false;
if ($this->tagType) {
if (isset($this->trackData[$this->tagType]['comments'][$tagname])) {
$value = $this->trackData[$this->tagType]['comments'][$tagname];
}
}
if ($value===false) {
$value = $this->getDefaultTagValue($tagname);
}
return $value;
}
/**
* Get default tag value by it's name
*
* @param string
* @return string
*/
protected function getDefaultTagValue($tagname) {
return (isset($this->trackData['comments'][$tagname]) ? $this->trackData['comments'][$tagname] : false);
}
}