PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/phpvideotoolkit/adapters/videoto.php

https://github.com/Wargo/reddevil
PHP | 457 lines | 329 code | 53 blank | 75 comment | 24 complexity | 2b8d1d59c3bc242f2e249db265f5173f MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
  5. * @package PHPVideoToolkit
  6. * @license BSD
  7. * @copyright Copyright (c) 2008 Oliver Lillie <http://www.buggedcom.co.uk>
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
  11. * is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  15. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. class VideoTo
  20. {
  21. private static $_log_files = array();
  22. private static $_error_messages = array();
  23. private static $_commands = array();
  24. private static $_outputs = array();
  25. public static function FLV($file, $options=array(), $target_extension='flv')
  26. {
  27. // merge the options with the defaults
  28. $options = array_merge(array(
  29. 'temp_dir' => '/tmp',
  30. 'width' => 320,
  31. 'height' => 240,
  32. 'frequency' => 44100,
  33. 'audio_bitrate' => 64,
  34. 'video_bitrate' => 1200,
  35. 'ratio' => PHPVideoToolkit::RATIO_STANDARD,
  36. 'frame_rate' => 29.7,
  37. 'output_dir' => null, // this doesn't have to be set it can be automatically retreived from 'output_file'
  38. 'output_file' => '#filename.#ext', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  39. 'use_multipass' => false,
  40. 'generate_log' => true,
  41. 'log_directory' => null,
  42. 'die_on_error' => false,
  43. 'overwrite_mode' => PHPVideoToolkit::OVERWRITE_FAIL
  44. ), $options);
  45. // start PHPVideoToolkit class
  46. require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  47. $toolkit = new PHPVideoToolkit($options['temp_dir']);
  48. $toolkit->on_error_die = $options['die_on_error'];
  49. // get the output directory
  50. if($options['output_dir'])
  51. {
  52. $output_dir = $options['output_dir'];
  53. }
  54. else
  55. {
  56. $output_dir = dirname($options['output_file']);
  57. $output_dir = $output_dir == '.' ? dirname($file) : $output_dir;
  58. }
  59. // get the filename parts
  60. $filename = basename($file);
  61. $filename_minus_ext = substr($filename, 0, strrpos($filename, '.'));
  62. // get the output filename
  63. $output_filename = str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_file']));
  64. // set the input file
  65. $ok = $toolkit->setInputFile($file);
  66. // check the return value in-case of error
  67. if(!$ok)
  68. {
  69. $toolkit->reset();
  70. array_push(self::$_error_messages, $toolkit->getLastError());
  71. return false;
  72. }
  73. // set the output dimensions
  74. $toolkit->setVideoAspectRatio($options['ratio']);
  75. $toolkit->setVideoOutputDimensions($options['width'], $options['height']);
  76. $toolkit->setVideoBitRate($options['video_bitrate']);
  77. $toolkit->setVideoFrameRate($options['frame_rate']);
  78. // set the video to be converted to flv
  79. $toolkit->setFormatToFLV($options['frequency'], $options['audio_bitrate']);
  80. // set the output details and overwrite if nessecary
  81. $ok = $toolkit->setOutput($output_dir, $output_filename, $options['overwrite_mode']);
  82. // check the return value in-case of error
  83. if(!$ok)
  84. {
  85. $toolkit->reset();
  86. array_push(self::$_error_messages, $toolkit->getLastError());
  87. return false;
  88. }
  89. // execute the ffmpeg command using multiple passes and log the calls and PHPVideoToolkit results
  90. $result = $toolkit->execute($options['use_multipass'], $options['generate_log']);
  91. array_push(self::$_commands, $toolkit->getLastCommand());
  92. // check the return value in-case of error
  93. if($result !== PHPVideoToolkit::RESULT_OK)
  94. {
  95. // move the log file to the log directory as something has gone wrong
  96. if($options['generate_log'])
  97. {
  98. $log_dir = $options['log_directory'] ? $options['log_directory'] : $output_dir;
  99. $toolkit->moveLog($log_dir.$filename_minus_ext.'.log');
  100. array_push(self::$_log_files, $log_dir.$filename_minus_ext.'.log');
  101. }
  102. $toolkit->reset();
  103. array_push(self::$_error_messages, $toolkit->getLastError());
  104. return $result;
  105. }
  106. array_push(self::$_outputs, $toolkit->getLastOutput());
  107. // reset
  108. $toolkit->reset();
  109. return $result;
  110. }
  111. public static function PSP($file, $options=array(), $target_extension='mp4')
  112. {
  113. // merge the options with the defaults
  114. $options = array_merge(array(
  115. 'temp_dir' => '/tmp',
  116. 'width' => 368,
  117. 'height' => 192,
  118. 'frequency' => 44100,
  119. 'audio_bitrate' => 128,
  120. 'video_bitrate' => 1200,
  121. 'ratio' => PHPVideoToolkit::RATIO_STANDARD,
  122. 'frame_rate' => 29.7,
  123. 'output_dir' => null, // this doesn't have to be set it can be automatically retreived from 'output_file'
  124. 'output_file' => '#filename.#ext', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  125. 'output_title' => '#filename', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  126. 'use_multipass' => false,
  127. 'generate_log' => true,
  128. 'log_directory' => null,
  129. 'die_on_error' => false,
  130. 'overwrite_mode' => PHPVideoToolkit::OVERWRITE_FAIL
  131. ), $options);
  132. // start PHPVideoToolkit class
  133. require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  134. $toolkit = new PHPVideoToolkit($options['temp_dir']);
  135. $toolkit->on_error_die = $options['die_on_error'];
  136. // get the output directory
  137. if($options['output_dir'])
  138. {
  139. $output_dir = $options['output_dir'];
  140. }
  141. else
  142. {
  143. $output_dir = dirname($options['output_file']);
  144. $output_dir = $output_dir == '.' ? dirname($file) : $output_dir;
  145. }
  146. // get the filename parts
  147. $filename = basename($file);
  148. $filename_minus_ext = substr($filename, 0, strrpos($filename, '.'));
  149. // get the output filename
  150. $output_filename = str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_file']));
  151. // set the input file
  152. $ok = $toolkit->setInputFile($file);
  153. // check the return value in-case of error
  154. if(!$ok)
  155. {
  156. $toolkit->reset();
  157. array_push(self::$_error_messages, $toolkit->getLastError());
  158. return false;
  159. }
  160. $toolkit->setFormat(PHPVideoToolkit::FORMAT_PSP);
  161. $toolkit->setAudioSampleFrequency($options['frequency']);
  162. $toolkit->setAudioBitRate($options['audio_bitrate']);
  163. // $toolkit->addCommand('-acodec', 'libfaac');
  164. // $toolkit->addCommand('-acodec', 'mp3');
  165. $toolkit->setVideoFormat(PHPVideoToolkit::FORMAT_MPEG4);
  166. $toolkit->setVideoAspectRatio($options['ratio']);
  167. $toolkit->setVideoOutputDimensions($options['width'], $options['height']);
  168. $toolkit->setVideoBitRate($options['video_bitrate']);
  169. $toolkit->setVideoFrameRate($options['frame_rate']);
  170. $toolkit->addCommand('-flags', 'loop');
  171. $toolkit->addCommand('-trellis', '2');
  172. $toolkit->addCommand('-partitions', 'parti4x4+parti8x8+partp4x4+partp8x8+partb8x8');
  173. $toolkit->addCommand('-coder', '1');
  174. $toolkit->addCommand('-mbd', '2');
  175. $toolkit->addCommand('-cmp', '2');
  176. $toolkit->addCommand('-subcmp', '2');
  177. $toolkit->addCommand('-title', str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_title'])));
  178. // set the output details and overwrite if nessecary
  179. $ok = $toolkit->setOutput($output_dir, $output_filename, $options['overwrite_mode']);
  180. // check the return value in-case of error
  181. if(!$ok)
  182. {
  183. $toolkit->reset();
  184. array_push(self::$_error_messages, $toolkit->getLastError());
  185. return false;
  186. }
  187. // execute the ffmpeg command using multiple passes and log the calls and PHPVideoToolkit results
  188. $result = $toolkit->execute($options['use_multipass'], $options['generate_log']);
  189. array_push(self::$_commands, $toolkit->getLastCommand());
  190. // check the return value in-case of error
  191. if($result !== PHPVideoToolkit::RESULT_OK)
  192. {
  193. // move the log file to the log directory as something has gone wrong
  194. if($options['generate_log'])
  195. {
  196. $log_dir = $options['log_directory'] ? $options['log_directory'] : $output_dir;
  197. $toolkit->moveLog($log_dir.$filename_minus_ext.'.log');
  198. array_push(self::$_log_files, $log_dir.$filename_minus_ext.'.log');
  199. }
  200. $toolkit->reset();
  201. array_push(self::$_error_messages, $toolkit->getLastError());
  202. return $result;
  203. }
  204. array_push(self::$_outputs, $toolkit->getLastOutput());
  205. // reset
  206. $toolkit->reset();
  207. return $result;
  208. }
  209. public static function iPod($file, $options=array(), $target_extension='mp4')
  210. {
  211. // merge the options with the defaults
  212. $options = array_merge(array(
  213. 'temp_dir' => '/tmp',
  214. 'width' => 320,
  215. 'height' => 240,
  216. 'frequency' => 44100,
  217. 'audio_bitrate' => 128,
  218. 'video_bitrate' => 1200,
  219. 'ratio' => PHPVideoToolkit::RATIO_STANDARD,
  220. 'frame_rate' => 29.7,
  221. 'output_dir' => null, // this doesn't have to be set it can be automatically retreived from 'output_file'
  222. 'output_file' => '#filename.#ext', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  223. 'output_title' => '#filename', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  224. 'use_multipass' => false,
  225. 'generate_log' => true,
  226. 'log_directory' => null,
  227. 'die_on_error' => false,
  228. 'overwrite_mode' => PHPVideoToolkit::OVERWRITE_FAIL
  229. ), $options);
  230. // start PHPVideoToolkit class
  231. require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  232. $toolkit = new PHPVideoToolkit($options['temp_dir']);
  233. $toolkit->on_error_die = $options['die_on_error'];
  234. // get the output directory
  235. if($options['output_dir'])
  236. {
  237. $output_dir = $options['output_dir'];
  238. }
  239. else
  240. {
  241. $output_dir = dirname($options['output_file']);
  242. $output_dir = $output_dir == '.' ? dirname($file) : $output_dir;
  243. }
  244. // get the filename parts
  245. $filename = basename($file);
  246. $filename_minus_ext = substr($filename, 0, strrpos($filename, '.'));
  247. // get the output filename
  248. $output_filename = str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_file']));
  249. // set the input file
  250. $ok = $toolkit->setInputFile($file);
  251. // check the return value in-case of error
  252. if(!$ok)
  253. {
  254. $toolkit->reset();
  255. array_push(self::$_error_messages, $toolkit->getLastError());
  256. return false;
  257. }
  258. $toolkit->setFormat(PHPVideoToolkit::FORMAT_MP4);
  259. $toolkit->setAudioSampleFrequency($options['frequency']);
  260. $toolkit->setAudioBitRate($options['audio_bitrate']);
  261. // $toolkit->addCommand('-acodec', 'libfaac');
  262. // $toolkit->addCommand('-acodec', 'mp3');
  263. $toolkit->setVideoFormat(PHPVideoToolkit::FORMAT_MPEG4);
  264. $toolkit->setVideoAspectRatio($options['ratio']);
  265. $toolkit->setVideoOutputDimensions($options['width'], $options['height']);
  266. $toolkit->setVideoFrameRate($options['frame_rate']);
  267. $toolkit->addCommand('-mbd', '2');
  268. $toolkit->addCommand('-flags', '+4mv+trell');
  269. $toolkit->addCommand('-aic', '2');
  270. $toolkit->addCommand('-cmp', '2');
  271. $toolkit->addCommand('-subcmp', '2');
  272. $toolkit->addCommand('-title', str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_title'])));
  273. // set the output details and overwrite if nessecary
  274. $ok = $toolkit->setOutput($output_dir, $output_filename, $options['overwrite_mode']);
  275. // check the return value in-case of error
  276. if(!$ok)
  277. {
  278. $toolkit->reset();
  279. array_push(self::$_error_messages, $toolkit->getLastError());
  280. return false;
  281. }
  282. // execute the ffmpeg command using multiple passes and log the calls and PHPVideoToolkit results
  283. $result = $toolkit->execute($options['use_multipass'], $options['generate_log']);
  284. array_push(self::$_commands, $toolkit->getLastCommand());
  285. // check the return value in-case of error
  286. if($result !== PHPVideoToolkit::RESULT_OK)
  287. {
  288. // move the log file to the log directory as something has gone wrong
  289. if($options['generate_log'])
  290. {
  291. $log_dir = $options['log_directory'] ? $options['log_directory'] : $output_dir;
  292. $toolkit->moveLog($log_dir.$filename_minus_ext.'.log');
  293. array_push(self::$_log_files, $log_dir.$filename_minus_ext.'.log');
  294. }
  295. $toolkit->reset();
  296. array_push(self::$_error_messages, $toolkit->getLastError());
  297. return $result;
  298. }
  299. array_push(self::$_outputs, $toolkit->getLastOutput());
  300. // reset
  301. $toolkit->reset();
  302. return $result;
  303. }
  304. public static function gif($file, $options=array(), $target_extension='gif')
  305. {
  306. // merge the options with the defaults
  307. $options = array_merge(array(
  308. 'temp_dir' => '/tmp',
  309. 'width' => 320,
  310. 'height' => 240,
  311. 'ratio' => PHPVideoToolkit::RATIO_STANDARD,
  312. 'frame_rate' => 1,
  313. 'loop_output' => 0, // 0 will loop endlessly
  314. 'output_dir' => null, // this doesn't have to be set it can be automatically retreived from 'output_file'
  315. 'output_file' => '#filename.#ext', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  316. 'output_title' => '#filename', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension
  317. 'use_multipass' => false,
  318. 'generate_log' => true,
  319. 'log_directory' => null,
  320. 'die_on_error' => false,
  321. 'overwrite_mode' => PHPVideoToolkit::OVERWRITE_FAIL
  322. ), $options);
  323. // start PHPVideoToolkit class
  324. require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  325. $toolkit = new PHPVideoToolkit($options['temp_dir']);
  326. $toolkit->on_error_die = $options['die_on_error'];
  327. // get the output directory
  328. if($options['output_dir'])
  329. {
  330. $output_dir = $options['output_dir'];
  331. }
  332. else
  333. {
  334. $output_dir = dirname($options['output_file']);
  335. $output_dir = $output_dir == '.' ? dirname($file) : $output_dir;
  336. }
  337. // get the filename parts
  338. $filename = basename($file);
  339. $filename_minus_ext = substr($filename, 0, strrpos($filename, '.'));
  340. // get the output filename
  341. $output_filename = str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_file']));
  342. // set the input file
  343. $ok = $toolkit->setInputFile($file);
  344. // check the return value in-case of error
  345. if(!$ok)
  346. {
  347. $toolkit->reset();
  348. array_push(self::$_error_messages, $toolkit->getLastError());
  349. return false;
  350. }
  351. $toolkit->setFormat(PHPVideoToolkit::FORMAT_GIF);
  352. $toolkit->disableAudio();
  353. $toolkit->setVideoAspectRatio($options['ratio']);
  354. $toolkit->setVideoOutputDimensions($options['width'], $options['height']);
  355. $toolkit->setVideoFrameRate($options['frame_rate']);
  356. $toolkit->addCommand('-loop_output', $options['loop_output']);
  357. // set the output details and overwrite if nessecary
  358. $ok = $toolkit->setOutput($output_dir, $output_filename, $options['overwrite_mode']);
  359. // check the return value in-case of error
  360. if(!$ok)
  361. {
  362. $toolkit->reset();
  363. array_push(self::$_error_messages, $toolkit->getLastError());
  364. return false;
  365. }
  366. // execute the ffmpeg command using multiple passes and log the calls and PHPVideoToolkit results
  367. $result = $toolkit->execute($options['use_multipass'], $options['generate_log']);
  368. array_push(self::$_commands, $toolkit->getLastCommand());
  369. // check the return value in-case of error
  370. if($result !== PHPVideoToolkit::RESULT_OK)
  371. {
  372. // move the log file to the log directory as something has gone wrong
  373. if($options['generate_log'])
  374. {
  375. $log_dir = $options['log_directory'] ? $options['log_directory'] : $output_dir;
  376. $toolkit->moveLog($log_dir.$filename_minus_ext.'.log');
  377. array_push(self::$_log_files, $log_dir.$filename_minus_ext.'.log');
  378. }
  379. $toolkit->reset();
  380. array_push(self::$_error_messages, $toolkit->getLastError());
  381. return $result;
  382. }
  383. array_push(self::$_outputs, $toolkit->getLastOutput());
  384. // reset
  385. $toolkit->reset();
  386. return $result;
  387. }
  388. public static function getOutput($all=false)
  389. {
  390. return $all ? self::$_outputs : self::$_outputs[count(self::$_outputs)-1];
  391. }
  392. public static function getCommand($all=false)
  393. {
  394. return $all ? self::$_commands : self::$_commands[count(self::$_commands)-1];
  395. }
  396. public static function getError($all=false)
  397. {
  398. return $all ? self::$_error_messages : self::$_error_messages[count(self::$_error_messages)-1];
  399. }
  400. public static function getLogFile($all=false)
  401. {
  402. return $all ? self::$_log_files : self::$_log_files[count(self::$_log_files)-1];
  403. }
  404. }