Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 80 additions & 29 deletions lib/private/Preview/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,55 +96,106 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {

return $result;
}

private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage {
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();

$binaryType = substr(strrchr($this->binary, '/'), 1);
$cmd = $this->buildCommand($binaryType, $second, $absPath, $tmpPath);

if (!$cmd) {
unlink($tmpPath);
return null;
}

$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
if (!is_resource($proc)) {
unlink($tmpPath);
return null;
}

$output = $this->processPipes($pipes);
$status = proc_get_status($proc);

if ($status['running']) {
proc_terminate($proc, 9); // SIGKILL
}

$returnCode = proc_close($proc);

if ($returnCode === 0) {
return $this->handleSuccessfulThumbnailCreation($tmpPath, $maxX, $maxY);
}

$this->logError($second, $output);
unlink($tmpPath);
return null;
}

private function buildCommand(string $binaryType, int $second, string $absPath, string $tmpPath): ?array {
if ($binaryType === 'avconv') {
$cmd = [$this->binary, '-y', '-ss', (string)$second,
$cmd = [
$this->binary, '-y', '-ss', (string)$second,
'-i', $absPath,
'-an', '-f', 'mjpeg', '-vframes', '1', '-vsync', '1',
$tmpPath];
$tmpPath
];
} elseif ($binaryType === 'ffmpeg') {
$cmd = [$this->binary, '-y', '-ss', (string)$second,
$cmd = [
$this->binary, '-y', '-ss', (string)$second,
'-i', $absPath,
'-f', 'mjpeg', '-vframes', '1',
$tmpPath];
$tmpPath
];
} else {
// Not supported
unlink($tmpPath);
return null;
}
return $cmd;
}

$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
$returnCode = -1;
private function processPipes(array $pipes): string {
$output = "";
if (is_resource($proc)) {
$stdout = trim(stream_get_contents($pipes[1]));
$stderr = trim(stream_get_contents($pipes[2]));
$returnCode = proc_close($proc);
$output = $stdout . $stderr;
$endTime = time() + 10;

foreach ([1, 2] as $pipeNum) {
stream_set_blocking($pipes[$pipeNum], false);
stream_set_timeout($pipes[$pipeNum], 10);

while (!feof($pipes[$pipeNum])) {
$chunk = fread($pipes[$pipeNum], 8192);
if ($chunk === false || $chunk === "") {
$info = stream_get_meta_data($pipes[$pipeNum]);
if ($info['timed_out'] || $info['eof']) {
break;
}
if (time() > $endTime) {
break;
}
usleep(10000);
} else {
$output .= $chunk;
}
}
fclose($pipes[$pipeNum]);
}

if ($returnCode === 0) {
$image = new \OCP\Image();
$image->loadFromFile($tmpPath);
if ($image->valid()) {
unlink($tmpPath);
$image->scaleDownToFit($maxX, $maxY);
return $output;
}

return $image;
}
}

if ($second === 0) {
$logger = \OC::$server->get(LoggerInterface::class);
$logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]);
}

unlink($tmpPath);
private function handleSuccessfulThumbnailCreation(string $tmpPath, int $maxX, int $maxY): ?IImage {
$image = new \OCP\Image();
$image->loadFromFile($tmpPath);
if ($image->valid()) {
unlink($tmpPath);
$image->scaleDownToFit($maxX, $maxY);
return $image;
}
return null;
}

private function logError(int $second, string $output): void {
$logger = \OC::$server->get(LoggerInterface::class);
$logger->info('Thumbnail generation failed at second {second}. Output: {output}', ['app' => 'core', 'second' => $second, 'output' => $output]);
}

}