@@ -18,23 +18,23 @@ |
||
| 18 | 18 | * @package Test\Preview |
| 19 | 19 | */ |
| 20 | 20 | class MovieTest extends Provider { |
| 21 | - protected function setUp(): void { |
|
| 22 | - $binaryFinder = Server::get(IBinaryFinder::class); |
|
| 23 | - $movieBinary = $binaryFinder->findBinaryPath('avconv'); |
|
| 24 | - if (!is_string($movieBinary)) { |
|
| 25 | - $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); |
|
| 26 | - } |
|
| 21 | + protected function setUp(): void { |
|
| 22 | + $binaryFinder = Server::get(IBinaryFinder::class); |
|
| 23 | + $movieBinary = $binaryFinder->findBinaryPath('avconv'); |
|
| 24 | + if (!is_string($movieBinary)) { |
|
| 25 | + $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - if (is_string($movieBinary)) { |
|
| 29 | - parent::setUp(); |
|
| 28 | + if (is_string($movieBinary)) { |
|
| 29 | + parent::setUp(); |
|
| 30 | 30 | |
| 31 | - $fileName = 'testimage.mp4'; |
|
| 32 | - $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); |
|
| 33 | - $this->width = 560; |
|
| 34 | - $this->height = 320; |
|
| 35 | - $this->provider = new \OC\Preview\Movie(['movieBinary' => $movieBinary]); |
|
| 36 | - } else { |
|
| 37 | - $this->markTestSkipped('No Movie provider present'); |
|
| 38 | - } |
|
| 39 | - } |
|
| 31 | + $fileName = 'testimage.mp4'; |
|
| 32 | + $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); |
|
| 33 | + $this->width = 560; |
|
| 34 | + $this->height = 320; |
|
| 35 | + $this->provider = new \OC\Preview\Movie(['movieBinary' => $movieBinary]); |
|
| 36 | + } else { |
|
| 37 | + $this->markTestSkipped('No Movie provider present'); |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | parent::setUp(); |
| 30 | 30 | |
| 31 | 31 | $fileName = 'testimage.mp4'; |
| 32 | - $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); |
|
| 32 | + $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT.'/tests/data/'.$fileName); |
|
| 33 | 33 | $this->width = 560; |
| 34 | 34 | $this->height = 320; |
| 35 | 35 | $this->provider = new \OC\Preview\Movie(['movieBinary' => $movieBinary]); |
@@ -18,91 +18,91 @@ |
||
| 18 | 18 | use Psr\Log\LoggerInterface; |
| 19 | 19 | |
| 20 | 20 | abstract class ProviderV2 implements IProviderV2 { |
| 21 | - protected array $tmpFiles = []; |
|
| 22 | - |
|
| 23 | - public function __construct( |
|
| 24 | - protected array $options = [], |
|
| 25 | - ) { |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return string Regex with the mimetypes that are supported by this provider |
|
| 30 | - */ |
|
| 31 | - abstract public function getMimeType(): string ; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Check if a preview can be generated for $path |
|
| 35 | - * |
|
| 36 | - * @param FileInfo $file |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - public function isAvailable(FileInfo $file): bool { |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * get thumbnail for file at path $path |
|
| 45 | - * |
|
| 46 | - * @param File $file |
|
| 47 | - * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 48 | - * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 49 | - * @return null|\OCP\IImage null if no preview was generated |
|
| 50 | - * @since 17.0.0 |
|
| 51 | - */ |
|
| 52 | - abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage; |
|
| 53 | - |
|
| 54 | - protected function useTempFile(File $file): bool { |
|
| 55 | - return $file->isEncrypted() || !$file->getStorage()->isLocal(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Get a path to either the local file or temporary file |
|
| 60 | - * |
|
| 61 | - * @param File $file |
|
| 62 | - * @param ?int $maxSize maximum size for temporary files |
|
| 63 | - */ |
|
| 64 | - protected function getLocalFile(File $file, ?int $maxSize = null): string|false { |
|
| 65 | - if ($this->useTempFile($file)) { |
|
| 66 | - $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 67 | - |
|
| 68 | - if ($absPath === false) { |
|
| 69 | - Server::get(LoggerInterface::class)->error( |
|
| 70 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 71 | - ['app' => 'core'] |
|
| 72 | - ); |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $content = $file->fopen('r'); |
|
| 77 | - if ($content === false) { |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ($maxSize) { |
|
| 82 | - $content = stream_get_contents($content, $maxSize); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - file_put_contents($absPath, $content); |
|
| 86 | - $this->tmpFiles[] = $absPath; |
|
| 87 | - return $absPath; |
|
| 88 | - } else { |
|
| 89 | - $path = $file->getStorage()->getLocalFile($file->getInternalPath()); |
|
| 90 | - if (is_string($path)) { |
|
| 91 | - return $path; |
|
| 92 | - } else { |
|
| 93 | - return false; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Clean any generated temporary files |
|
| 100 | - */ |
|
| 101 | - protected function cleanTmpFiles(): void { |
|
| 102 | - foreach ($this->tmpFiles as $tmpFile) { |
|
| 103 | - unlink($tmpFile); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->tmpFiles = []; |
|
| 107 | - } |
|
| 21 | + protected array $tmpFiles = []; |
|
| 22 | + |
|
| 23 | + public function __construct( |
|
| 24 | + protected array $options = [], |
|
| 25 | + ) { |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return string Regex with the mimetypes that are supported by this provider |
|
| 30 | + */ |
|
| 31 | + abstract public function getMimeType(): string ; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Check if a preview can be generated for $path |
|
| 35 | + * |
|
| 36 | + * @param FileInfo $file |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + public function isAvailable(FileInfo $file): bool { |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * get thumbnail for file at path $path |
|
| 45 | + * |
|
| 46 | + * @param File $file |
|
| 47 | + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 48 | + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 49 | + * @return null|\OCP\IImage null if no preview was generated |
|
| 50 | + * @since 17.0.0 |
|
| 51 | + */ |
|
| 52 | + abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage; |
|
| 53 | + |
|
| 54 | + protected function useTempFile(File $file): bool { |
|
| 55 | + return $file->isEncrypted() || !$file->getStorage()->isLocal(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Get a path to either the local file or temporary file |
|
| 60 | + * |
|
| 61 | + * @param File $file |
|
| 62 | + * @param ?int $maxSize maximum size for temporary files |
|
| 63 | + */ |
|
| 64 | + protected function getLocalFile(File $file, ?int $maxSize = null): string|false { |
|
| 65 | + if ($this->useTempFile($file)) { |
|
| 66 | + $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 67 | + |
|
| 68 | + if ($absPath === false) { |
|
| 69 | + Server::get(LoggerInterface::class)->error( |
|
| 70 | + 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 71 | + ['app' => 'core'] |
|
| 72 | + ); |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $content = $file->fopen('r'); |
|
| 77 | + if ($content === false) { |
|
| 78 | + return false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ($maxSize) { |
|
| 82 | + $content = stream_get_contents($content, $maxSize); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + file_put_contents($absPath, $content); |
|
| 86 | + $this->tmpFiles[] = $absPath; |
|
| 87 | + return $absPath; |
|
| 88 | + } else { |
|
| 89 | + $path = $file->getStorage()->getLocalFile($file->getInternalPath()); |
|
| 90 | + if (is_string($path)) { |
|
| 91 | + return $path; |
|
| 92 | + } else { |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Clean any generated temporary files |
|
| 100 | + */ |
|
| 101 | + protected function cleanTmpFiles(): void { |
|
| 102 | + foreach ($this->tmpFiles as $tmpFile) { |
|
| 103 | + unlink($tmpFile); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->tmpFiles = []; |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * @return string Regex with the mimetypes that are supported by this provider |
| 30 | 30 | */ |
| 31 | - abstract public function getMimeType(): string ; |
|
| 31 | + abstract public function getMimeType(): string; |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Check if a preview can be generated for $path |
@@ -61,13 +61,13 @@ discard block |
||
| 61 | 61 | * @param File $file |
| 62 | 62 | * @param ?int $maxSize maximum size for temporary files |
| 63 | 63 | */ |
| 64 | - protected function getLocalFile(File $file, ?int $maxSize = null): string|false { |
|
| 64 | + protected function getLocalFile(File $file, ?int $maxSize = null): string | false { |
|
| 65 | 65 | if ($this->useTempFile($file)) { |
| 66 | 66 | $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
| 67 | 67 | |
| 68 | 68 | if ($absPath === false) { |
| 69 | 69 | Server::get(LoggerInterface::class)->error( |
| 70 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 70 | + 'Failed to get local file to generate thumbnail for: '.$file->getPath(), |
|
| 71 | 71 | ['app' => 'core'] |
| 72 | 72 | ); |
| 73 | 73 | return false; |
@@ -17,173 +17,173 @@ |
||
| 17 | 17 | use Psr\Log\LoggerInterface; |
| 18 | 18 | |
| 19 | 19 | class Movie extends ProviderV2 { |
| 20 | - private IConfig $config; |
|
| 21 | - |
|
| 22 | - private ?string $binary = null; |
|
| 23 | - |
|
| 24 | - public function __construct(array $options = []) { |
|
| 25 | - parent::__construct($options); |
|
| 26 | - $this->config = Server::get(IConfig::class); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function getMimeType(): string { |
|
| 30 | - return '/video\/.*/'; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * {@inheritDoc} |
|
| 35 | - */ |
|
| 36 | - public function isAvailable(FileInfo $file): bool { |
|
| 37 | - if (is_null($this->binary)) { |
|
| 38 | - if (isset($this->options['movieBinary'])) { |
|
| 39 | - $this->binary = $this->options['movieBinary']; |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - return is_string($this->binary); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * {@inheritDoc} |
|
| 47 | - */ |
|
| 48 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
| 49 | - // TODO: use proc_open() and stream the source file ? |
|
| 50 | - |
|
| 51 | - if (!$this->isAvailable($file)) { |
|
| 52 | - return null; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $result = null; |
|
| 56 | - if ($this->useTempFile($file)) { |
|
| 57 | - // try downloading 5 MB first as it's likely that the first frames are present there |
|
| 58 | - // in some cases this doesn't work for example when the moov atom is at the |
|
| 59 | - // end of the file, so if it fails we fall back to getting the full file |
|
| 60 | - $sizeAttempts = [5242880, null]; |
|
| 61 | - } else { |
|
| 62 | - // size is irrelevant, only attempt once |
|
| 63 | - $sizeAttempts = [null]; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - foreach ($sizeAttempts as $size) { |
|
| 67 | - $absPath = $this->getLocalFile($file, $size); |
|
| 68 | - if ($absPath === false) { |
|
| 69 | - Server::get(LoggerInterface::class)->error( |
|
| 70 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 71 | - ['app' => 'core'] |
|
| 72 | - ); |
|
| 73 | - return null; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); |
|
| 77 | - if ($result === null) { |
|
| 78 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); |
|
| 79 | - if ($result === null) { |
|
| 80 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $this->cleanTmpFiles(); |
|
| 85 | - |
|
| 86 | - if ($result !== null) { |
|
| 87 | - break; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $result; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private function useHdr(string $absPath): bool { |
|
| 95 | - // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
|
| 96 | - $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 97 | - // run ffprobe on the video file to get value of "color_transfer" |
|
| 98 | - $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 99 | - '-show_entries', 'stream=color_transfer', |
|
| 100 | - '-of', 'default=noprint_wrappers=1:nokey=1', |
|
| 101 | - $absPath]; |
|
| 102 | - $test_hdr_proc = proc_open($test_hdr_cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $test_hdr_pipes); |
|
| 103 | - if ($test_hdr_proc === false) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - $test_hdr_stdout = trim(stream_get_contents($test_hdr_pipes[1])); |
|
| 107 | - $test_hdr_stderr = trim(stream_get_contents($test_hdr_pipes[2])); |
|
| 108 | - proc_close($test_hdr_proc); |
|
| 109 | - // search build options for libzimg (provides zscale filter) |
|
| 110 | - $ffmpeg_libzimg_installed = strpos($test_hdr_stderr, '--enable-libzimg'); |
|
| 111 | - // Only values of "smpte2084" and "arib-std-b67" indicate an HDR video. |
|
| 112 | - // Only return true if video is detected as HDR and libzimg is installed. |
|
| 113 | - if (($test_hdr_stdout === 'smpte2084' || $test_hdr_stdout === 'arib-std-b67') && $ffmpeg_libzimg_installed !== false) { |
|
| 114 | - return true; |
|
| 115 | - } else { |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 121 | - $tmpPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 122 | - |
|
| 123 | - if ($tmpPath === false) { |
|
| 124 | - Server::get(LoggerInterface::class)->error( |
|
| 125 | - 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 126 | - ['app' => 'core'] |
|
| 127 | - ); |
|
| 128 | - return null; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 132 | - |
|
| 133 | - if ($binaryType === 'avconv') { |
|
| 134 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 135 | - '-i', $absPath, |
|
| 136 | - '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
|
| 137 | - $tmpPath]; |
|
| 138 | - } elseif ($binaryType === 'ffmpeg') { |
|
| 139 | - if ($this->useHdr($absPath)) { |
|
| 140 | - // Force colorspace to '2020_ncl' because some videos are |
|
| 141 | - // tagged incorrectly as 'reserved' resulting in fail if not forced. |
|
| 142 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 143 | - '-i', $absPath, |
|
| 144 | - '-f', 'mjpeg', '-vframes', '1', |
|
| 145 | - '-vf', 'zscale=min=2020_ncl:t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p', |
|
| 146 | - $tmpPath]; |
|
| 147 | - } else { |
|
| 148 | - // always default to generating preview using non-HDR command |
|
| 149 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 150 | - '-i', $absPath, |
|
| 151 | - '-f', 'mjpeg', '-vframes', '1', |
|
| 152 | - $tmpPath]; |
|
| 153 | - } |
|
| 154 | - } else { |
|
| 155 | - // Not supported |
|
| 156 | - unlink($tmpPath); |
|
| 157 | - return null; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 161 | - $returnCode = -1; |
|
| 162 | - $output = ''; |
|
| 163 | - if (is_resource($proc)) { |
|
| 164 | - $stdout = trim(stream_get_contents($pipes[1])); |
|
| 165 | - $stderr = trim(stream_get_contents($pipes[2])); |
|
| 166 | - $returnCode = proc_close($proc); |
|
| 167 | - $output = $stdout . $stderr; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if ($returnCode === 0) { |
|
| 171 | - $image = new \OCP\Image(); |
|
| 172 | - $image->loadFromFile($tmpPath); |
|
| 173 | - if ($image->valid()) { |
|
| 174 | - unlink($tmpPath); |
|
| 175 | - $image->scaleDownToFit($maxX, $maxY); |
|
| 176 | - |
|
| 177 | - return $image; |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - if ($second === 0) { |
|
| 182 | - $logger = Server::get(LoggerInterface::class); |
|
| 183 | - $logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - unlink($tmpPath); |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 20 | + private IConfig $config; |
|
| 21 | + |
|
| 22 | + private ?string $binary = null; |
|
| 23 | + |
|
| 24 | + public function __construct(array $options = []) { |
|
| 25 | + parent::__construct($options); |
|
| 26 | + $this->config = Server::get(IConfig::class); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function getMimeType(): string { |
|
| 30 | + return '/video\/.*/'; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * {@inheritDoc} |
|
| 35 | + */ |
|
| 36 | + public function isAvailable(FileInfo $file): bool { |
|
| 37 | + if (is_null($this->binary)) { |
|
| 38 | + if (isset($this->options['movieBinary'])) { |
|
| 39 | + $this->binary = $this->options['movieBinary']; |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + return is_string($this->binary); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * {@inheritDoc} |
|
| 47 | + */ |
|
| 48 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
| 49 | + // TODO: use proc_open() and stream the source file ? |
|
| 50 | + |
|
| 51 | + if (!$this->isAvailable($file)) { |
|
| 52 | + return null; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $result = null; |
|
| 56 | + if ($this->useTempFile($file)) { |
|
| 57 | + // try downloading 5 MB first as it's likely that the first frames are present there |
|
| 58 | + // in some cases this doesn't work for example when the moov atom is at the |
|
| 59 | + // end of the file, so if it fails we fall back to getting the full file |
|
| 60 | + $sizeAttempts = [5242880, null]; |
|
| 61 | + } else { |
|
| 62 | + // size is irrelevant, only attempt once |
|
| 63 | + $sizeAttempts = [null]; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + foreach ($sizeAttempts as $size) { |
|
| 67 | + $absPath = $this->getLocalFile($file, $size); |
|
| 68 | + if ($absPath === false) { |
|
| 69 | + Server::get(LoggerInterface::class)->error( |
|
| 70 | + 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 71 | + ['app' => 'core'] |
|
| 72 | + ); |
|
| 73 | + return null; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); |
|
| 77 | + if ($result === null) { |
|
| 78 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); |
|
| 79 | + if ($result === null) { |
|
| 80 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $this->cleanTmpFiles(); |
|
| 85 | + |
|
| 86 | + if ($result !== null) { |
|
| 87 | + break; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $result; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private function useHdr(string $absPath): bool { |
|
| 95 | + // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
|
| 96 | + $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 97 | + // run ffprobe on the video file to get value of "color_transfer" |
|
| 98 | + $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 99 | + '-show_entries', 'stream=color_transfer', |
|
| 100 | + '-of', 'default=noprint_wrappers=1:nokey=1', |
|
| 101 | + $absPath]; |
|
| 102 | + $test_hdr_proc = proc_open($test_hdr_cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $test_hdr_pipes); |
|
| 103 | + if ($test_hdr_proc === false) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + $test_hdr_stdout = trim(stream_get_contents($test_hdr_pipes[1])); |
|
| 107 | + $test_hdr_stderr = trim(stream_get_contents($test_hdr_pipes[2])); |
|
| 108 | + proc_close($test_hdr_proc); |
|
| 109 | + // search build options for libzimg (provides zscale filter) |
|
| 110 | + $ffmpeg_libzimg_installed = strpos($test_hdr_stderr, '--enable-libzimg'); |
|
| 111 | + // Only values of "smpte2084" and "arib-std-b67" indicate an HDR video. |
|
| 112 | + // Only return true if video is detected as HDR and libzimg is installed. |
|
| 113 | + if (($test_hdr_stdout === 'smpte2084' || $test_hdr_stdout === 'arib-std-b67') && $ffmpeg_libzimg_installed !== false) { |
|
| 114 | + return true; |
|
| 115 | + } else { |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 121 | + $tmpPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 122 | + |
|
| 123 | + if ($tmpPath === false) { |
|
| 124 | + Server::get(LoggerInterface::class)->error( |
|
| 125 | + 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 126 | + ['app' => 'core'] |
|
| 127 | + ); |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 132 | + |
|
| 133 | + if ($binaryType === 'avconv') { |
|
| 134 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 135 | + '-i', $absPath, |
|
| 136 | + '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
|
| 137 | + $tmpPath]; |
|
| 138 | + } elseif ($binaryType === 'ffmpeg') { |
|
| 139 | + if ($this->useHdr($absPath)) { |
|
| 140 | + // Force colorspace to '2020_ncl' because some videos are |
|
| 141 | + // tagged incorrectly as 'reserved' resulting in fail if not forced. |
|
| 142 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 143 | + '-i', $absPath, |
|
| 144 | + '-f', 'mjpeg', '-vframes', '1', |
|
| 145 | + '-vf', 'zscale=min=2020_ncl:t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p', |
|
| 146 | + $tmpPath]; |
|
| 147 | + } else { |
|
| 148 | + // always default to generating preview using non-HDR command |
|
| 149 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 150 | + '-i', $absPath, |
|
| 151 | + '-f', 'mjpeg', '-vframes', '1', |
|
| 152 | + $tmpPath]; |
|
| 153 | + } |
|
| 154 | + } else { |
|
| 155 | + // Not supported |
|
| 156 | + unlink($tmpPath); |
|
| 157 | + return null; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 161 | + $returnCode = -1; |
|
| 162 | + $output = ''; |
|
| 163 | + if (is_resource($proc)) { |
|
| 164 | + $stdout = trim(stream_get_contents($pipes[1])); |
|
| 165 | + $stderr = trim(stream_get_contents($pipes[2])); |
|
| 166 | + $returnCode = proc_close($proc); |
|
| 167 | + $output = $stdout . $stderr; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if ($returnCode === 0) { |
|
| 171 | + $image = new \OCP\Image(); |
|
| 172 | + $image->loadFromFile($tmpPath); |
|
| 173 | + if ($image->valid()) { |
|
| 174 | + unlink($tmpPath); |
|
| 175 | + $image->scaleDownToFit($maxX, $maxY); |
|
| 176 | + |
|
| 177 | + return $image; |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + if ($second === 0) { |
|
| 182 | + $logger = Server::get(LoggerInterface::class); |
|
| 183 | + $logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + unlink($tmpPath); |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | 189 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | $absPath = $this->getLocalFile($file, $size); |
| 68 | 68 | if ($absPath === false) { |
| 69 | 69 | Server::get(LoggerInterface::class)->error( |
| 70 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 70 | + 'Failed to get local file to generate thumbnail for: '.$file->getPath(), |
|
| 71 | 71 | ['app' => 'core'] |
| 72 | 72 | ); |
| 73 | 73 | return null; |
@@ -93,9 +93,9 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | private function useHdr(string $absPath): bool { |
| 95 | 95 | // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
| 96 | - $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 96 | + $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME).'/ffprobe'); |
|
| 97 | 97 | // run ffprobe on the video file to get value of "color_transfer" |
| 98 | - $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 98 | + $test_hdr_cmd = [$ffprobe_binary, '-select_streams', 'v:0', |
|
| 99 | 99 | '-show_entries', 'stream=color_transfer', |
| 100 | 100 | '-of', 'default=noprint_wrappers=1:nokey=1', |
| 101 | 101 | $absPath]; |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | if ($tmpPath === false) { |
| 124 | 124 | Server::get(LoggerInterface::class)->error( |
| 125 | - 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 125 | + 'Failed to get local file to generate thumbnail for: '.$absPath, |
|
| 126 | 126 | ['app' => 'core'] |
| 127 | 127 | ); |
| 128 | 128 | return null; |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | $binaryType = substr(strrchr($this->binary, '/'), 1); |
| 132 | 132 | |
| 133 | 133 | if ($binaryType === 'avconv') { |
| 134 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 134 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 135 | 135 | '-i', $absPath, |
| 136 | 136 | '-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1', |
| 137 | 137 | $tmpPath]; |
@@ -139,14 +139,14 @@ discard block |
||
| 139 | 139 | if ($this->useHdr($absPath)) { |
| 140 | 140 | // Force colorspace to '2020_ncl' because some videos are |
| 141 | 141 | // tagged incorrectly as 'reserved' resulting in fail if not forced. |
| 142 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 142 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 143 | 143 | '-i', $absPath, |
| 144 | 144 | '-f', 'mjpeg', '-vframes', '1', |
| 145 | 145 | '-vf', 'zscale=min=2020_ncl:t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p', |
| 146 | 146 | $tmpPath]; |
| 147 | 147 | } else { |
| 148 | 148 | // always default to generating preview using non-HDR command |
| 149 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 149 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 150 | 150 | '-i', $absPath, |
| 151 | 151 | '-f', 'mjpeg', '-vframes', '1', |
| 152 | 152 | $tmpPath]; |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | $stdout = trim(stream_get_contents($pipes[1])); |
| 165 | 165 | $stderr = trim(stream_get_contents($pipes[2])); |
| 166 | 166 | $returnCode = proc_close($proc); |
| 167 | - $output = $stdout . $stderr; |
|
| 167 | + $output = $stdout.$stderr; |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | if ($returnCode === 0) { |