1
0

Movie.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = null;
  91. if (is_string($absPath)) {
  92. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
  93. if ($result === null) {
  94. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
  95. if ($result === null) {
  96. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
  97. }
  98. }
  99. }
  100. $this->cleanTmpFiles();
  101. if ($result !== null) {
  102. break;
  103. }
  104. }
  105. return $result;
  106. }
  107. private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage {
  108. $tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
  109. $binaryType = substr(strrchr($this->binary, '/'), 1);
  110. if ($binaryType === 'avconv') {
  111. $cmd = [$this->binary, '-y', '-ss', (string)$second,
  112. '-i', $absPath,
  113. '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1',
  114. $tmpPath];
  115. } elseif ($binaryType === 'ffmpeg') {
  116. $cmd = [$this->binary, '-y', '-ss', (string)$second,
  117. '-i', $absPath,
  118. '-f', 'mjpeg', '-vframes', '1',
  119. $tmpPath];
  120. } else {
  121. // Not supported
  122. unlink($tmpPath);
  123. return null;
  124. }
  125. $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
  126. $returnCode = -1;
  127. $output = "";
  128. if (is_resource($proc)) {
  129. $stdout = trim(stream_get_contents($pipes[1]));
  130. $stderr = trim(stream_get_contents($pipes[2]));
  131. $returnCode = proc_close($proc);
  132. $output = $stdout . $stderr;
  133. }
  134. if ($returnCode === 0) {
  135. $image = new \OCP\Image();
  136. $image->loadFromFile($tmpPath);
  137. if ($image->valid()) {
  138. unlink($tmpPath);
  139. $image->scaleDownToFit($maxX, $maxY);
  140. return $image;
  141. }
  142. }
  143. if ($second === 0) {
  144. $logger = \OC::$server->get(LoggerInterface::class);
  145. $logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]);
  146. }
  147. unlink($tmpPath);
  148. return null;
  149. }
  150. }