PageRenderTime 51ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/Console/Command/ConversionShell.php

https://github.com/Wargo/reddevil
PHP | 293 lines | 242 code | 37 blank | 14 comment | 78 complexity | 7d149dfe0a77058418a72a2ee6ca5bfe MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. class ConversionShell extends AppShell {
  3. public $formats = array(
  4. //'mp4' => array('folder' => 'mp4', 'sizes' => array('m', 's')),
  5. //'flv' => array('folder' => 'flv', 'sizes' => array('l', 'm', 's')),
  6. //'wmv' => array('folder' => 'wmv', 'sizes' => array('l', 'm', 's')),
  7. //'v3gp' => array('folder' => '3gp', 'sizes' => array('s')),
  8. //'ogg' => array('folder' => 'ogg', 'sizes' => array('l', 'm', 's'))
  9. //'mp4' => array('folder' => 'mp4', 'sizes' => array('m', 's')),
  10. //'ogg' => array('folder' => 'ogg', 'sizes' => array('m')),
  11. //'flv' => array('folder' => 'flv', 'sizes' => array('m', 's')),
  12. 'flv' => array('folder' => 'flv', 'sizes' => array('m')),
  13. 'mp4' => array('folder' => 'mp4', 'sizes' => array('m')),
  14. );
  15. public function convert_all() {
  16. $Conversion = ClassRegistry::init('Conversion');
  17. $this->Video = ClassRegistry::init('Video');
  18. $limit = 1;
  19. $conditions = array('state' => 0);
  20. while ($conversion = $Conversion->find('first', compact('conditions'))) {
  21. $Conversion->id = $conversion['Conversion']['id'];
  22. $Conversion->save(array('state' => 1));
  23. foreach ($this->formats as $format => $config) {
  24. $this->{$format}($conversion['Conversion']['foreign_id'], $conversion['Conversion']['model']);
  25. }
  26. $Conversion->id = $conversion['Conversion']['id'];
  27. $Conversion->save(array('state' => 2));
  28. }
  29. }
  30. public function reconvert($id = false, $model = false) {
  31. if (!empty($this->args[0])) {
  32. $id = $this->args[0];
  33. }
  34. if (!empty($this->args[1])) {
  35. $model = $this->args[1];
  36. }
  37. if (!$id || !$model) {
  38. return false;
  39. }
  40. foreach ($this->formats as $format => $config) {
  41. $this->{$format}($id, $model);
  42. }
  43. }
  44. public function mp4($id, $model, $reconvert = false) {
  45. $path = Configure::read($model . 'UploadFolder');
  46. $input = $id . '.mp4';
  47. $movie = new ffmpeg_movie($path.$input, false);
  48. $duration = round($movie->getDuration());
  49. $sizes = $this->formats['mp4']['sizes'];
  50. foreach ($sizes as $size) {
  51. $output = Configure::read($model . 'RootFolder') . 'mp4' . DS . $size . DS . $id . '.mp4';
  52. if (file_exists($output)) {
  53. continue;
  54. }
  55. if ($size == 'm') {
  56. $res = '1280x720';
  57. $bitrate = '2500k';
  58. } elseif ($size = 's') {
  59. $res = '480x270';
  60. $bitrate = '700k';
  61. }
  62. if ($reconvert && file_exists($output)) {
  63. unlink($output);
  64. }
  65. if (!$reconvert && !$this->_checkVideo($id, $model, 'mp4', $size, $duration) && file_exists($output)) {
  66. unlink($output);
  67. }
  68. if (!file_exists($output)) {
  69. $priority = '';
  70. if (Configure::read('VideoProcessPriority')) {
  71. $priority = 'nice -n ' . Configure::read('VideoProcessPriority') . ' ';
  72. }
  73. $ffmpegPath = Configure::read('FfmpegPath');
  74. $cmd = $priority . " " . $ffmpegPath . "ffmpeg -i ".$path.$input." -vcodec libx264 -f mp4 -preset slow -level 30 -s ".$res." -b:v ".$bitrate." -strict -2 ".$output;
  75. shell_exec($cmd);
  76. if ($this->_checkVideo($id, $model, 'mp4', $size, $duration)) {
  77. $this->_saveFormat($id, $model, 'mp4', $size);
  78. }
  79. }
  80. }
  81. }
  82. public function flv($id, $model, $reconvert = false) {
  83. $path = Configure::read($model. 'UploadFolder');
  84. $input = $id. '.mp4';
  85. $movie = new ffmpeg_movie($path.$input, false);
  86. $duration = round($movie->getDuration());
  87. /*
  88. $w = $movie->getFrameWidth();
  89. $h = $movie->getFrameHeight();
  90. $_w = 1600;
  91. $_h = round(($_w * $h) / $w);
  92. */
  93. $sizes = $this->formats['flv']['sizes'];
  94. foreach ($sizes as $size) {
  95. $output = Configure::read($model . 'RootFolder') . 'flv' . DS . $size . DS . $id . '.flv';
  96. if ($size == 'l') {
  97. $res = '1920x1080';
  98. $bitrate = '4000k';
  99. } else if ($size == 'm') {
  100. $res = '1280x720';
  101. $bitrate = '2500k';
  102. } else if ($size == 's') {
  103. $res = '480x270';
  104. $bitrate = '700k';
  105. }
  106. if ($reconvert && file_exists($output)) {
  107. unlink($output);
  108. }
  109. if (!$reconvert && !$this->_checkVideo($id, $model, 'flv', $size, $duration) && file_exists($output)) {
  110. unlink($output);
  111. }
  112. if (!file_exists($output)) {
  113. $priority = '';
  114. if (Configure::read('VideoProcessPriority')) {
  115. $priority = 'nice -n ' . Configure::read('VideoProcessPriority') . ' ';
  116. }
  117. $ffmpegPath = Configure::read('FfmpegPath');
  118. $cmd = $priority . " " . $ffmpegPath . "ffmpeg -i ".$path.$input." -vcodec libx264 -preset medium -f flv -acodec copy -b:v ".$bitrate." -f flv -s ".$res." ".$output;
  119. shell_exec($cmd);
  120. if ($this->_checkVideo($id, $model, 'flv', $size, $duration)) {
  121. $this->_saveFormat($id, $model, 'flv', $size);
  122. }
  123. }
  124. }
  125. }
  126. public function wmv($id, $model, $reconvert = false) {
  127. $path = Configure::read($model. 'UploadFolder');
  128. $input = $id . '.mp4';
  129. $movie = new ffmpeg_movie($path.$input, false);
  130. $duration = round($movie->getDuration());
  131. $sizes = $this->formats['wmv']['sizes'];
  132. foreach ($sizes as $size) {
  133. $output = Configure::read($model . 'RootFolder') . 'wmv' . DS . $size . DS . $id . '.wmv';
  134. if ($size == 'l') {
  135. $res = '1920x1080';
  136. $bitrate = '4000k';
  137. } else if ($size == 'm') {
  138. $res = '1280x720';
  139. $bitrate = '2500k';
  140. } else if ($size == 's') {
  141. $res = '480x270';
  142. $bitrate = '700k';
  143. }
  144. if ($reconvert && file_exists($output)) {
  145. unlink($output);
  146. }
  147. if (!$reconvert && !$this->_checkVideo($id, $model, 'flv', $size, $duration) && file_exists($output)) {
  148. unlink($output);
  149. }
  150. if (!file_exists($output)) {
  151. $priority = '';
  152. if (Configure::read('VideoProcessPriority')) {
  153. $priority = 'nice -n ' . Configure::read('VideoProcessPriority') . ' ';
  154. }
  155. $ffmpegPath = Configure::read('FfmpegPath');
  156. $cmd = $priority . " " . $ffmpegPath . "ffmpeg -i ".$path.$input." -b:v ".$bitrate." -s ".$res." -ac 2 ".$output;
  157. shell_exec($cmd);
  158. if ($this->_checkVideo($id, $model, 'wmv', $size, $duration)) {
  159. $this->_saveFormat($id, $model, 'wmv', $size);
  160. }
  161. }
  162. }
  163. }
  164. public function v3gp($id, $model, $reconvert = false) {
  165. $path = Configure::read($model. 'UploadFolder');
  166. $input = $id . '.mp4';
  167. $movie = new ffmpeg_movie($path.$input, false);
  168. $duration = round($movie->getDuration());
  169. $sizes = $this->formats['v3gp']['sizes'];
  170. foreach ($sizes as $size) {
  171. $output = Configure::read($model . 'RootFolder') . '3gp' . DS . $size . DS . $id . '.3gp';
  172. if ($reconvert && file_exists($output)) {
  173. unlink($output);
  174. }
  175. if (!$reconvert && !$this->_checkVideo($id, $model, 'flv', $size, $duration) && file_exists($output)) {
  176. unlink($output);
  177. }
  178. if (!file_exists($output)) {
  179. $priority = '';
  180. if (Configure::read('VideoProcessPriority')) {
  181. $priority = 'nice -n ' . Configure::read('VideoProcessPriority') . ' ';
  182. }
  183. $ffmpegPath = Configure::read('FfmpegPath');
  184. $cmd = $priority . " " . $ffmpegPath . "ffmpeg -i ".$path.$input." -s 352x288 -q:v 0 -vcodec h263 -acodec aac -ac 1 -ar 8000 -r 25 -ab 16k -strict -2 -y ".$output;
  185. shell_exec($cmd);
  186. if ($this->_checkVideo($id, $model, 'v3gp', $size, $duration)) {
  187. $this->_saveFormat($id, $model, '3gp', $size);
  188. }
  189. }
  190. }
  191. }
  192. public function ogg ($id, $model, $reconvert = false) {
  193. $path = Configure::read($model. 'UploadFolder');
  194. $input = $id . '.mp4';
  195. $movie = new ffmpeg_movie($path.$input, false);
  196. $duration = round($movie->getDuration());
  197. $sizes = $this->formats['ogg']['sizes'];
  198. foreach ($sizes as $size) {
  199. $output = Configure::read($model . 'RootFolder') . 'ogg' . DS . $size . DS . $id . '.ogg';
  200. if ($size == 'l') {
  201. $res = '1920x1080';
  202. $bitrate = '4000k';
  203. } else if ($size == 'm') {
  204. $res = '1280x720';
  205. $bitrate = '2500k';
  206. } else if ($size == 's') {
  207. $res = '480x270';
  208. $bitrate = '700k';
  209. }
  210. if ($reconvert && file_exists($output)) {
  211. unlink($output);
  212. }
  213. if (!$reconvert && !$this->_checkVideo($id, $model, 'ogg', $size, $duration) && file_exists($output)) {
  214. unlink($output);
  215. }
  216. if (!file_exists($output)) {
  217. $priority = '';
  218. if (Configure::read('VideoProcessPriority')) {
  219. $priority = 'nice -n ' . Configure::read('VideoProcessPriority') . ' ';
  220. }
  221. $ffmpegPath = Configure::read('FfmpegPath');
  222. $cmd = $priority . " " . $ffmpegPath . "ffmpeg -i ".$path.$input." -vcodec libtheora -b:v ".$bitrate." -s ".$res." -acodec libvorbis -aq 60 ".$output;
  223. shell_exec($cmd);
  224. if ($this->_checkVideo($id, $model, 'ogg', $size, $duration)) {
  225. $this->_saveFormat($id, $model, 'ogg', $size);
  226. }
  227. }
  228. }
  229. }
  230. protected function _saveFormat($id, $model, $format, $size) {
  231. $formats = unserialize($this->Video->field('formats', array('id' => $id)));
  232. if (empty($formats[$model])) {
  233. $formats[$model] = array();
  234. }
  235. if (empty($formats[$model][$format])) {
  236. $formats[$model][$format] = array();
  237. }
  238. if (empty($formats[$model][$format][$size])) {
  239. $formats[$model][$format][$size] = 1;
  240. }
  241. $formats = serialize($formats);
  242. $this->Video->id = $id;
  243. $this->Video->save(compact('formats'));
  244. }
  245. protected function _checkVideo($id, $model, $format, $size, $duration) {
  246. $video = Configure::read($model. 'RootFolder') . $this->formats[$format]['folder'] . DS . $size . DS . $id . '.' . $format;
  247. if (!file_exists($video)) {
  248. return false;
  249. }
  250. $movie = new ffmpeg_movie($video, false);
  251. if (round($movie->getDuration()) != $duration) {
  252. return false;
  253. }
  254. return true;
  255. }
  256. }