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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
<?php
/**
* This is a pure php emulation of the PHP module FFmpeg-PHP.
* NOTE: Please note whenever possible you should use ffmpeg-php as it is much more efficient than this pure PHP emulation.
* @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
* @package PHPVideoToolkit
* @license BSD
* @copyright Copyright (c) 2008 Oliver Lillie <http://www.buggedcom.co.uk>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* @uses php-reader
* - @link http://code.google.com/p/php-reader/
* - @author Sven Vollbehr <svollbehr@gmail.com>
* - @license http://code.google.com/p/php-reader/wiki/License New BSD License
* @see ffmpeg-php,
* - @link http://ffmpeg-php.sourceforge.net/
* - @author Todd Kirby.
* - all phpdoc documentation is lifted directly from the ffmpeg-php docs
*/
if(!defined('DS'))
{
define('DS', DIRECTORY_SEPARATOR);
}
class ffmpeg_movie
{
private $_frame_index = 1;
private $_toolkit = null;
private $_media_data = null;
private $_php_reader = null;
private $_path_to_media = null;
private $_tmp_directory = null;
/**
* Class Constructor
* @param string $path_to_media The path to the media file you want to use.
* @param string $persistent (not used in this class - but exists to emulate the ffmpeg-php module)
* @param string $tmp_directory The temp directory to which to work from. (This is only required by this class
* and not by ffmpeg-php so some minor hacking of your scripts may need to be done). (remember the trailing slash)
*/
function __construct($path_to_media, $persistent=false, $tmp_directory='/tmp/')
{
// store the media path
$this->_path_to_media = $path_to_media;
$this->_tmp_directory = $tmp_directory;
// init PHPVideoToolkit class
require_once dirname(dirname(dirname(__FILE__))).DS.'phpvideotoolkit.php5.php';
$this->_toolkit = new PHPVideoToolkit($tmp_directory);
$this->_toolkit->on_error_die = false;
// set the input
$this->_toolkit->setInputFile($path_to_media);
$this->_media_data = $this->_toolkit->getFileInfo();
// print_r($this->_media_data);
}
/**
* Access and returns the id3 information using getID3.
* @access private
* @return boolean true if the information was able to be retrieved, false if not
*/
private function _getPHPReader()
{
if($this->_php_reader === null)
{
$this->_php_reader = -1;
$php_reader = dirname(__FILE__).DS.'php-reader'.DS.'src'.DS.'ID3v1.php';
if(is_file($php_reader))
{
require_once $php_reader;
try
{
$this->_php_reader = new ID3v1($this->_path_to_media);
}
catch (Exception $e)
{
return false;
}
return true;
}
}
return $this->_php_reader !== -1;
}
/**
* Return the duration of a movie or audio file in seconds.
* @access public
* @return integer
*/
public function getDuration()
{
return $this->_media_data['duration']['seconds'];
}
/**
* Return the number of frames in a movie or audio file.
* @access public
* @return integer
*/
public function getFrameCount()
{
return $this->hasVideo() ? $this->_media_data['video']['frame_count'] : -1;
}
/**
* Return the frame rate of a movie in fps.
* @access public
* @return integer
*/
public function getFrameRate()
{
return $this->hasVideo() ? $this->_media_data['video']['frame_rate'] : -1;
}
/**
* Return the path and name of the movie file or audio file.
* @access public
* @return string
*/
public function getFilename()
{
return basename($this->_path_to_media);
}
/**
* Makes checks and returns the id3 element value.
* @access private
* @return mixed string | -1
*/
private function _getPHPReaderElement($element)
{
if($this->hasAudio())
{
if($this->_getPHPReader() && isset($this->_php_reader->{$element}))
{
return $this->_php_reader->{$element};
}
}
return -1;
}
/**
* Return the comment field from the movie or audio file.
* Returns -1 on failure.
* @access public
* @return mixed array | -1
*/
public function getComment()
{
return $this->_getPHPReaderElement('comment');
}
/**
* Return the title field from the movie or audio file.
* Returns -1 on failure.
* @access public
* @return string
*/
public function getTitle()
{
return $this->_getPHPReaderElement('title');
}
/**
* Return the copyright field from the movie or audio file.
* @access public
* @return string
*/
public function getCopyright()
{
return $this->_getPHPReaderElement('copyright');
}
/**
* Return the author field from the movie or the artist ID3 field from an mp3 file.
* @uses ffmpeg_movie::getArtist();
* @access public
* @return string
*/
public function getAuthor()
{
return $this->getArtist();
}
/**
* Return the artist ID3 field from an mp3 file.
* @access public
* @return string
*/
public function getArtist()
{
return $this->_getPHPReaderElement('artist');
}
/**
* Return the album ID3 field from an mp3 file.
* @access public
* @return string
*/
public function getAlbum()
{
return $this->_getPHPReaderElement('album');
}
/**
* Return the genre ID3 field from an mp3 file.
* @access public
* @return string
*/
public function getGenre()
{
return $this->_getPHPReaderElement('genre');
}
/**
* Return the track ID3 field from an mp3 file.
* @access public
* @return integer
*/
public function getTrackNumber()
{
return $this->_getPHPReaderElement('track');
}
/**
* Return the year ID3 field from an mp3 file.
* @access public
* @return integer
*/
public function getYear()
{
return $this->_getPHPReaderElement('year');
}
/**
* Return the height of the movie in pixels.
* @access public
* @return integer
*/
public function getFrameHeight()
{
return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['height'] : -1;
}
/**
* Return the width of the movie in pixels.
* @access public
* @return integer
*/
public function getFrameWidth()
{
return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['width'] : -1;
}
/**
* Return the pixel format of the movie.
* @access public
* @return mixed string | -1
*/
public function getPixelFormat()
{
return $this->hasVideo() ? $this->_media_data['video']['pixel_format'] : -1;
}
/**
* Return the pixel aspect ratio of the movie
* @access public
* @return integer
*/
public function getPixelAspectRatio()
{
return -1;
}
/**
* Return the bit rate of the movie or audio file in bits per second.
* @access public
* @return integer
*/
public function getBitRate()
{
return isset($this->_media_data['bitrate']) ? $this->_media_data['bitrate'] : -1;
}
/**
* Return the bit rate of the video in bits per second.
* NOTE: This only works for files with constant bit rate.
* @access public
* @return integer
*/
public function getVideoBitRate()
{
return $this->hasVideo() && isset($this->_media_data['video']['bitrate']) ? $this->_media_data['video']['bitrate'] : -1;
}
/**
* Return the audio bit rate of the media file in bits per second.
* @access public
* @return integer
*/
public function getAudioBitRate()
{
return $this->hasAudio() && isset($this->_media_data['audio']['bitrate']) ? $this->_media_data['audio']['bitrate'] : -1;
}
/**
* Return the audio sample rate of the media file in bits per second.
* @access public
* @return integer
*/
public function getAudioSampleRate()
{
return $this->hasAudio() && isset($this->_media_data['audio']['sample_rate']) ? $this->_media_data['audio']['sample_rate'] : -1;
}
/**
* Return the name of the video codec used to encode this movie as a string.
* @access public
* @param boolean $return_all If true it will return all audio codecs found.
* @return mixed string | array
*/
public function getVideoCodec($return_all=false)
{
return $this->hasVideo() ? $this->_media_data['video']['codec'] : -1;
}
/**
* Return the name of the audio codec used to encode this movie as a string.
* @access public
* @param boolean $return_all If true it will return all audio codecs found.
* @return mixed string | array
*/
public function getAudioCodec()
{
return $this->hasAudio() ? $this->_media_data['audio']['codec'] : -1;
}
/**
* Return the number of audio channels in this movie as an integer.
* @access public
* @return integer
*/
public function getAudioChannels()
{
if($this->hasAudio())
{
if($this->_getPHPReader() && isset($this->_getid3_data['audio']) && isset($this->_getid3_data['audio']['channels']))
{
return $this->_getid3_data['audio']['channels'];
}
return 1;
}
return 0;
}
/**
* Return boolean value indicating whether the movie has an audio stream.
* @access public
* @return boolean
*/
public function hasAudio()
{
return isset($this->_media_data['audio']);
}
/**
* Return boolean value indicating whether the movie has a video stream.
* @access public
* @return boolean
*/
public function hasVideo()
{
return isset($this->_media_data['video']);
}
/**
* Returns a frame from the movie as an ffmpeg_frame object.
* Returns false if the frame was not found.
* @access public
* @return mixed boolean | ffmpeg_frame
*/
public function getFrame($frame_number=false)
{
if(!$this->hasVideo())
{
return false;
}
$this->_toolkit->reset(true);
require_once dirname(__FILE__).DS.'ffmpeg_frame.php';
if(!$frame_number)
{
$frame_number = $this->_frame_index;
$this->_frame_index += 1;
}
else
{
$this->_frame_index = $frame_number;
}
// check the frame required exists in the video
if($frame_number > $this->getFrameCount())
{
return false;
}
// work out the exact frame to take
$frame_rate = $this->getFrameRate();
// generate a unique name
$tmp_name = $this->_toolkit->unique().'-%index.jpg';
// extract the frame
// print_r(array($frame_number, $frame_rate, '%ft'));
$this->_toolkit->extractFrame($frame_number, $frame_rate, '%ft');
$this->_toolkit->setOutput($this->_tmp_directory, $tmp_name, PHPVideoToolkit::OVERWRITE_EXISTING);
$result = $this->_toolkit->execute(false, true);
// check the image has been outputted
// print_r(array($this->_toolkit->getLastError(), $this->_toolkit->getLastCommand()));
// print_r(array($this->_toolkit->getLastCommand()));
// print_r(array($tmp_name, $this->_toolkit->getLastOutput()));
if($result !== PHPVideoToolkit::RESULT_OK)
{
return false;
}
// load the frame into gd
$temp_output = array_shift(array_flip($this->_toolkit->getLastOutput()));
$gd_img = imagecreatefromjpeg($temp_output);
// delete the temp image
unlink($temp_output);
// return the ffmpeg frame instance
$ffmpeg_frame_time = $this->_toolkit->formatTimecode($frame_number, '%ft', '%hh:%mm:%ss.%ms', $frame_rate);
return new ffmpeg_frame($gd_img, $ffmpeg_frame_time);
}
/**
* Note; this doesn't behave exactly as ffmpeg_movie, this will get the first frame
* of the next second in the movie.
* Returns the next key frame from the movie as an ffmpeg_frame object.
* Returns false if the frame was not found.
* @uses ffmpeg_movie::getFrame();
* @access public
* @return mixed boolean | ffmpeg_frame
*/
public function getNextKeyFrame()
{
$frame_rate = $this->getFrameRate();
// work out the next frame
$current_second = floor($frame_number/$frame_rate);
$excess = $frame_number-($seconds * $frame_rate);
$frames_to_next = $frame_rate-$excess;
$this->_frame_index += $frames_to_next;
// get the frame
return $this->getFrame();
}
/**
* Return the current frame index.
* @access public
* @return integer
*/
public function getFrameNumber()
{
return $this->_frame_index;
}
}
|