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.

106 lines
2.9 KiB

3 years ago
  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\IImage;
  33. class Movie extends ProviderV2 {
  34. public static $avconvBinary;
  35. public static $ffmpegBinary;
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function getMimeType(): string {
  40. return '/video\/.*/';
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  46. // TODO: use proc_open() and stream the source file ?
  47. $absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB
  48. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
  49. if ($result === null) {
  50. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
  51. if ($result === null) {
  52. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
  53. }
  54. }
  55. $this->cleanTmpFiles();
  56. return $result;
  57. }
  58. /**
  59. * @param int $maxX
  60. * @param int $maxY
  61. * @param string $absPath
  62. * @param int $second
  63. * @return null|\OCP\IImage
  64. */
  65. private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage {
  66. $tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
  67. if (self::$avconvBinary) {
  68. $cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
  69. ' -i ' . escapeshellarg($absPath) .
  70. ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
  71. ' > /dev/null 2>&1';
  72. } else {
  73. $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
  74. ' -i ' . escapeshellarg($absPath) .
  75. ' -f mjpeg -vframes 1' .
  76. ' ' . escapeshellarg($tmpPath) .
  77. ' > /dev/null 2>&1';
  78. }
  79. exec($cmd, $output, $returnCode);
  80. if ($returnCode === 0) {
  81. $image = new \OC_Image();
  82. $image->loadFromFile($tmpPath);
  83. if ($image->valid()) {
  84. unlink($tmpPath);
  85. $image->scaleDownToFit($maxX, $maxY);
  86. return $image;
  87. }
  88. }
  89. unlink($tmpPath);
  90. return null;
  91. }
  92. }