@@ -17,289 +17,289 @@ |
||
| 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 | - |
|
| 57 | - // Timestamps to make attempts to generate a still |
|
| 58 | - $timeAttempts = [5, 1, 0]; |
|
| 59 | - |
|
| 60 | - // By default, download $sizeAttempts from the file along with |
|
| 61 | - // the 'moov' atom. |
|
| 62 | - // Example bitrates in the higher range: |
|
| 63 | - // 4K HDR H265 60 FPS = 75 Mbps = 9 MB per second needed for a still |
|
| 64 | - // 1080p H265 30 FPS = 10 Mbps = 1.25 MB per second needed for a still |
|
| 65 | - // 1080p H264 30 FPS = 16 Mbps = 2 MB per second needed for a still |
|
| 66 | - $sizeAttempts = [1024 * 1024 * 10]; |
|
| 67 | - |
|
| 68 | - if ($this->useTempFile($file)) { |
|
| 69 | - if ($file->getStorage()->isLocal()) { |
|
| 70 | - // Temp file required but file is local, so retrieve $sizeAttempt bytes first, |
|
| 71 | - // and if it doesn't work, retrieve the entire file. |
|
| 72 | - $sizeAttempts[] = null; |
|
| 73 | - } |
|
| 74 | - } else { |
|
| 75 | - // Temp file is not required and file is local so retrieve entire file. |
|
| 76 | - $sizeAttempts = [null]; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - foreach ($sizeAttempts as $size) { |
|
| 80 | - $absPath = false; |
|
| 81 | - // File is remote, generate a sparse file |
|
| 82 | - if (!$file->getStorage()->isLocal()) { |
|
| 83 | - $absPath = $this->getSparseFile($file, $size); |
|
| 84 | - } |
|
| 85 | - // Defaults to existing routine if generating sparse file fails |
|
| 86 | - if ($absPath === false) { |
|
| 87 | - $absPath = $this->getLocalFile($file, $size); |
|
| 88 | - } |
|
| 89 | - if ($absPath === false) { |
|
| 90 | - Server::get(LoggerInterface::class)->error( |
|
| 91 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 92 | - ['app' => 'core'] |
|
| 93 | - ); |
|
| 94 | - return null; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Attempt still image grabs from selected timestamps |
|
| 98 | - foreach ($timeAttempts as $timeStamp) { |
|
| 99 | - $result = $this->generateThumbNail($maxX, $maxY, $absPath, $timeStamp); |
|
| 100 | - if ($result !== null) { |
|
| 101 | - break; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->cleanTmpFiles(); |
|
| 106 | - |
|
| 107 | - if ($result !== null) { |
|
| 108 | - break; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - return $result; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - private function getSparseFile(File $file, int $size): string|false { |
|
| 115 | - // File is smaller than $size or file is larger than max int size |
|
| 116 | - // of the host so return false so getLocalFile method is used |
|
| 117 | - if (($size >= $file->getSize()) || ($file->getSize() > PHP_INT_MAX)) { |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - $content = $file->fopen('r'); |
|
| 121 | - |
|
| 122 | - // Stream does not support seeking so generating a sparse file is not possible. |
|
| 123 | - if (stream_get_meta_data($content)['seekable'] !== true) { |
|
| 124 | - fclose($content); |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 129 | - if ($absPath === false) { |
|
| 130 | - Server::get(LoggerInterface::class)->error( |
|
| 131 | - 'Failed to get sparse file to generate thumbnail: ' . $file->getPath(), |
|
| 132 | - ['app' => 'core'] |
|
| 133 | - ); |
|
| 134 | - fclose($content); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - $sparseFile = fopen($absPath, 'w'); |
|
| 138 | - |
|
| 139 | - // Firsts 4 bytes indicate length of 1st atom. |
|
| 140 | - $ftypSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, 0))); |
|
| 141 | - // Download next 4 bytes to find name of 1st atom. |
|
| 142 | - $ftypLabel = stream_get_contents($content, 4, 4); |
|
| 143 | - |
|
| 144 | - // MP4/MOVs all begin with the 'ftyp' atom. Anything else is not MP4/MOV |
|
| 145 | - // and therefore should be processed differently. |
|
| 146 | - if ($ftypLabel === 'ftyp') { |
|
| 147 | - // Set offset for 2nd atom. Atoms begin where the previous one ends. |
|
| 148 | - $offset = $ftypSize; |
|
| 149 | - $moovSize = 0; |
|
| 150 | - $moovOffset = 0; |
|
| 151 | - // Iterate and seek from atom to until the 'moov' atom is found or |
|
| 152 | - // EOF is reached |
|
| 153 | - while (($offset + 8 < $file->getSize()) && ($moovSize === 0)) { |
|
| 154 | - // First 4 bytes of atom header indicates size of the atom. |
|
| 155 | - $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, (int)$offset))); |
|
| 156 | - // Next 4 bytes of atom header is the name/label of the atom |
|
| 157 | - $atomLabel = stream_get_contents($content, 4, (int)($offset + 4)); |
|
| 158 | - // Size value has two special values that don't directly indicate size |
|
| 159 | - // 0 = atom size equals the rest of the file |
|
| 160 | - if ($atomSize === 0) { |
|
| 161 | - $atomSize = $file->getsize() - $offset; |
|
| 162 | - } else { |
|
| 163 | - // 1 = read an additional 8 bytes after the label to get the 64 bit |
|
| 164 | - // size of the atom. Needed for large atoms like 'mdat' (the video data) |
|
| 165 | - if ($atomSize === 1) { |
|
| 166 | - $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 8, (int)($offset + 8)))); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - // Found the 'moov' atom, store its location and size |
|
| 170 | - if ($atomLabel === 'moov') { |
|
| 171 | - $moovSize = $atomSize; |
|
| 172 | - $moovOffset = $offset; |
|
| 173 | - break; |
|
| 174 | - } |
|
| 175 | - $offset += $atomSize; |
|
| 176 | - } |
|
| 177 | - // 'moov' atom wasn't found or larger than $size |
|
| 178 | - // 'moov' atoms are generally small relative to video length. |
|
| 179 | - // Examples: |
|
| 180 | - // 4K HDR H265 60 FPS, 10 second video = 12.5 KB 'moov' atom, 54 MB total file size |
|
| 181 | - // 4K HDR H265 60 FPS, 5 minute video = 330 KB 'moov' atom, 1.95 GB total file size |
|
| 182 | - // Capping it at $size is a precaution against a corrupt/malicious 'moov' atom. |
|
| 183 | - // This effectively caps the total download size to 2x $size. |
|
| 184 | - // Also, if the 'moov' atom size+offset extends past EOF, it is invalid. |
|
| 185 | - if (($moovSize === 0) || ($moovSize > $size) || ($moovOffset + $moovSize > $file->getSize())) { |
|
| 186 | - fclose($content); |
|
| 187 | - fclose($sparseFile); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - // Generate new file of same size |
|
| 191 | - ftruncate($sparseFile, (int)($file->getSize())); |
|
| 192 | - fseek($sparseFile, 0); |
|
| 193 | - fseek($content, 0); |
|
| 194 | - // Copy first $size bytes of video into new file |
|
| 195 | - stream_copy_to_stream($content, $sparseFile, $size, 0); |
|
| 196 | - |
|
| 197 | - // If 'moov' is located before $size in the video, it was already streamed, |
|
| 198 | - // so no need to download it again. |
|
| 199 | - if ($moovOffset >= $size) { |
|
| 200 | - // Seek to where 'moov' atom needs to be placed |
|
| 201 | - fseek($content, (int)$moovOffset); |
|
| 202 | - fseek($sparseFile, (int)$moovOffset); |
|
| 203 | - stream_copy_to_stream($content, $sparseFile, (int)$moovSize, 0); |
|
| 204 | - } |
|
| 205 | - } else { |
|
| 206 | - // 'ftyp' atom not found, not a valid MP4/MOV |
|
| 207 | - fclose($content); |
|
| 208 | - fclose($sparseFile); |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - fclose($content); |
|
| 212 | - fclose($sparseFile); |
|
| 213 | - return $absPath; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - private function useHdr(string $absPath): bool { |
|
| 217 | - // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
|
| 218 | - $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 219 | - // run ffprobe on the video file to get value of "color_transfer" |
|
| 220 | - $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 221 | - '-show_entries', 'stream=color_transfer', |
|
| 222 | - '-of', 'default=noprint_wrappers=1:nokey=1', |
|
| 223 | - $absPath]; |
|
| 224 | - $test_hdr_proc = proc_open($test_hdr_cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $test_hdr_pipes); |
|
| 225 | - if ($test_hdr_proc === false) { |
|
| 226 | - return false; |
|
| 227 | - } |
|
| 228 | - $test_hdr_stdout = trim(stream_get_contents($test_hdr_pipes[1])); |
|
| 229 | - $test_hdr_stderr = trim(stream_get_contents($test_hdr_pipes[2])); |
|
| 230 | - proc_close($test_hdr_proc); |
|
| 231 | - // search build options for libzimg (provides zscale filter) |
|
| 232 | - $ffmpeg_libzimg_installed = strpos($test_hdr_stderr, '--enable-libzimg'); |
|
| 233 | - // Only values of "smpte2084" and "arib-std-b67" indicate an HDR video. |
|
| 234 | - // Only return true if video is detected as HDR and libzimg is installed. |
|
| 235 | - if (($test_hdr_stdout === 'smpte2084' || $test_hdr_stdout === 'arib-std-b67') && $ffmpeg_libzimg_installed !== false) { |
|
| 236 | - return true; |
|
| 237 | - } else { |
|
| 238 | - return false; |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 243 | - $tmpPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 244 | - if ($tmpPath === false) { |
|
| 245 | - Server::get(LoggerInterface::class)->error( |
|
| 246 | - 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 247 | - ['app' => 'core'] |
|
| 248 | - ); |
|
| 249 | - return null; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 253 | - |
|
| 254 | - if ($binaryType === 'ffmpeg') { |
|
| 255 | - if ($this->useHdr($absPath)) { |
|
| 256 | - // Force colorspace to '2020_ncl' because some videos are |
|
| 257 | - // tagged incorrectly as 'reserved' resulting in fail if not forced. |
|
| 258 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 259 | - '-i', $absPath, |
|
| 260 | - '-f', 'mjpeg', '-vframes', '1', |
|
| 261 | - '-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', |
|
| 262 | - $tmpPath]; |
|
| 263 | - } else { |
|
| 264 | - // always default to generating preview using non-HDR command |
|
| 265 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 266 | - '-i', $absPath, |
|
| 267 | - '-f', 'mjpeg', '-vframes', '1', |
|
| 268 | - $tmpPath]; |
|
| 269 | - } |
|
| 270 | - } else { |
|
| 271 | - // Not supported |
|
| 272 | - unlink($tmpPath); |
|
| 273 | - return null; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 277 | - $returnCode = -1; |
|
| 278 | - $output = ''; |
|
| 279 | - if (is_resource($proc)) { |
|
| 280 | - $stderr = trim(stream_get_contents($pipes[2])); |
|
| 281 | - $stdout = trim(stream_get_contents($pipes[1])); |
|
| 282 | - $returnCode = proc_close($proc); |
|
| 283 | - $output = $stdout . $stderr; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - if ($returnCode === 0) { |
|
| 287 | - $image = new \OCP\Image(); |
|
| 288 | - $image->loadFromFile($tmpPath); |
|
| 289 | - if ($image->valid()) { |
|
| 290 | - unlink($tmpPath); |
|
| 291 | - $image->scaleDownToFit($maxX, $maxY); |
|
| 292 | - |
|
| 293 | - return $image; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if ($second === 0) { |
|
| 298 | - $logger = Server::get(LoggerInterface::class); |
|
| 299 | - $logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - unlink($tmpPath); |
|
| 303 | - return null; |
|
| 304 | - } |
|
| 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 | + |
|
| 57 | + // Timestamps to make attempts to generate a still |
|
| 58 | + $timeAttempts = [5, 1, 0]; |
|
| 59 | + |
|
| 60 | + // By default, download $sizeAttempts from the file along with |
|
| 61 | + // the 'moov' atom. |
|
| 62 | + // Example bitrates in the higher range: |
|
| 63 | + // 4K HDR H265 60 FPS = 75 Mbps = 9 MB per second needed for a still |
|
| 64 | + // 1080p H265 30 FPS = 10 Mbps = 1.25 MB per second needed for a still |
|
| 65 | + // 1080p H264 30 FPS = 16 Mbps = 2 MB per second needed for a still |
|
| 66 | + $sizeAttempts = [1024 * 1024 * 10]; |
|
| 67 | + |
|
| 68 | + if ($this->useTempFile($file)) { |
|
| 69 | + if ($file->getStorage()->isLocal()) { |
|
| 70 | + // Temp file required but file is local, so retrieve $sizeAttempt bytes first, |
|
| 71 | + // and if it doesn't work, retrieve the entire file. |
|
| 72 | + $sizeAttempts[] = null; |
|
| 73 | + } |
|
| 74 | + } else { |
|
| 75 | + // Temp file is not required and file is local so retrieve entire file. |
|
| 76 | + $sizeAttempts = [null]; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + foreach ($sizeAttempts as $size) { |
|
| 80 | + $absPath = false; |
|
| 81 | + // File is remote, generate a sparse file |
|
| 82 | + if (!$file->getStorage()->isLocal()) { |
|
| 83 | + $absPath = $this->getSparseFile($file, $size); |
|
| 84 | + } |
|
| 85 | + // Defaults to existing routine if generating sparse file fails |
|
| 86 | + if ($absPath === false) { |
|
| 87 | + $absPath = $this->getLocalFile($file, $size); |
|
| 88 | + } |
|
| 89 | + if ($absPath === false) { |
|
| 90 | + Server::get(LoggerInterface::class)->error( |
|
| 91 | + 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 92 | + ['app' => 'core'] |
|
| 93 | + ); |
|
| 94 | + return null; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Attempt still image grabs from selected timestamps |
|
| 98 | + foreach ($timeAttempts as $timeStamp) { |
|
| 99 | + $result = $this->generateThumbNail($maxX, $maxY, $absPath, $timeStamp); |
|
| 100 | + if ($result !== null) { |
|
| 101 | + break; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->cleanTmpFiles(); |
|
| 106 | + |
|
| 107 | + if ($result !== null) { |
|
| 108 | + break; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + return $result; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + private function getSparseFile(File $file, int $size): string|false { |
|
| 115 | + // File is smaller than $size or file is larger than max int size |
|
| 116 | + // of the host so return false so getLocalFile method is used |
|
| 117 | + if (($size >= $file->getSize()) || ($file->getSize() > PHP_INT_MAX)) { |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + $content = $file->fopen('r'); |
|
| 121 | + |
|
| 122 | + // Stream does not support seeking so generating a sparse file is not possible. |
|
| 123 | + if (stream_get_meta_data($content)['seekable'] !== true) { |
|
| 124 | + fclose($content); |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 129 | + if ($absPath === false) { |
|
| 130 | + Server::get(LoggerInterface::class)->error( |
|
| 131 | + 'Failed to get sparse file to generate thumbnail: ' . $file->getPath(), |
|
| 132 | + ['app' => 'core'] |
|
| 133 | + ); |
|
| 134 | + fclose($content); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + $sparseFile = fopen($absPath, 'w'); |
|
| 138 | + |
|
| 139 | + // Firsts 4 bytes indicate length of 1st atom. |
|
| 140 | + $ftypSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, 0))); |
|
| 141 | + // Download next 4 bytes to find name of 1st atom. |
|
| 142 | + $ftypLabel = stream_get_contents($content, 4, 4); |
|
| 143 | + |
|
| 144 | + // MP4/MOVs all begin with the 'ftyp' atom. Anything else is not MP4/MOV |
|
| 145 | + // and therefore should be processed differently. |
|
| 146 | + if ($ftypLabel === 'ftyp') { |
|
| 147 | + // Set offset for 2nd atom. Atoms begin where the previous one ends. |
|
| 148 | + $offset = $ftypSize; |
|
| 149 | + $moovSize = 0; |
|
| 150 | + $moovOffset = 0; |
|
| 151 | + // Iterate and seek from atom to until the 'moov' atom is found or |
|
| 152 | + // EOF is reached |
|
| 153 | + while (($offset + 8 < $file->getSize()) && ($moovSize === 0)) { |
|
| 154 | + // First 4 bytes of atom header indicates size of the atom. |
|
| 155 | + $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, (int)$offset))); |
|
| 156 | + // Next 4 bytes of atom header is the name/label of the atom |
|
| 157 | + $atomLabel = stream_get_contents($content, 4, (int)($offset + 4)); |
|
| 158 | + // Size value has two special values that don't directly indicate size |
|
| 159 | + // 0 = atom size equals the rest of the file |
|
| 160 | + if ($atomSize === 0) { |
|
| 161 | + $atomSize = $file->getsize() - $offset; |
|
| 162 | + } else { |
|
| 163 | + // 1 = read an additional 8 bytes after the label to get the 64 bit |
|
| 164 | + // size of the atom. Needed for large atoms like 'mdat' (the video data) |
|
| 165 | + if ($atomSize === 1) { |
|
| 166 | + $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 8, (int)($offset + 8)))); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + // Found the 'moov' atom, store its location and size |
|
| 170 | + if ($atomLabel === 'moov') { |
|
| 171 | + $moovSize = $atomSize; |
|
| 172 | + $moovOffset = $offset; |
|
| 173 | + break; |
|
| 174 | + } |
|
| 175 | + $offset += $atomSize; |
|
| 176 | + } |
|
| 177 | + // 'moov' atom wasn't found or larger than $size |
|
| 178 | + // 'moov' atoms are generally small relative to video length. |
|
| 179 | + // Examples: |
|
| 180 | + // 4K HDR H265 60 FPS, 10 second video = 12.5 KB 'moov' atom, 54 MB total file size |
|
| 181 | + // 4K HDR H265 60 FPS, 5 minute video = 330 KB 'moov' atom, 1.95 GB total file size |
|
| 182 | + // Capping it at $size is a precaution against a corrupt/malicious 'moov' atom. |
|
| 183 | + // This effectively caps the total download size to 2x $size. |
|
| 184 | + // Also, if the 'moov' atom size+offset extends past EOF, it is invalid. |
|
| 185 | + if (($moovSize === 0) || ($moovSize > $size) || ($moovOffset + $moovSize > $file->getSize())) { |
|
| 186 | + fclose($content); |
|
| 187 | + fclose($sparseFile); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + // Generate new file of same size |
|
| 191 | + ftruncate($sparseFile, (int)($file->getSize())); |
|
| 192 | + fseek($sparseFile, 0); |
|
| 193 | + fseek($content, 0); |
|
| 194 | + // Copy first $size bytes of video into new file |
|
| 195 | + stream_copy_to_stream($content, $sparseFile, $size, 0); |
|
| 196 | + |
|
| 197 | + // If 'moov' is located before $size in the video, it was already streamed, |
|
| 198 | + // so no need to download it again. |
|
| 199 | + if ($moovOffset >= $size) { |
|
| 200 | + // Seek to where 'moov' atom needs to be placed |
|
| 201 | + fseek($content, (int)$moovOffset); |
|
| 202 | + fseek($sparseFile, (int)$moovOffset); |
|
| 203 | + stream_copy_to_stream($content, $sparseFile, (int)$moovSize, 0); |
|
| 204 | + } |
|
| 205 | + } else { |
|
| 206 | + // 'ftyp' atom not found, not a valid MP4/MOV |
|
| 207 | + fclose($content); |
|
| 208 | + fclose($sparseFile); |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + fclose($content); |
|
| 212 | + fclose($sparseFile); |
|
| 213 | + return $absPath; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + private function useHdr(string $absPath): bool { |
|
| 217 | + // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
|
| 218 | + $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 219 | + // run ffprobe on the video file to get value of "color_transfer" |
|
| 220 | + $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 221 | + '-show_entries', 'stream=color_transfer', |
|
| 222 | + '-of', 'default=noprint_wrappers=1:nokey=1', |
|
| 223 | + $absPath]; |
|
| 224 | + $test_hdr_proc = proc_open($test_hdr_cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $test_hdr_pipes); |
|
| 225 | + if ($test_hdr_proc === false) { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 228 | + $test_hdr_stdout = trim(stream_get_contents($test_hdr_pipes[1])); |
|
| 229 | + $test_hdr_stderr = trim(stream_get_contents($test_hdr_pipes[2])); |
|
| 230 | + proc_close($test_hdr_proc); |
|
| 231 | + // search build options for libzimg (provides zscale filter) |
|
| 232 | + $ffmpeg_libzimg_installed = strpos($test_hdr_stderr, '--enable-libzimg'); |
|
| 233 | + // Only values of "smpte2084" and "arib-std-b67" indicate an HDR video. |
|
| 234 | + // Only return true if video is detected as HDR and libzimg is installed. |
|
| 235 | + if (($test_hdr_stdout === 'smpte2084' || $test_hdr_stdout === 'arib-std-b67') && $ffmpeg_libzimg_installed !== false) { |
|
| 236 | + return true; |
|
| 237 | + } else { |
|
| 238 | + return false; |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage { |
|
| 243 | + $tmpPath = Server::get(ITempManager::class)->getTemporaryFile(); |
|
| 244 | + if ($tmpPath === false) { |
|
| 245 | + Server::get(LoggerInterface::class)->error( |
|
| 246 | + 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 247 | + ['app' => 'core'] |
|
| 248 | + ); |
|
| 249 | + return null; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $binaryType = substr(strrchr($this->binary, '/'), 1); |
|
| 253 | + |
|
| 254 | + if ($binaryType === 'ffmpeg') { |
|
| 255 | + if ($this->useHdr($absPath)) { |
|
| 256 | + // Force colorspace to '2020_ncl' because some videos are |
|
| 257 | + // tagged incorrectly as 'reserved' resulting in fail if not forced. |
|
| 258 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 259 | + '-i', $absPath, |
|
| 260 | + '-f', 'mjpeg', '-vframes', '1', |
|
| 261 | + '-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', |
|
| 262 | + $tmpPath]; |
|
| 263 | + } else { |
|
| 264 | + // always default to generating preview using non-HDR command |
|
| 265 | + $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 266 | + '-i', $absPath, |
|
| 267 | + '-f', 'mjpeg', '-vframes', '1', |
|
| 268 | + $tmpPath]; |
|
| 269 | + } |
|
| 270 | + } else { |
|
| 271 | + // Not supported |
|
| 272 | + unlink($tmpPath); |
|
| 273 | + return null; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); |
|
| 277 | + $returnCode = -1; |
|
| 278 | + $output = ''; |
|
| 279 | + if (is_resource($proc)) { |
|
| 280 | + $stderr = trim(stream_get_contents($pipes[2])); |
|
| 281 | + $stdout = trim(stream_get_contents($pipes[1])); |
|
| 282 | + $returnCode = proc_close($proc); |
|
| 283 | + $output = $stdout . $stderr; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + if ($returnCode === 0) { |
|
| 287 | + $image = new \OCP\Image(); |
|
| 288 | + $image->loadFromFile($tmpPath); |
|
| 289 | + if ($image->valid()) { |
|
| 290 | + unlink($tmpPath); |
|
| 291 | + $image->scaleDownToFit($maxX, $maxY); |
|
| 292 | + |
|
| 293 | + return $image; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if ($second === 0) { |
|
| 298 | + $logger = Server::get(LoggerInterface::class); |
|
| 299 | + $logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + unlink($tmpPath); |
|
| 303 | + return null; |
|
| 304 | + } |
|
| 305 | 305 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | if ($absPath === false) { |
| 90 | 90 | Server::get(LoggerInterface::class)->error( |
| 91 | - 'Failed to get local file to generate thumbnail for: ' . $file->getPath(), |
|
| 91 | + 'Failed to get local file to generate thumbnail for: '.$file->getPath(), |
|
| 92 | 92 | ['app' => 'core'] |
| 93 | 93 | ); |
| 94 | 94 | return null; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | return $result; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - private function getSparseFile(File $file, int $size): string|false { |
|
| 114 | + private function getSparseFile(File $file, int $size): string | false { |
|
| 115 | 115 | // File is smaller than $size or file is larger than max int size |
| 116 | 116 | // of the host so return false so getLocalFile method is used |
| 117 | 117 | if (($size >= $file->getSize()) || ($file->getSize() > PHP_INT_MAX)) { |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | $absPath = Server::get(ITempManager::class)->getTemporaryFile(); |
| 129 | 129 | if ($absPath === false) { |
| 130 | 130 | Server::get(LoggerInterface::class)->error( |
| 131 | - 'Failed to get sparse file to generate thumbnail: ' . $file->getPath(), |
|
| 131 | + 'Failed to get sparse file to generate thumbnail: '.$file->getPath(), |
|
| 132 | 132 | ['app' => 'core'] |
| 133 | 133 | ); |
| 134 | 134 | fclose($content); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | $sparseFile = fopen($absPath, 'w'); |
| 138 | 138 | |
| 139 | 139 | // Firsts 4 bytes indicate length of 1st atom. |
| 140 | - $ftypSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, 0))); |
|
| 140 | + $ftypSize = (int) hexdec(bin2hex(stream_get_contents($content, 4, 0))); |
|
| 141 | 141 | // Download next 4 bytes to find name of 1st atom. |
| 142 | 142 | $ftypLabel = stream_get_contents($content, 4, 4); |
| 143 | 143 | |
@@ -152,9 +152,9 @@ discard block |
||
| 152 | 152 | // EOF is reached |
| 153 | 153 | while (($offset + 8 < $file->getSize()) && ($moovSize === 0)) { |
| 154 | 154 | // First 4 bytes of atom header indicates size of the atom. |
| 155 | - $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 4, (int)$offset))); |
|
| 155 | + $atomSize = (int) hexdec(bin2hex(stream_get_contents($content, 4, (int) $offset))); |
|
| 156 | 156 | // Next 4 bytes of atom header is the name/label of the atom |
| 157 | - $atomLabel = stream_get_contents($content, 4, (int)($offset + 4)); |
|
| 157 | + $atomLabel = stream_get_contents($content, 4, (int) ($offset + 4)); |
|
| 158 | 158 | // Size value has two special values that don't directly indicate size |
| 159 | 159 | // 0 = atom size equals the rest of the file |
| 160 | 160 | if ($atomSize === 0) { |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | // 1 = read an additional 8 bytes after the label to get the 64 bit |
| 164 | 164 | // size of the atom. Needed for large atoms like 'mdat' (the video data) |
| 165 | 165 | if ($atomSize === 1) { |
| 166 | - $atomSize = (int)hexdec(bin2hex(stream_get_contents($content, 8, (int)($offset + 8)))); |
|
| 166 | + $atomSize = (int) hexdec(bin2hex(stream_get_contents($content, 8, (int) ($offset + 8)))); |
|
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | 169 | // Found the 'moov' atom, store its location and size |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | return false; |
| 189 | 189 | } |
| 190 | 190 | // Generate new file of same size |
| 191 | - ftruncate($sparseFile, (int)($file->getSize())); |
|
| 191 | + ftruncate($sparseFile, (int) ($file->getSize())); |
|
| 192 | 192 | fseek($sparseFile, 0); |
| 193 | 193 | fseek($content, 0); |
| 194 | 194 | // Copy first $size bytes of video into new file |
@@ -198,9 +198,9 @@ discard block |
||
| 198 | 198 | // so no need to download it again. |
| 199 | 199 | if ($moovOffset >= $size) { |
| 200 | 200 | // Seek to where 'moov' atom needs to be placed |
| 201 | - fseek($content, (int)$moovOffset); |
|
| 202 | - fseek($sparseFile, (int)$moovOffset); |
|
| 203 | - stream_copy_to_stream($content, $sparseFile, (int)$moovSize, 0); |
|
| 201 | + fseek($content, (int) $moovOffset); |
|
| 202 | + fseek($sparseFile, (int) $moovOffset); |
|
| 203 | + stream_copy_to_stream($content, $sparseFile, (int) $moovSize, 0); |
|
| 204 | 204 | } |
| 205 | 205 | } else { |
| 206 | 206 | // 'ftyp' atom not found, not a valid MP4/MOV |
@@ -215,9 +215,9 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | private function useHdr(string $absPath): bool { |
| 217 | 217 | // load ffprobe path from configuration, otherwise generate binary path using ffmpeg binary path |
| 218 | - $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME) . '/ffprobe'); |
|
| 218 | + $ffprobe_binary = $this->config->getSystemValue('preview_ffprobe_path', null) ?? (pathinfo($this->binary, PATHINFO_DIRNAME).'/ffprobe'); |
|
| 219 | 219 | // run ffprobe on the video file to get value of "color_transfer" |
| 220 | - $test_hdr_cmd = [$ffprobe_binary,'-select_streams', 'v:0', |
|
| 220 | + $test_hdr_cmd = [$ffprobe_binary, '-select_streams', 'v:0', |
|
| 221 | 221 | '-show_entries', 'stream=color_transfer', |
| 222 | 222 | '-of', 'default=noprint_wrappers=1:nokey=1', |
| 223 | 223 | $absPath]; |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | $tmpPath = Server::get(ITempManager::class)->getTemporaryFile(); |
| 244 | 244 | if ($tmpPath === false) { |
| 245 | 245 | Server::get(LoggerInterface::class)->error( |
| 246 | - 'Failed to get local file to generate thumbnail for: ' . $absPath, |
|
| 246 | + 'Failed to get local file to generate thumbnail for: '.$absPath, |
|
| 247 | 247 | ['app' => 'core'] |
| 248 | 248 | ); |
| 249 | 249 | return null; |
@@ -255,14 +255,14 @@ discard block |
||
| 255 | 255 | if ($this->useHdr($absPath)) { |
| 256 | 256 | // Force colorspace to '2020_ncl' because some videos are |
| 257 | 257 | // tagged incorrectly as 'reserved' resulting in fail if not forced. |
| 258 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 258 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 259 | 259 | '-i', $absPath, |
| 260 | 260 | '-f', 'mjpeg', '-vframes', '1', |
| 261 | 261 | '-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', |
| 262 | 262 | $tmpPath]; |
| 263 | 263 | } else { |
| 264 | 264 | // always default to generating preview using non-HDR command |
| 265 | - $cmd = [$this->binary, '-y', '-ss', (string)$second, |
|
| 265 | + $cmd = [$this->binary, '-y', '-ss', (string) $second, |
|
| 266 | 266 | '-i', $absPath, |
| 267 | 267 | '-f', 'mjpeg', '-vframes', '1', |
| 268 | 268 | $tmpPath]; |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | $stderr = trim(stream_get_contents($pipes[2])); |
| 281 | 281 | $stdout = trim(stream_get_contents($pipes[1])); |
| 282 | 282 | $returnCode = proc_close($proc); |
| 283 | - $output = $stdout . $stderr; |
|
| 283 | + $output = $stdout.$stderr; |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | if ($returnCode === 0) { |
@@ -23,45 +23,45 @@ |
||
| 23 | 23 | * @package Test\Preview |
| 24 | 24 | */ |
| 25 | 25 | class MovieTestRemoteFile extends Provider { |
| 26 | - // 1080p (1920x1080) 30 FPS HEVC/H264, 10 secs, avg. bitrate: ~10 Mbps |
|
| 27 | - protected string $fileName = 'testvideo-remote-file.mp4'; |
|
| 28 | - protected int $width = 1920; |
|
| 29 | - protected int $height = 1080; |
|
| 26 | + // 1080p (1920x1080) 30 FPS HEVC/H264, 10 secs, avg. bitrate: ~10 Mbps |
|
| 27 | + protected string $fileName = 'testvideo-remote-file.mp4'; |
|
| 28 | + protected int $width = 1920; |
|
| 29 | + protected int $height = 1080; |
|
| 30 | 30 | |
| 31 | - protected function setUp(): void { |
|
| 32 | - $binaryFinder = Server::get(IBinaryFinder::class); |
|
| 33 | - $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); |
|
| 34 | - if (is_string($movieBinary)) { |
|
| 35 | - parent::setUp(); |
|
| 36 | - $this->imgPath = $this->prepareTestFile($this->fileName, \OC::$SERVERROOT . '/tests/data/' . $this->fileName); |
|
| 37 | - $this->provider = new Movie(['movieBinary' => $movieBinary]); |
|
| 38 | - } else { |
|
| 39 | - $this->markTestSkipped('No Movie provider present'); |
|
| 40 | - } |
|
| 41 | - } |
|
| 31 | + protected function setUp(): void { |
|
| 32 | + $binaryFinder = Server::get(IBinaryFinder::class); |
|
| 33 | + $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); |
|
| 34 | + if (is_string($movieBinary)) { |
|
| 35 | + parent::setUp(); |
|
| 36 | + $this->imgPath = $this->prepareTestFile($this->fileName, \OC::$SERVERROOT . '/tests/data/' . $this->fileName); |
|
| 37 | + $this->provider = new Movie(['movieBinary' => $movieBinary]); |
|
| 38 | + } else { |
|
| 39 | + $this->markTestSkipped('No Movie provider present'); |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - #[\PHPUnit\Framework\Attributes\DataProvider('dimensionsDataProvider')] |
|
| 44 | - public function testGetThumbnail($widthAdjustment, $heightAdjustment): void { |
|
| 45 | - $ratio = round($this->width / $this->height, 2); |
|
| 46 | - $this->maxWidth = $this->width - $widthAdjustment; |
|
| 47 | - $this->maxHeight = $this->height - $heightAdjustment; |
|
| 48 | - $file = new File(Server::get(IRootFolder::class), $this->rootView, $this->imgPath); |
|
| 49 | - // Create mock remote file to be passed |
|
| 50 | - $remoteStorage = $this->createMock(Storage::class); |
|
| 51 | - $remoteStorage->method('isLocal') |
|
| 52 | - ->willReturn(false); |
|
| 53 | - $mockRemoteVideo = $this->createMock(File::class); |
|
| 54 | - $mockRemoteVideo->method('getStorage') |
|
| 55 | - ->willReturn($remoteStorage); |
|
| 56 | - $mockRemoteVideo->method('getSize') |
|
| 57 | - ->willReturn($file->getSize()); |
|
| 58 | - $mockRemoteVideo->method('fopen') |
|
| 59 | - ->with('r') |
|
| 60 | - ->willreturn($file->fopen('r')); |
|
| 61 | - $remotePreview = $this->provider->getThumbnail($mockRemoteVideo, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 62 | - $localPreview = $this->provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 63 | - $this->assertNotFalse($remotePreview); |
|
| 64 | - $this->assertTrue($remotePreview->valid()); |
|
| 65 | - $this->assertEquals($remotePreview->data(), $localPreview->data()); |
|
| 66 | - } |
|
| 43 | + #[\PHPUnit\Framework\Attributes\DataProvider('dimensionsDataProvider')] |
|
| 44 | + public function testGetThumbnail($widthAdjustment, $heightAdjustment): void { |
|
| 45 | + $ratio = round($this->width / $this->height, 2); |
|
| 46 | + $this->maxWidth = $this->width - $widthAdjustment; |
|
| 47 | + $this->maxHeight = $this->height - $heightAdjustment; |
|
| 48 | + $file = new File(Server::get(IRootFolder::class), $this->rootView, $this->imgPath); |
|
| 49 | + // Create mock remote file to be passed |
|
| 50 | + $remoteStorage = $this->createMock(Storage::class); |
|
| 51 | + $remoteStorage->method('isLocal') |
|
| 52 | + ->willReturn(false); |
|
| 53 | + $mockRemoteVideo = $this->createMock(File::class); |
|
| 54 | + $mockRemoteVideo->method('getStorage') |
|
| 55 | + ->willReturn($remoteStorage); |
|
| 56 | + $mockRemoteVideo->method('getSize') |
|
| 57 | + ->willReturn($file->getSize()); |
|
| 58 | + $mockRemoteVideo->method('fopen') |
|
| 59 | + ->with('r') |
|
| 60 | + ->willreturn($file->fopen('r')); |
|
| 61 | + $remotePreview = $this->provider->getThumbnail($mockRemoteVideo, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 62 | + $localPreview = $this->provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 63 | + $this->assertNotFalse($remotePreview); |
|
| 64 | + $this->assertTrue($remotePreview->valid()); |
|
| 65 | + $this->assertEquals($remotePreview->data(), $localPreview->data()); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -33,7 +33,7 @@ |
||
| 33 | 33 | $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); |
| 34 | 34 | if (is_string($movieBinary)) { |
| 35 | 35 | parent::setUp(); |
| 36 | - $this->imgPath = $this->prepareTestFile($this->fileName, \OC::$SERVERROOT . '/tests/data/' . $this->fileName); |
|
| 36 | + $this->imgPath = $this->prepareTestFile($this->fileName, \OC::$SERVERROOT.'/tests/data/'.$this->fileName); |
|
| 37 | 37 | $this->provider = new Movie(['movieBinary' => $movieBinary]); |
| 38 | 38 | } else { |
| 39 | 39 | $this->markTestSkipped('No Movie provider present'); |