movie.php 3.1 KB

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