Movie.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Olivier Paroz <github@oparoz.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Preview;
  27. class Movie extends Provider {
  28. public static $avconvBinary;
  29. public static $ffmpegBinary;
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function getMimeType() {
  34. return '/video\/.*/';
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  40. // TODO: use proc_open() and stream the source file ?
  41. $fileInfo = $fileview->getFileInfo($path);
  42. $useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());
  43. if ($useFileDirectly) {
  44. $absPath = $fileview->getLocalFile($path);
  45. } else {
  46. $absPath = \OC::$server->getTempManager()->getTemporaryFile();
  47. $handle = $fileview->fopen($path, 'rb');
  48. // we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB.
  49. // in some cases 1MB was no enough to generate thumbnail
  50. $firstmb = stream_get_contents($handle, 5242880);
  51. file_put_contents($absPath, $firstmb);
  52. }
  53. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
  54. if ($result === false) {
  55. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
  56. if ($result === false) {
  57. $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
  58. }
  59. }
  60. if (!$useFileDirectly) {
  61. unlink($absPath);
  62. }
  63. return $result;
  64. }
  65. /**
  66. * @param int $maxX
  67. * @param int $maxY
  68. * @param string $absPath
  69. * @param int $second
  70. * @return bool|\OCP\IImage
  71. */
  72. private function generateThumbNail($maxX, $maxY, $absPath, $second) {
  73. $tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
  74. if (self::$avconvBinary) {
  75. $cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
  76. ' -i ' . escapeshellarg($absPath) .
  77. ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
  78. ' > /dev/null 2>&1';
  79. } else {
  80. $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
  81. ' -i ' . escapeshellarg($absPath) .
  82. ' -f mjpeg -vframes 1' .
  83. ' ' . escapeshellarg($tmpPath) .
  84. ' > /dev/null 2>&1';
  85. }
  86. exec($cmd, $output, $returnCode);
  87. if ($returnCode === 0) {
  88. $image = new \OC_Image();
  89. $image->loadFromFile($tmpPath);
  90. unlink($tmpPath);
  91. if ($image->valid()) {
  92. $image->scaleDownToFit($maxX, $maxY);
  93. return $image;
  94. }
  95. }
  96. unlink($tmpPath);
  97. return false;
  98. }
  99. }