PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/phpvideotoolkit/adapters/ffmpeg-php/ffmpeg_movie.php

http://phpvideotoolkit.googlecode.com/
PHP | 473 lines | 218 code | 38 blank | 217 comment | 19 complexity | f2e19dc342ae7e5bd6b6ca6a1fc9db3d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * This is a pure php emulation of the PHP module FFmpeg-PHP.
  4. * NOTE: Please note whenever possible you should use ffmpeg-php as it is much more efficient than this pure PHP emulation.
  5. * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
  6. * @package PHPVideoToolkit
  7. * @license BSD
  8. * @copyright Copyright (c) 2008 Oliver Lillie <http://www.buggedcom.co.uk>
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
  12. * is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  16. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. * @uses php-reader
  20. * - @link http://code.google.com/p/php-reader/
  21. * - @author Sven Vollbehr <svollbehr@gmail.com>
  22. * - @license http://code.google.com/p/php-reader/wiki/License New BSD License
  23. * @see ffmpeg-php,
  24. * - @link http://ffmpeg-php.sourceforge.net/
  25. * - @author Todd Kirby.
  26. * - all phpdoc documentation is lifted directly from the ffmpeg-php docs
  27. */
  28. if(!defined('DS'))
  29. {
  30. define('DS', DIRECTORY_SEPARATOR);
  31. }
  32. class ffmpeg_movie
  33. {
  34. private $_frame_index = 1;
  35. private $_toolkit = null;
  36. private $_media_data = null;
  37. private $_php_reader = null;
  38. private $_path_to_media = null;
  39. private $_tmp_directory = null;
  40. /**
  41. * Class Constructor
  42. * @param string $path_to_media The path to the media file you want to use.
  43. * @param string $persistent (not used in this class - but exists to emulate the ffmpeg-php module)
  44. * @param string $tmp_directory The temp directory to which to work from. (This is only required by this class
  45. * and not by ffmpeg-php so some minor hacking of your scripts may need to be done). (remember the trailing slash)
  46. */
  47. function __construct($path_to_media, $persistent=false, $tmp_directory='/tmp/')
  48. {
  49. // store the media path
  50. $this->_path_to_media = $path_to_media;
  51. $this->_tmp_directory = $tmp_directory;
  52. // init PHPVideoToolkit class
  53. require_once dirname(dirname(dirname(__FILE__))).DS.'phpvideotoolkit.php5.php';
  54. $this->_toolkit = new PHPVideoToolkit($tmp_directory);
  55. $this->_toolkit->on_error_die = false;
  56. // set the input
  57. $this->_toolkit->setInputFile($path_to_media);
  58. $this->_media_data = $this->_toolkit->getFileInfo();
  59. // print_r($this->_media_data);
  60. }
  61. /**
  62. * Access and returns the id3 information using getID3.
  63. * @access private
  64. * @return boolean true if the information was able to be retrieved, false if not
  65. */
  66. private function _getPHPReader()
  67. {
  68. if($this->_php_reader === null)
  69. {
  70. $this->_php_reader = -1;
  71. $php_reader = dirname(__FILE__).DS.'php-reader'.DS.'src'.DS.'ID3v1.php';
  72. if(is_file($php_reader))
  73. {
  74. require_once $php_reader;
  75. try
  76. {
  77. $this->_php_reader = new ID3v1($this->_path_to_media);
  78. }
  79. catch (Exception $e)
  80. {
  81. return false;
  82. }
  83. return true;
  84. }
  85. }
  86. return $this->_php_reader !== -1;
  87. }
  88. /**
  89. * Return the duration of a movie or audio file in seconds.
  90. * @access public
  91. * @return integer
  92. */
  93. public function getDuration()
  94. {
  95. return $this->_media_data['duration']['seconds'];
  96. }
  97. /**
  98. * Return the number of frames in a movie or audio file.
  99. * @access public
  100. * @return integer
  101. */
  102. public function getFrameCount()
  103. {
  104. return $this->hasVideo() ? $this->_media_data['video']['frame_count'] : -1;
  105. }
  106. /**
  107. * Return the frame rate of a movie in fps.
  108. * @access public
  109. * @return integer
  110. */
  111. public function getFrameRate()
  112. {
  113. return $this->hasVideo() ? $this->_media_data['video']['frame_rate'] : -1;
  114. }
  115. /**
  116. * Return the path and name of the movie file or audio file.
  117. * @access public
  118. * @return string
  119. */
  120. public function getFilename()
  121. {
  122. return basename($this->_path_to_media);
  123. }
  124. /**
  125. * Makes checks and returns the id3 element value.
  126. * @access private
  127. * @return mixed string | -1
  128. */
  129. private function _getPHPReaderElement($element)
  130. {
  131. if($this->hasAudio())
  132. {
  133. if($this->_getPHPReader() && isset($this->_php_reader->{$element}))
  134. {
  135. return $this->_php_reader->{$element};
  136. }
  137. }
  138. return -1;
  139. }
  140. /**
  141. * Return the comment field from the movie or audio file.
  142. * Returns -1 on failure.
  143. * @access public
  144. * @return mixed array | -1
  145. */
  146. public function getComment()
  147. {
  148. return $this->_getPHPReaderElement('comment');
  149. }
  150. /**
  151. * Return the title field from the movie or audio file.
  152. * Returns -1 on failure.
  153. * @access public
  154. * @return string
  155. */
  156. public function getTitle()
  157. {
  158. return $this->_getPHPReaderElement('title');
  159. }
  160. /**
  161. * Return the copyright field from the movie or audio file.
  162. * @access public
  163. * @return string
  164. */
  165. public function getCopyright()
  166. {
  167. return $this->_getPHPReaderElement('copyright');
  168. }
  169. /**
  170. * Return the author field from the movie or the artist ID3 field from an mp3 file.
  171. * @uses ffmpeg_movie::getArtist();
  172. * @access public
  173. * @return string
  174. */
  175. public function getAuthor()
  176. {
  177. return $this->getArtist();
  178. }
  179. /**
  180. * Return the artist ID3 field from an mp3 file.
  181. * @access public
  182. * @return string
  183. */
  184. public function getArtist()
  185. {
  186. return $this->_getPHPReaderElement('artist');
  187. }
  188. /**
  189. * Return the album ID3 field from an mp3 file.
  190. * @access public
  191. * @return string
  192. */
  193. public function getAlbum()
  194. {
  195. return $this->_getPHPReaderElement('album');
  196. }
  197. /**
  198. * Return the genre ID3 field from an mp3 file.
  199. * @access public
  200. * @return string
  201. */
  202. public function getGenre()
  203. {
  204. return $this->_getPHPReaderElement('genre');
  205. }
  206. /**
  207. * Return the track ID3 field from an mp3 file.
  208. * @access public
  209. * @return integer
  210. */
  211. public function getTrackNumber()
  212. {
  213. return $this->_getPHPReaderElement('track');
  214. }
  215. /**
  216. * Return the year ID3 field from an mp3 file.
  217. * @access public
  218. * @return integer
  219. */
  220. public function getYear()
  221. {
  222. return $this->_getPHPReaderElement('year');
  223. }
  224. /**
  225. * Return the height of the movie in pixels.
  226. * @access public
  227. * @return integer
  228. */
  229. public function getFrameHeight()
  230. {
  231. return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['height'] : -1;
  232. }
  233. /**
  234. * Return the width of the movie in pixels.
  235. * @access public
  236. * @return integer
  237. */
  238. public function getFrameWidth()
  239. {
  240. return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['width'] : -1;
  241. }
  242. /**
  243. * Return the pixel format of the movie.
  244. * @access public
  245. * @return mixed string | -1
  246. */
  247. public function getPixelFormat()
  248. {
  249. return $this->hasVideo() ? $this->_media_data['video']['pixel_format'] : -1;
  250. }
  251. /**
  252. * Return the pixel aspect ratio of the movie
  253. * @access public
  254. * @return integer
  255. */
  256. public function getPixelAspectRatio()
  257. {
  258. return -1;
  259. }
  260. /**
  261. * Return the bit rate of the movie or audio file in bits per second.
  262. * @access public
  263. * @return integer
  264. */
  265. public function getBitRate()
  266. {
  267. return isset($this->_media_data['bitrate']) ? $this->_media_data['bitrate'] : -1;
  268. }
  269. /**
  270. * Return the bit rate of the video in bits per second.
  271. * NOTE: This only works for files with constant bit rate.
  272. * @access public
  273. * @return integer
  274. */
  275. public function getVideoBitRate()
  276. {
  277. return $this->hasVideo() && isset($this->_media_data['video']['bitrate']) ? $this->_media_data['video']['bitrate'] : -1;
  278. }
  279. /**
  280. * Return the audio bit rate of the media file in bits per second.
  281. * @access public
  282. * @return integer
  283. */
  284. public function getAudioBitRate()
  285. {
  286. return $this->hasAudio() && isset($this->_media_data['audio']['bitrate']) ? $this->_media_data['audio']['bitrate'] : -1;
  287. }
  288. /**
  289. * Return the audio sample rate of the media file in bits per second.
  290. * @access public
  291. * @return integer
  292. */
  293. public function getAudioSampleRate()
  294. {
  295. return $this->hasAudio() && isset($this->_media_data['audio']['sample_rate']) ? $this->_media_data['audio']['sample_rate'] : -1;
  296. }
  297. /**
  298. * Return the name of the video codec used to encode this movie as a string.
  299. * @access public
  300. * @param boolean $return_all If true it will return all audio codecs found.
  301. * @return mixed string | array
  302. */
  303. public function getVideoCodec($return_all=false)
  304. {
  305. return $this->hasVideo() ? $this->_media_data['video']['codec'] : -1;
  306. }
  307. /**
  308. * Return the name of the audio codec used to encode this movie as a string.
  309. * @access public
  310. * @param boolean $return_all If true it will return all audio codecs found.
  311. * @return mixed string | array
  312. */
  313. public function getAudioCodec()
  314. {
  315. return $this->hasAudio() ? $this->_media_data['audio']['codec'] : -1;
  316. }
  317. /**
  318. * Return the number of audio channels in this movie as an integer.
  319. * @access public
  320. * @return integer
  321. */
  322. public function getAudioChannels()
  323. {
  324. if($this->hasAudio())
  325. {
  326. if($this->_getPHPReader() && isset($this->_getid3_data['audio']) && isset($this->_getid3_data['audio']['channels']))
  327. {
  328. return $this->_getid3_data['audio']['channels'];
  329. }
  330. return 1;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * Return boolean value indicating whether the movie has an audio stream.
  336. * @access public
  337. * @return boolean
  338. */
  339. public function hasAudio()
  340. {
  341. return isset($this->_media_data['audio']);
  342. }
  343. /**
  344. * Return boolean value indicating whether the movie has a video stream.
  345. * @access public
  346. * @return boolean
  347. */
  348. public function hasVideo()
  349. {
  350. return isset($this->_media_data['video']);
  351. }
  352. /**
  353. * Returns a frame from the movie as an ffmpeg_frame object.
  354. * Returns false if the frame was not found.
  355. * @access public
  356. * @return mixed boolean | ffmpeg_frame
  357. */
  358. public function getFrame($frame_number=false)
  359. {
  360. if(!$this->hasVideo())
  361. {
  362. return false;
  363. }
  364. $this->_toolkit->reset(true);
  365. require_once dirname(__FILE__).DS.'ffmpeg_frame.php';
  366. if(!$frame_number)
  367. {
  368. $frame_number = $this->_frame_index;
  369. $this->_frame_index += 1;
  370. }
  371. else
  372. {
  373. $this->_frame_index = $frame_number;
  374. }
  375. // check the frame required exists in the video
  376. if($frame_number > $this->getFrameCount())
  377. {
  378. return false;
  379. }
  380. // work out the exact frame to take
  381. $frame_rate = $this->getFrameRate();
  382. // generate a unique name
  383. $tmp_name = $this->_toolkit->unique().'-%index.jpg';
  384. // extract the frame
  385. // print_r(array($frame_number, $frame_rate, '%ft'));
  386. $this->_toolkit->extractFrame($frame_number, $frame_rate, '%ft');
  387. $this->_toolkit->setOutput($this->_tmp_directory, $tmp_name, PHPVideoToolkit::OVERWRITE_EXISTING);
  388. $result = $this->_toolkit->execute(false, true);
  389. // check the image has been outputted
  390. // print_r(array($this->_toolkit->getLastError(), $this->_toolkit->getLastCommand()));
  391. // print_r(array($this->_toolkit->getLastCommand()));
  392. // print_r(array($tmp_name, $this->_toolkit->getLastOutput()));
  393. if($result !== PHPVideoToolkit::RESULT_OK)
  394. {
  395. return false;
  396. }
  397. // load the frame into gd
  398. $temp_output = array_shift(array_flip($this->_toolkit->getLastOutput()));
  399. $gd_img = imagecreatefromjpeg($temp_output);
  400. // delete the temp image
  401. unlink($temp_output);
  402. // return the ffmpeg frame instance
  403. $ffmpeg_frame_time = $this->_toolkit->formatTimecode($frame_number, '%ft', '%hh:%mm:%ss.%ms', $frame_rate);
  404. return new ffmpeg_frame($gd_img, $ffmpeg_frame_time);
  405. }
  406. /**
  407. * Note; this doesn't behave exactly as ffmpeg_movie, this will get the first frame
  408. * of the next second in the movie.
  409. * Returns the next key frame from the movie as an ffmpeg_frame object.
  410. * Returns false if the frame was not found.
  411. * @uses ffmpeg_movie::getFrame();
  412. * @access public
  413. * @return mixed boolean | ffmpeg_frame
  414. */
  415. public function getNextKeyFrame()
  416. {
  417. $frame_rate = $this->getFrameRate();
  418. // work out the next frame
  419. $current_second = floor($frame_number/$frame_rate);
  420. $excess = $frame_number-($seconds * $frame_rate);
  421. $frames_to_next = $frame_rate-$excess;
  422. $this->_frame_index += $frames_to_next;
  423. // get the frame
  424. return $this->getFrame();
  425. }
  426. /**
  427. * Return the current frame index.
  428. * @access public
  429. * @return integer
  430. */
  431. public function getFrameNumber()
  432. {
  433. return $this->_frame_index;
  434. }
  435. }