summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/EmbedVideo/classes/media/FFProbe.php
blob: ccc2a50267accad8e7658d5ee1913fa85ecd9750 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
 * EmbedVideo
 * FFProbe
 *
 * @author		Alexia E. Smith
 * @license		MIT
 * @package		EmbedVideo
 * @link		https://www.mediawiki.org/wiki/Extension:EmbedVideo
 *
 **/

namespace EmbedVideo;

class FFProbe {
	/**
	 * File Location
	 *
	 * @var		string
	 */
	private $file;

	/**
	 * Meta Data Cache
	 *
	 * @var		array
	 */
	private $metadata = null;

	/**
	 * Main Constructor
	 *
	 * @access	public
	 * @param	string	File Location on Disk
	 * @return	void
	 */
	public function __construct($file) {
		$this->file = $file;
	}

	/**
	 * Return the entire cache of meta data.
	 *
	 * @access	public
	 * @return	array	Meta Data
	 */
	public function getMetaData() {
		if (!is_array($this->metadata)) {
			$this->invokeFFProbe();
		}
		return $this->metadata;
	}

	/**
	 * Get a selected stream.  Follows ffmpeg's stream selection style.
	 *
	 * @access	public
	 * @param	string	Stream identifier
	 * Examples:
	 *		"v:0" - Select the first video stream
	 * 		"a:1" - Second audio stream
	 * 		"i:0" - First stream, whatever it is.
	 * 		"s:2" - Third subtitle
	 * 		"d:0" - First generic data stream
	 * 		"t:1" - Second attachment
	 * @return	mixed	StreamInfo object or false if does not exist.
	 */
	public function getStream($select) {
		$this->getMetaData();

		$types = [
			'v'	=> 'video',
			'a'	=> 'audio',
			'i'	=> false,
			's'	=> 'subtitle',
			'd'	=> 'data',
			't'	=> 'attachment'
		];

		if (!isset($this->metadata['streams'])) {
			return false;
		}

		list($type, $index) = explode(":", $select);
		$index = intval($index);

		$type = (isset($types[$type]) ? $types[$type] : false);

		$i = 0;
		foreach ($this->metadata['streams'] as $stream) {
			if ($type !== false && isset($stream['codec_type'])) {
				if ($index === $i && $stream['codec_type'] === $type) {
					return new StreamInfo($stream);
				}
			}
			if ($type === false || $stream['codec_type'] === $type) {
				$i++;
			}
		}
		return false;
	}

	/**
	 * Get the FormatInfo object.
	 *
	 * @access	public
	 * @return	mixed	FormatInfo object or false if does not exist.
	 */
	public function getFormat() {
		$this->getMetaData();

		if (!isset($this->metadata['format'])) {
			return false;
		}

		return new FormatInfo($this->metadata['format']);
	}

	/**
	 * Invoke ffprobe on the command line.
	 *
	 * @access	private
	 * @return	boolean	Success
	 */
	private function invokeFFProbe() {
		global $wgFFprobeLocation;

		if (!file_exists($wgFFprobeLocation)) {
			$this->metadata = [];
			return false;
		}

		$json = shell_exec(escapeshellcmd($wgFFprobeLocation.' -v quiet -print_format json -show_format -show_streams ').escapeshellarg($this->file));

		$metadata = @json_decode($json, true);

		if (is_array($metadata)) {
			$this->metadata = $metadata;
		} else {
			$this->metadata = [];
			return false;
		}
		return true;
	}
}

class StreamInfo {
	/**
	 * Stream Info
	 *
	 * @var		array
	 */
	private $info = null;

	/**
	 * Main Constructor
	 *
	 * @access	public
	 * @param	array	Stream Info from FFProbe
	 * @return	void
	 */
	public function __construct($info) {
		$this->info = $info;
	}

	/**
	 * Simple helper instead of repeating an if statement everything.
	 *
	 * @access	private
	 * @param	string	Field Name
	 * @return	void
	 */
	private function getField($field) {
		return (isset($this->info[$field]) ? $this->info[$field] : false);
	}

	/**
	 * Return the codec type.
	 *
	 * @access	public
	 * @return 	string	Codec type or false if unavailable.
	 */
	public function getType() {
		return $this->getField('codec_type');
	}

	/**
	 * Return the codec name.
	 *
	 * @access	public
	 * @return 	string	Codec name or false if unavailable.
	 */
	public function getCodecName() {
		return $this->getField('codec_name');
	}

	/**
	 * Return the codec long name.
	 *
	 * @access	public
	 * @return 	string	Codec long name or false if unavailable.
	 */
	public function getCodecLongName() {
		return $this->getField('codec_long_name');
	}

	/**
	 * Return the width of the stream.
	 *
	 * @access	public
	 * @return 	integer	Width or false if unavailable.
	 */
	public function getWidth() {
		return $this->getField('width');
	}

	/**
	 * Return the height of the stream.
	 *
	 * @access	public
	 * @return 	integer	Height or false if unavailable.
	 */
	public function getHeight() {
		return $this->getField('height');
	}

	/**
	 * Return bit depth for a video or thumbnail.
	 *
	 * @access	public
	 * @return 	integer	Bit Depth or false if unavailable.
	 */
	public function getBitDepth() {
		return $this->getField('bits_per_raw_sample');
	}

	/**
	 * Get the duration in seconds.
	 *
	 * @access	public
	 * @return 	mixed	Duration in seconds or false if unavailable.
	 */
	public function getDuration() {
		return $this->getField('duration');
	}

	/**
	 * Bit rate in bPS.
	 *
	 * @access	public
	 * @return 	mixed	Bite rate in bPS or false if unavailable.
	 */
	public function getBitRate() {
		return $this->getField('bit_rate');
	}
}

class FormatInfo {
	/**
	 * Format Info
	 *
	 * @var		array
	 */
	private $info = null;

	/**
	 * Main Constructor
	 *
	 * @access	public
	 * @param	array	Format Info from FFProbe
	 * @return	void
	 */
	public function __construct($info) {
		$this->info = $info;
	}

	/**
	 * Simple helper instead of repeating an if statement everything.
	 *
	 * @access	private
	 * @param	string	Field Name
	 * @return	void
	 */
	private function getField($field) {
		return (isset($this->info[$field]) ? $this->info[$field] : false);
	}

	/**
	 * Get the file path.
	 *
	 * @access	public
	 * @return 	mixed	File path or false if unavailable.
	 */
	public function getFilePath() {
		return $this->getField('filename');
	}

	/**
	 * Get the duration in seconds.
	 *
	 * @access	public
	 * @return 	mixed	Duration in seconds or false if unavailable.
	 */
	public function getDuration() {
		return $this->getField('duration');
	}

	/**
	 * Bit rate in bPS.
	 *
	 * @access	public
	 * @return 	mixed	Bite rate in bPS or false if unavailable.
	 */
	public function getBitRate() {
		return $this->getField('bit_rate');
	}
}