You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Movie.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Alexander A. Klimov <grandmaster@al2klimov.de>
  6. * @author Daniel Schneider <daniel@schneidoa.de>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Olivier Paroz <github@oparoz.com>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Preview;
  31. use OCP\Files\File;
  32. use OCP\Files\FileInfo;
  33. use OCP\IImage;
  34. use Psr\Log\LoggerInterface;
  35. class Movie extends ProviderV2 {
  36. /**
  37. * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
  38. * @var string
  39. */
  40. public static $avconvBinary;
  41. /**
  42. * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
  43. * @var string
  44. */
  45. public static $ffmpegBinary;
  46. /** @var string */
  47. private $binary;
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function getMimeType(): string {
  52. return '/video\/.*/';
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function isAvailable(FileInfo $file): bool {
  58. // TODO: remove when avconv is dropped
  59. if (is_null($this->binary)) {
  60. if (isset($this->options['movieBinary'])) {
  61. $this->binary = $this->options['movieBinary'];
  62. } elseif (is_string(self::$avconvBinary)) {
  63. $this->binary = self::$avconvBinary;
  64. } elseif (is_string(self::$ffmpegBinary)) {
  65. $this->binary = self::$ffmpegBinary;
  66. }
  67. }
  68. return is_string($this->binary);
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  74. // TODO: use proc_open() and stream the source file ?
  75. if (!$this->isAvailable($file)) {
  76. return null;
  77. }
  78. $result = null;
  79. if ($this->useTempFile($file)) {
  80. // try downloading 5 MB first as it's likely that the first frames are present there
  81. // in some cases this doesn't work for example when the moov atom is at the
  82. // end of the file, so if it fails we fall back to getting the full file
  83. $sizeAttempts = [5242880, null];
  84. } else {
  85. // size is irrelevant, only attempt once
  86. $sizeAttempts = [null];
  87. }
  88. foreach ($sizeAttempts as $size) {
  89. $absPath = $this->getLocalFile($file, $size);
  90. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
  91. if ($result === null) {
  92. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
  93. if ($result === null) {
  94. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
  95. }
  96. }
  97. $this->cleanTmpFiles();
  98. if ($result !== null) {
  99. break;
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * @param int $maxX
  106. * @param int $maxY
  107. * @param string $absPath
  108. * @param int $second
  109. * @return null|\OCP\IImage
  110. */
  111. private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage {
  112. $tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
  113. $binaryType = substr(strrchr($this->binary, '/'), 1);
  114. if ($binaryType === 'avconv') {
  115. $cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
  116. ' -i ' . escapeshellarg($absPath) .
  117. ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
  118. ' 2>&1';
  119. } elseif ($binaryType === 'ffmpeg') {
  120. $cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
  121. ' -i ' . escapeshellarg($absPath) .
  122. ' -f mjpeg -vframes 1' .
  123. ' ' . escapeshellarg($tmpPath) .
  124. ' 2>&1';
  125. } else {
  126. // Not supported
  127. unlink($tmpPath);
  128. return null;
  129. }
  130. exec($cmd, $output, $returnCode);
  131. if ($returnCode === 0) {
  132. $image = new \OC_Image();
  133. $image->loadFromFile($tmpPath);
  134. if ($image->valid()) {
  135. unlink($tmpPath);
  136. $image->scaleDownToFit($maxX, $maxY);
  137. return $image;
  138. }
  139. }
  140. $logger = \OC::$server->get(LoggerInterface::class);
  141. $logger->error('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]);
  142. unlink($tmpPath);
  143. return null;
  144. }
  145. }