Merge pull request #747 from sanmadjack/ffmpeg_thumb_temp_file

Added handling to ensure that video thumbnails don't leave temp files…
This commit is contained in:
Shish 2020-09-16 11:20:51 +01:00 committed by GitHub
commit 111c4e3fb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -328,47 +328,51 @@ class Media extends Extension
$inname = warehouse_path(Image::IMAGE_DIR, $hash); $inname = warehouse_path(Image::IMAGE_DIR, $hash);
$tmpname = tempnam(sys_get_temp_dir(), "shimmie_ffmpeg_thumb"); $tmpname = tempnam(sys_get_temp_dir(), "shimmie_ffmpeg_thumb");
$outname = warehouse_path(Image::THUMBNAIL_DIR, $hash); try {
$outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
$orig_size = self::video_size($inname); $orig_size = self::video_size($inname);
$scaled_size = get_thumbnail_size($orig_size[0], $orig_size[1], true); $scaled_size = get_thumbnail_size($orig_size[0], $orig_size[1], true);
$codec = "mjpeg"; $codec = "mjpeg";
$quality = $config->get_int(ImageConfig::THUMB_QUALITY); $quality = $config->get_int(ImageConfig::THUMB_QUALITY);
if ($config->get_string(ImageConfig::THUMB_MIME) == MimeType::WEBP) { if ($config->get_string(ImageConfig::THUMB_MIME) == MimeType::WEBP) {
$codec = "libwebp"; $codec = "libwebp";
} else { } else {
// mjpeg quality ranges from 2-31, with 2 being the best quality. // mjpeg quality ranges from 2-31, with 2 being the best quality.
$quality = floor(31 - (31 * ($quality / 100))); $quality = floor(31 - (31 * ($quality / 100)));
if ($quality < 2) { if ($quality < 2) {
$quality = 2; $quality = 2;
}
} }
}
$args = [ $args = [
escapeshellarg($ffmpeg), escapeshellarg($ffmpeg),
"-y", "-i", escapeshellarg($inname), "-y", "-i", escapeshellarg($inname),
"-vf", "thumbnail", "-vf", "thumbnail",
"-f", "image2", "-f", "image2",
"-vframes", "1", "-vframes", "1",
"-c:v", "png", "-c:v", "png",
escapeshellarg($tmpname), escapeshellarg($tmpname),
]; ];
$cmd = escapeshellcmd(implode(" ", $args)); $cmd = escapeshellcmd(implode(" ", $args));
exec($cmd, $output, $ret); exec($cmd, $output, $ret);
if ((int)$ret == (int)0) { if ((int)$ret === (int)0) {
log_debug('media', "Generating thumbnail with command `$cmd`, returns $ret"); log_debug('media', "Generating thumbnail with command `$cmd`, returns $ret");
create_scaled_image($tmpname, $outname, $scaled_size, MimeType::PNG); create_scaled_image($tmpname, $outname, $scaled_size, MimeType::PNG);
return true; return true;
} else { } else {
log_error('media', "Generating thumbnail with command `$cmd`, returns $ret"); log_error('media', "Generating thumbnail with command `$cmd`, returns $ret");
return false; return false;
}
} finally {
@unlink($tmpname);
} }
} }