PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/adapters/ffmpeg-php/ffmpeg_movie.php

https://github.com/mariuz/firetube
PHP | 461 lines | 207 code | 36 blank | 218 comment | 18 complexity | ff5332506009fd04456343fb568feb90 MD5 | raw file
Possible License(s): LGPL-2.1
  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. class ffmpeg_movie
  29. {
  30. private $_frame_index = 1;
  31. private $_toolkit = null;
  32. private $_media_data = null;
  33. private $_php_reader = null;
  34. private $_path_to_media = null;
  35. private $_tmp_directory = null;
  36. /**
  37. * Class Constructor
  38. * @param string $path_to_media The path to the media file you want to use.
  39. * @param string $persistent (not used in this class - but exists to emulate the ffmpeg-php module)
  40. * @param string $tmp_directory The temp directory to which to work from. (This is only required by this class
  41. * and not by ffmpeg-php so some minor hacking of your scripts may need to be done). (remember the trailing slash)
  42. */
  43. function __construct($path_to_media, $persistent=false, $tmp_directory='/tmp/')
  44. {
  45. // store the media path
  46. $this->_path_to_media = $path_to_media;
  47. $this->_tmp_directory = $tmp_directory;
  48. // init PHPVideoToolkit class
  49. require_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  50. $this->_toolkit = new PHPVideoToolkit($tmp_directory);
  51. $this->_toolkit->on_error_die = false;
  52. // set the input
  53. $this->_toolkit->setInputFile($path_to_media);
  54. $this->_media_data = $this->_toolkit->getFileInfo();
  55. // print_r($this->_media_data);
  56. }
  57. /**
  58. * Access and returns the id3 information using getID3.
  59. * @access private
  60. * @return boolean true if the information was able to be retrieved, false if not
  61. */
  62. private function _getPHPReader()
  63. {
  64. if($this->_php_reader === null)
  65. {
  66. $php_reader = dirname(__FILE__).DIRECTORY_SEPARATOR.'php-reader'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'ID3v1.php';
  67. if(is_file($php_reader))
  68. {
  69. require_once $php_reader;
  70. $this->_php_reader = new ID3v1($this->_path_to_media);
  71. // print_r($this->_getid3_data);
  72. return true;
  73. }
  74. $this->_php_reader = -1;
  75. }
  76. return $this->_php_reader !== -1;
  77. }
  78. /**
  79. * Return the duration of a movie or audio file in seconds.
  80. * @access public
  81. * @return integer
  82. */
  83. public function getDuration()
  84. {
  85. return $this->_media_data['duration']['seconds'];
  86. }
  87. /**
  88. * Return the number of frames in a movie or audio file.
  89. * @access public
  90. * @return integer
  91. */
  92. public function getFrameCount()
  93. {
  94. return $this->hasVideo() ? $this->_media_data['video']['frame_count'] : -1;
  95. }
  96. /**
  97. * Return the frame rate of a movie in fps.
  98. * @access public
  99. * @return integer
  100. */
  101. public function getFrameRate()
  102. {
  103. return $this->hasVideo() ? $this->_media_data['video']['frame_rate'] : -1;
  104. }
  105. /**
  106. * Return the path and name of the movie file or audio file.
  107. * @access public
  108. * @return string
  109. */
  110. public function getFilename()
  111. {
  112. return basename($this->_path_to_media);
  113. }
  114. /**
  115. * Makes checks and returns the id3 element value.
  116. * @access private
  117. * @return mixed string | -1
  118. */
  119. private function _getPHPReaderElement($element)
  120. {
  121. if($this->hasAudio())
  122. {
  123. if($this->_getPHPReader() && isset($this->_php_reader->{$element})
  124. {
  125. return $this->_php_reader->{$element};
  126. }
  127. }
  128. return -1;
  129. }
  130. /**
  131. * Return the comment field from the movie or audio file.
  132. * Returns -1 on failure.
  133. * @access public
  134. * @return mixed array | -1
  135. */
  136. public function getComment()
  137. {
  138. return $this->_getPHPReaderElement('comment');
  139. }
  140. /**
  141. * Return the title field from the movie or audio file.
  142. * Returns -1 on failure.
  143. * @access public
  144. * @return string
  145. */
  146. public function getTitle()
  147. {
  148. return $this->_getPHPReaderElement('title');
  149. }
  150. /**
  151. * Return the copyright field from the movie or audio file.
  152. * @access public
  153. * @return string
  154. */
  155. public function getCopyright()
  156. {
  157. return $this->_getPHPReaderElement('copyright');
  158. }
  159. /**
  160. * Return the author field from the movie or the artist ID3 field from an mp3 file.
  161. * @uses ffmpeg_movie::getArtist();
  162. * @access public
  163. * @return string
  164. */
  165. public function getAuthor()
  166. {
  167. return $this->getArtist();
  168. }
  169. /**
  170. * Return the artist ID3 field from an mp3 file.
  171. * @access public
  172. * @return string
  173. */
  174. public function getArtist()
  175. {
  176. return $this->_getPHPReaderElement('artist');
  177. }
  178. /**
  179. * Return the album ID3 field from an mp3 file.
  180. * @access public
  181. * @return string
  182. */
  183. public function getAlbum()
  184. {
  185. return $this->_getPHPReaderElement('album');
  186. }
  187. /**
  188. * Return the genre ID3 field from an mp3 file.
  189. * @access public
  190. * @return string
  191. */
  192. public function getGenre()
  193. {
  194. return $this->_getPHPReaderElement('genre');
  195. }
  196. /**
  197. * Return the track ID3 field from an mp3 file.
  198. * @access public
  199. * @return integer
  200. */
  201. public function getTrackNumber()
  202. {
  203. return $this->_getPHPReaderElement('track');
  204. }
  205. /**
  206. * Return the year ID3 field from an mp3 file.
  207. * @access public
  208. * @return integer
  209. */
  210. public function getYear()
  211. {
  212. return $this->_getPHPReaderElement('year');
  213. }
  214. /**
  215. * Return the height of the movie in pixels.
  216. * @access public
  217. * @return integer
  218. */
  219. public function getFrameHeight()
  220. {
  221. return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['height'] : -1;
  222. }
  223. /**
  224. * Return the width of the movie in pixels.
  225. * @access public
  226. * @return integer
  227. */
  228. public function getFrameWidth()
  229. {
  230. return $this->hasVideo() && isset($this->_media_data['video']['dimensions']) ? $this->_media_data['video']['dimensions']['width'] : -1;
  231. }
  232. /**
  233. * Return the pixel format of the movie.
  234. * @access public
  235. * @return mixed string | -1
  236. */
  237. public function getPixelFormat()
  238. {
  239. return $this->hasVideo() ? $this->_media_data['video']['pixel_format'] : -1;
  240. }
  241. /**
  242. * Return the pixel aspect ratio of the movie
  243. * @access public
  244. * @return integer
  245. */
  246. public function getPixelAspectRatio()
  247. {
  248. return -1;
  249. }
  250. /**
  251. * Return the bit rate of the movie or audio file in bits per second.
  252. * @access public
  253. * @return integer
  254. */
  255. public function getBitRate()
  256. {
  257. return isset($this->_media_data['bitrate']) ? $this->_media_data['bitrate'] : -1;
  258. }
  259. /**
  260. * Return the bit rate of the video in bits per second.
  261. * NOTE: This only works for files with constant bit rate.
  262. * @access public
  263. * @return integer
  264. */
  265. public function getVideoBitRate()
  266. {
  267. return $this->hasVideo() && isset($this->_media_data['video']['bitrate']) ? $this->_media_data['video']['bitrate'] : -1;
  268. }
  269. /**
  270. * Return the audio bit rate of the media file in bits per second.
  271. * @access public
  272. * @return integer
  273. */
  274. public function getAudioBitRate()
  275. {
  276. return $this->hasAudio() && isset($this->_media_data['audio']['bitrate']) ? $this->_media_data['audio']['bitrate'] : -1;
  277. }
  278. /**
  279. * Return the audio sample rate of the media file in bits per second.
  280. * @access public
  281. * @return integer
  282. */
  283. public function getAudioSampleRate()
  284. {
  285. return $this->hasAudio() && isset($this->_media_data['audio']['sample_rate']) ? $this->_media_data['audio']['sample_rate'] : -1;
  286. }
  287. /**
  288. * Return the name of the video codec used to encode this movie as a string.
  289. * @access public
  290. * @param boolean $return_all If true it will return all audio codecs found.
  291. * @return mixed string | array
  292. */
  293. public function getVideoCodec($return_all=false)
  294. {
  295. return $this->hasVideo() ? $this->_media_data['video']['codec'] : -1;
  296. }
  297. /**
  298. * Return the name of the audio 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 getAudioCodec()
  304. {
  305. return $this->hasAudio() ? $this->_media_data['audio']['codec'] : -1;
  306. }
  307. /**
  308. * Return the number of audio channels in this movie as an integer.
  309. * @access public
  310. * @return integer
  311. */
  312. public function getAudioChannels()
  313. {
  314. if($this->hasAudio())
  315. {
  316. if($this->_getPHPReader() && isset($this->_getid3_data['audio']) && isset($this->_getid3_data['audio']['channels']))
  317. {
  318. return $this->_getid3_data['audio']['channels'];
  319. }
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. /**
  325. * Return boolean value indicating whether the movie has an audio stream.
  326. * @access public
  327. * @return boolean
  328. */
  329. public function hasAudio()
  330. {
  331. return isset($this->_media_data['audio']);
  332. }
  333. /**
  334. * Return boolean value indicating whether the movie has a video stream.
  335. * @access public
  336. * @return boolean
  337. */
  338. public function hasVideo()
  339. {
  340. return isset($this->_media_data['video']);
  341. }
  342. /**
  343. * Returns a frame from the movie as an ffmpeg_frame object.
  344. * Returns false if the frame was not found.
  345. * @access public
  346. * @return mixed boolean | ffmpeg_frame
  347. */
  348. public function getFrame($frame_number=false)
  349. {
  350. if(!$this->hasVideo())
  351. {
  352. return false;
  353. }
  354. $this->_toolkit->reset(true);
  355. require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'ffmpeg_frame.php';
  356. if(!$frame_number)
  357. {
  358. $frame_number = $this->_frame_index;
  359. $this->_frame_index += 1;
  360. }
  361. else
  362. {
  363. $this->_frame_index = $frame_number;
  364. }
  365. // check the frame required exists in the video
  366. if($frame_number > $this->getFrameCount())
  367. {
  368. return false;
  369. }
  370. // work out the exact frame to take
  371. $frame_rate = $this->getFrameRate();
  372. // generate a unique name
  373. $tmp_name = $this->_toolkit->unique().'-%index.jpg';
  374. // extract the frame
  375. // print_r(array($frame_number, $frame_rate, '%ft'));
  376. $this->_toolkit->extractFrame($frame_number, $frame_rate, '%ft');
  377. $this->_toolkit->setOutput($this->_tmp_directory, $tmp_name, PHPVideoToolkit::OVERWRITE_EXISTING);
  378. $result = $this->_toolkit->execute(false, true);
  379. // check the image has been outputted
  380. // print_r(array($this->_toolkit->getLastError(), $this->_toolkit->getLastCommand()));
  381. // print_r(array($this->_toolkit->getLastCommand()));
  382. // print_r(array($tmp_name, $this->_toolkit->getLastOutput()));
  383. if($result !== PHPVideoToolkit::RESULT_OK)
  384. {
  385. return false;
  386. }
  387. // load the frame into gd
  388. $temp_output = array_shift(array_flip($this->_toolkit->getLastOutput()));
  389. $gd_img = imagecreatefromjpeg($temp_output);
  390. // delete the temp image
  391. unlink($temp_output);
  392. // return the ffmpeg frame instance
  393. $ffmpeg_frame_time = $this->_toolkit->formatTimecode($frame_number, '%ft', '%hh:%mm:%ss.%ms', $frame_rate);
  394. return new ffmpeg_frame($gd_img, $ffmpeg_frame_time);
  395. }
  396. /**
  397. * Note; this doesn't behave exactly as ffmpeg_movie, this will get the first frame
  398. * of the next second in the movie.
  399. * Returns the next key frame from the movie as an ffmpeg_frame object.
  400. * Returns false if the frame was not found.
  401. * @uses ffmpeg_movie::getFrame();
  402. * @access public
  403. * @return mixed boolean | ffmpeg_frame
  404. */
  405. public function getNextKeyFrame()
  406. {
  407. $frame_rate = $this->getFrameRate();
  408. // work out the next frame
  409. $current_second = floor($frame_number/$frame_rate);
  410. $excess = $frame_number-($seconds * $frame_rate);
  411. $frames_to_next = $frame_rate-$excess;
  412. $this->_frame_index += $frames_to_next;
  413. // get the frame
  414. return $this->getFrame();
  415. }
  416. /**
  417. * Return the current frame index.
  418. * @access public
  419. * @return integer
  420. */
  421. public function getFrameNumber()
  422. {
  423. return $this->_frame_index;
  424. }
  425. }