/Controller/Component/FlashVideoComponent.php

https://github.com/xemle/phtagr · PHP · 143 lines · 116 code · 7 blank · 20 comment · 9 complexity · 6fdb81f92d5f559c250a63b6cd28e835 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP versions 5
  4. *
  5. * phTagr : Organize, Browse, and Share Your Photos.
  6. * Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  7. *
  8. * Licensed under The GPL-2.0 License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  12. * @link http://www.phtagr.org phTagr
  13. * @package Phtagr
  14. * @since phTagr 2.2b3
  15. * @license GPL-2.0 (http://www.opensource.org/licenses/GPL-2.0)
  16. */
  17. class FlashVideoComponent extends Component {
  18. var $controller = null;
  19. var $components = array('FileCache', 'Command');
  20. var $config = array(
  21. 'size' => OUTPUT_SIZE_VIDEO,
  22. 'bitrate' => OUTPUT_BITRATE_VIDEO,
  23. 'framerate' => 30
  24. );
  25. var $_semaphoreId = false;
  26. public function initialize(Controller $controller) {
  27. $this->controller = $controller;
  28. if (function_exists('sem_get')) {
  29. $this->_semaphoreId = sem_get(4712);
  30. }
  31. }
  32. public function _scaleSize($media, $size) {
  33. $width = $media['Media']['width'];
  34. $height = $media['Media']['height'];
  35. if ($width > $size && $width > $height) {
  36. $height = intval($size * $height / $width);
  37. $width = $size;
  38. } elseif ($height > $size) {
  39. $width = intval($size * $width / $height);
  40. $height = $size;
  41. }
  42. // fix for ffmpeg: even frame size
  43. $width += ($width & 1);
  44. $height += ($height & 1);
  45. return $width . 'x' . $height;
  46. }
  47. /** Evaluates if the media is a flash movie
  48. @param media Current media
  49. @param file Video file of the media
  50. @return True if current media is a flash movie */
  51. public function isValidFlash($media, $file) {
  52. if ($this->controller->MyFile->getExtension($file) == 'flv' &&
  53. $media['Media']['width'] <= $this->config['size'] &&
  54. $media['Media']['height'] <= $this->config['size']) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. public function create($media, $config = array()) {
  60. $config = am($this->config, $config);
  61. if (!$this->controller->Media->isType($media, MEDIA_TYPE_VIDEO)) {
  62. CakeLog::error("Media {$media['Media']['id']} is not a video");
  63. return false;
  64. }
  65. $video = $this->controller->Media->getFile($media, FILE_TYPE_VIDEO);
  66. if (!$video) {
  67. CakeLog::error("Could not find video for media {$media['Media']['id']}");
  68. return false;
  69. }
  70. $src = $this->controller->MyFile->getFilename($video);
  71. if ($this->isValidFlash($media, $video)) {
  72. CakeLog::debug("Use media's flash video as source: $src");
  73. return $src;
  74. }
  75. $flashFilename = $this->FileCache->getFilePath($media, 'flashmovie', 'flv');
  76. if (!$flashFilename) {
  77. CakeLog::fatal("Precondition of cache directory failed: $cacheDir");
  78. return false;
  79. }
  80. if (!file_exists($flashFilename) && !$this->convertVideo($media, $src, $flashFilename, $config)) {
  81. CakeLog::error("Could not create preview file {$flashFilename}");
  82. return false;
  83. }
  84. return $flashFilename;
  85. }
  86. public function convertVideo($media, $src, $dst, $config = array()) {
  87. $config = am($this->config, $config);
  88. $bin = $this->controller->getOption('bin.ffmpeg');
  89. if (!$bin) {
  90. CakeLog::warning("Path to external program ffmpeg is missing");
  91. return false;
  92. }
  93. $args = array(
  94. '-i' => $src,
  95. '-s' => $this->_scaleSize($media, $config['size']),
  96. '-r' => $config['framerate'],
  97. '-b' => $config['bitrate'],
  98. '-ar' => 22050,
  99. '-ab' => 48,
  100. '-y', $dst);
  101. if ($this->_semaphoreId) {
  102. sem_acquire($this->_semaphoreId);
  103. }
  104. $result = $this->Command->run($bin, $args);
  105. if ($this->_semaphoreId) {
  106. sem_release($this->_semaphoreId);
  107. }
  108. if ($result != 0) {
  109. CakeLog::error("Command '$bin' returned unexcpected with $result");
  110. @unlink($dst);
  111. return false;
  112. }
  113. CakeLog::info("Created flash video '$dst' of '$src'");
  114. $this->_addCuePoints($dst);
  115. return true;
  116. }
  117. public function _addCuePoints($filename) {
  118. $bin = $this->controller->getOption('bin.flvtool2');
  119. if (!$bin) {
  120. return;
  121. }
  122. if ($this->Command->run($bin, array('-U' => $filename))) {
  123. CakeLog::error("Command '$bin' returned unexcpected $result");
  124. return false;
  125. }
  126. CakeLog::info("Updated flash video '$filename' with meta tags");
  127. return true;
  128. }
  129. }
  130. ?>