@@ -35,138 +35,138 @@ |
||
| 35 | 35 | use Psr\Log\LoggerInterface; |
| 36 | 36 | |
| 37 | 37 | class Movie extends ProviderV2 { |
| 38 | - /** |
|
| 39 | - * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - public static $avconvBinary; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - public static $ffmpegBinary; |
|
| 49 | - |
|
| 50 | - /** @var string */ |
|
| 51 | - private $binary; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * {@inheritDoc} |
|
| 55 | - */ |
|
| 56 | - public function getMimeType(): string { |
|
| 57 | - return '/video\/.*/'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * {@inheritDoc} |
|
| 62 | - */ |
|
| 63 | - public function isAvailable(FileInfo $file): bool { |
|
| 64 | - // TODO: remove when avconv is dropped |
|
| 65 | - if (is_null($this->binary)) { |
|
| 66 | - if (isset($this->options['movieBinary'])) { |
|
| 67 | - $this->binary = $this->options['movieBinary']; |
|
| 68 | - } elseif (is_string(self::$avconvBinary)) { |
|
| 69 | - $this->binary = self::$avconvBinary; |
|
| 70 | - } elseif (is_string(self::$ffmpegBinary)) { |
|
| 71 | - $this->binary = self::$ffmpegBinary; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - return is_string($this->binary); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * {@inheritDoc} |
|
| 79 | - */ |
|
| 80 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
| 81 | - // TODO: use proc_open() and stream the source file ? |
|
| 82 | - |
|
| 83 | - if (!$this->isAvailable($file)) { |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $result = null; |
|
| 88 | - if ($this->useTempFile($file)) { |
|
| 89 | - // try downloading 5 MB first as it's likely that the first frames are present there |
|
| 90 | - // in some cases this doesn't work for example when the moov atom is at the |
|
| 91 | - // end of the file, so if it fails we fall back to getting the full file |
|
| 92 | - $sizeAttempts = [5242880, null]; |
|
| 93 | - } else { |
|
| 94 | - // size is irrelevant, only attempt once |
|
| 95 | - $sizeAttempts = [null]; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - foreach ($sizeAttempts as $size) { |
|
| 99 | - $absPath = $this->getLocalFile($file, $size); |
|
| 100 | - |
|
| 101 | - $result = null; |
|
| 102 | - if (is_string($absPath)) { |
|
| 103 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); |
|
| 104 | - if ($result === null) { |
|
| 105 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); |
|
| 106 | - if ($result === null) { |
|
| 107 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->cleanTmpFiles(); |
|
| 113 | - |
|
| 114 | - if ($result !== null) { |
|
| 115 | - break; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $result; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 123 | - $tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); |
|
| 124 | - |
|
| 125 | - $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 126 | - |
|
| 127 | - if ($binaryType === 'avconv') { |
|
| 128 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 129 | - '-i', $absPath, |
|
| 130 | - '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
|
| 131 | - $tmpPath]; |
|
| 132 | - } elseif ($binaryType === 'ffmpeg') { |
|
| 133 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 134 | - '-i', $absPath, |
|
| 135 | - '-f', 'mjpeg', '-vframes', '1', |
|
| 136 | - $tmpPath]; |
|
| 137 | - } else { |
|
| 138 | - // Not supported |
|
| 139 | - unlink($tmpPath); |
|
| 140 | - return null; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 144 | - $returnCode = -1; |
|
| 145 | - $output = ""; |
|
| 146 | - if (is_resource($proc)) { |
|
| 147 | - $stdout = trim(stream_get_contents($pipes[1])); |
|
| 148 | - $stderr = trim(stream_get_contents($pipes[2])); |
|
| 149 | - $returnCode = proc_close($proc); |
|
| 150 | - $output = $stdout . $stderr; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - if ($returnCode === 0) { |
|
| 154 | - $image = new \OCP\Image(); |
|
| 155 | - $image->loadFromFile($tmpPath); |
|
| 156 | - if ($image->valid()) { |
|
| 157 | - unlink($tmpPath); |
|
| 158 | - $image->scaleDownToFit($maxX, $maxY); |
|
| 159 | - |
|
| 160 | - return $image; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if ($second === 0) { |
|
| 165 | - $logger = \OC::$server->get(LoggerInterface::class); |
|
| 166 | - $logger->error('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - unlink($tmpPath); |
|
| 170 | - return null; |
|
| 171 | - } |
|
| 38 | + /** |
|
| 39 | + * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + public static $avconvBinary; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + public static $ffmpegBinary; |
|
| 49 | + |
|
| 50 | + /** @var string */ |
|
| 51 | + private $binary; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * {@inheritDoc} |
|
| 55 | + */ |
|
| 56 | + public function getMimeType(): string { |
|
| 57 | + return '/video\/.*/'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * {@inheritDoc} |
|
| 62 | + */ |
|
| 63 | + public function isAvailable(FileInfo $file): bool { |
|
| 64 | + // TODO: remove when avconv is dropped |
|
| 65 | + if (is_null($this->binary)) { |
|
| 66 | + if (isset($this->options['movieBinary'])) { |
|
| 67 | + $this->binary = $this->options['movieBinary']; |
|
| 68 | + } elseif (is_string(self::$avconvBinary)) { |
|
| 69 | + $this->binary = self::$avconvBinary; |
|
| 70 | + } elseif (is_string(self::$ffmpegBinary)) { |
|
| 71 | + $this->binary = self::$ffmpegBinary; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + return is_string($this->binary); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * {@inheritDoc} |
|
| 79 | + */ |
|
| 80 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
| 81 | + // TODO: use proc_open() and stream the source file ? |
|
| 82 | + |
|
| 83 | + if (!$this->isAvailable($file)) { |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $result = null; |
|
| 88 | + if ($this->useTempFile($file)) { |
|
| 89 | + // try downloading 5 MB first as it's likely that the first frames are present there |
|
| 90 | + // in some cases this doesn't work for example when the moov atom is at the |
|
| 91 | + // end of the file, so if it fails we fall back to getting the full file |
|
| 92 | + $sizeAttempts = [5242880, null]; |
|
| 93 | + } else { |
|
| 94 | + // size is irrelevant, only attempt once |
|
| 95 | + $sizeAttempts = [null]; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + foreach ($sizeAttempts as $size) { |
|
| 99 | + $absPath = $this->getLocalFile($file, $size); |
|
| 100 | + |
|
| 101 | + $result = null; |
|
| 102 | + if (is_string($absPath)) { |
|
| 103 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); |
|
| 104 | + if ($result === null) { |
|
| 105 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); |
|
| 106 | + if ($result === null) { |
|
| 107 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $this->cleanTmpFiles(); |
|
| 113 | + |
|
| 114 | + if ($result !== null) { |
|
| 115 | + break; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $result; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 123 | + $tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); |
|
| 124 | + |
|
| 125 | + $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 126 | + |
|
| 127 | + if ($binaryType === 'avconv') { |
|
| 128 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 129 | + '-i', $absPath, |
|
| 130 | + '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
|
| 131 | + $tmpPath]; |
|
| 132 | + } elseif ($binaryType === 'ffmpeg') { |
|
| 133 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 134 | + '-i', $absPath, |
|
| 135 | + '-f', 'mjpeg', '-vframes', '1', |
|
| 136 | + $tmpPath]; |
|
| 137 | + } else { |
|
| 138 | + // Not supported |
|
| 139 | + unlink($tmpPath); |
|
| 140 | + return null; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 144 | + $returnCode = -1; |
|
| 145 | + $output = ""; |
|
| 146 | + if (is_resource($proc)) { |
|
| 147 | + $stdout = trim(stream_get_contents($pipes[1])); |
|
| 148 | + $stderr = trim(stream_get_contents($pipes[2])); |
|
| 149 | + $returnCode = proc_close($proc); |
|
| 150 | + $output = $stdout . $stderr; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + if ($returnCode === 0) { |
|
| 154 | + $image = new \OCP\Image(); |
|
| 155 | + $image->loadFromFile($tmpPath); |
|
| 156 | + if ($image->valid()) { |
|
| 157 | + unlink($tmpPath); |
|
| 158 | + $image->scaleDownToFit($maxX, $maxY); |
|
| 159 | + |
|
| 160 | + return $image; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if ($second === 0) { |
|
| 165 | + $logger = \OC::$server->get(LoggerInterface::class); |
|
| 166 | + $logger->error('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + unlink($tmpPath); |
|
| 170 | + return null; |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | $binaryType = substr(strrchr($this->binary, '/'), 1); |
| 126 | 126 | |
| 127 | 127 | if ($binaryType === 'avconv') { |
| 128 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 128 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 129 | 129 | '-i', $absPath, |
| 130 | 130 | '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
| 131 | 131 | $tmpPath]; |
| 132 | 132 | } elseif ($binaryType === 'ffmpeg') { |
| 133 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 133 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 134 | 134 | '-i', $absPath, |
| 135 | 135 | '-f', 'mjpeg', '-vframes', '1', |
| 136 | 136 | $tmpPath]; |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $stdout = trim(stream_get_contents($pipes[1])); |
| 148 | 148 | $stderr = trim(stream_get_contents($pipes[2])); |
| 149 | 149 | $returnCode = proc_close($proc); |
| 150 | - $output = $stdout . $stderr; |
|
| 150 | + $output = $stdout.$stderr; |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | if ($returnCode === 0) { |