aboutsummaryrefslogtreecommitdiff
path: root/libraries/phpvideotoolkit/adapters/toolkit/video.php
blob: 759d88466093a07febe46b11f2a11eb6226edaad (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
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
<?php

	/* SVN FILE: $Id$ */
	/**
	 * @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.
	 */
	if(!defined('DS'))
	{
		define('DS', DIRECTORY_SEPARATOR);
	}
	
	/**
	 * This is similar in terms to FFmpeg-PHP's ffmpeg_movie, however it is just for video's and not just audio.
	 */
	class PHPVideoToolkit_Video
	{
		private $_frame_index 	= 1;
		private $_toolkit 		= null;
		private $_media_data 	= 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 $tmp_directory The temp directory to which to work from. (remember the trailing slash), default set by PHPVIDEOTOOLKIT_TEMP_DIRECTORY
		 */
		function __construct($path_to_media, $tmp_directory=PHPVIDEOTOOLKIT_TEMP_DIRECTORY)
		{
// 			init PHPVideoToolkit class
			require_once dirname(dirname(dirname(__FILE__))).DS.'phpvideotoolkit.php5.php';
// 			store the media path
			$this->_path_to_media = $path_to_media;
			$this->_tmp_directory = $tmp_directory;
// 			if the path is infact a class of PHPVideoToolkit then just copy the class
			if(get_class($path_to_media) === false)
			{
				$this->_toolkit = $path_to_media;
			}
			else
			{
// 				create the toolkit item
				$this->_toolkit = new PHPVideoToolkit($tmp_directory);
				$this->_toolkit->on_error_die = false;
// 				set the input
				$this->_toolkit->setInputFile($path_to_media);
			}
		}
		
		/**
		 * Destructs any internal processes.
		 * @access private
		 */
		function __destruct()
		{
//			loop through the temp files to remove first as they have to be removed before the dir can be removed
			if(!empty($this->_unlink_files))
			{
				foreach ($this->_unlink_files as $key=>$file)
				{
					if(is_file($file))
					{
						@unlink($file);
					}
				}
				$this->_unlink_files = array();
			}
//			loop through the dirs to remove
			if(!empty($this->_unlink_dirs))
			{
				foreach ($this->_unlink_dirs as $key=>$dir)
				{
					if(is_dir($dir))
					{
						@rmdir($dir);
					}
				}
				$this->_unlink_dirs = array();
			}
		}
		
		/**
		 * Loads the meta data for the media file
		 * @access private
		 */
		private function _getMediaData()
		{
			if($this->_media_data === null)
			{
				$this->_media_data = $this->_toolkit->getFileInfo();
			}
		}
		
		/**
		 * Return the duration of a movie or audio file in seconds.
		 * @access public
		 * @return integer
		 */
		public function getDuration()
		{
			$this->_getMediaData();
			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);
		}
		
		/**
		 * 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()
		{
			$this->_getMediaData();
			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()
		{
			return $this->hasAudio();
		}
		
		/**
		 * Return boolean value indicating whether the movie has an audio stream.
		 * @access public
		 * @return boolean
		 */
		public function hasAudio()
		{
			$this->_getMediaData();
			return isset($this->_media_data['audio']);
		}
		
		/**
		 * Return boolean value indicating whether the movie has a video stream.
		 * @access public
		 * @return boolean
		 */
		public function hasVideo()
		{
			$this->_getMediaData();
			return isset($this->_media_data['video']);
		}
		
		public function convert()
		{
		}
		
		/**
		 * This will resize and return an new instance of PHPVideoToolkit_Video.
		 * @access public
		 * @return PHPVideoToolkit_Video
		 */
		public function resize($width, $height=null, $multi_pass_encode=false)
		{
// 			set the instance dimensions
			$this->_toolkit->setVideoDimensions($width, $height);
// 			create a new video toolkit object to return
			return new PHPVideoToolkit_Video($this->_toolkit, $this->_tmp_directory);
		}
		
		public function watermark()
		{
		}
		
		public function getAudio()
		{
		}
		
		public function save($filename, $multi_pass_encode=false, $overwrite_mode=PHPVideoToolkit::OVERWRITE_FAIL)
		{
			$output_directory, $output_name, 
			$path_info = pathinfo($filename);
// 			set the output
			$this->_toolkit->setOutput($path_info['dirname'], $path_parts['basename'], $overwrite_mode);
			return $this->_toolkit->execute($multi_pass_encode, false);
		}
		
		/**
		 * Returns a frame from the movie as an PHPVideoToolkit_Frame object. 
		 * Returns false if the frame was not found.
		 * @access public
		 * @return mixed boolean | PHPVideoToolkit_Frame
		 */
		public function getFrame($frame_number=false)
		{
			if(!$this->hasVideo())
			{
				return false;
			}
			$this->_toolkit->reset(true);
			require_once dirname(__FILE__).DS.'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
			$this->_toolkit->setOutput($this->_tmp_directory, $this->_toolkit->unique().'-%timecode.jpg', PHPVideoToolkit::OVERWRITE_EXISTING);
// 			extract the frame and check the extract is ok
			if(!($result = $this->_toolkit->extractFrame($frame_number, $frame_rate, '%ft'))
			{
				return $result;
			}
// 			return the PHPVideoToolkit_Frame instance
			return new PHPVideoToolkit_Frame($this->_toolkit);
		}
		
		/**
		 * Returns the next key frame from the movie as an PHPVideoToolkit_Frame object. 
		 * Returns false if the frame was not found.
		 * @uses PHPVideoToolkit_Video::getFrame();
		 * @access public
		 * @return mixed boolean | PHPVideoToolkit_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;
		}
	}