Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix short videos thumbnail generation #765

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/_h5ai/private/conf/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@
- mov: array of strings
- doc: array of strings
- delay: number, delay in milliseconds after "dom-ready" before thumb-requesting starts
- size: number, size in pixel of the generated thumbnails
- size: number, height in pixel of the generated thumbnails
- seek: number, percentage of total video duration to seek into
- exif: boolean, use included EXIF thumbs if possible
- chunksize: int, number of thumbs per request
*/
Expand All @@ -366,6 +367,7 @@
"doc": ["x-pdf", "x-ps"],
"delay": 1,
"size": 240,
"seek": 50,
"exif": false,
"chunksize": 20
},
Expand Down
4 changes: 3 additions & 1 deletion src/_h5ai/private/php/core/class-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ public function get_l10n($iso_codes) {

public function get_thumbs($requests) {
$hrefs = [];
$height = $this->options['thumbnails']['size'] ?? 240;
$width = floor($height * (4 / 3));

foreach ($requests as $req) {
$thumb = new Thumb($this);
$hrefs[] = $thumb->thumb($req['type'], $req['href'], $req['width'], $req['height']);
$hrefs[] = $thumb->thumb($req['type'], $req['href'], $width, $height);
}

return $hrefs;
Expand Down
2 changes: 1 addition & 1 deletion src/_h5ai/private/php/core/class-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function add_sys_cmd_checks() {
$cmd = 'where';
}

foreach (['avconv', 'convert', 'du', 'ffmpeg', 'gm', 'tar', 'zip'] as $c) {
foreach (['avconv', 'avprobe', 'convert', 'du', 'ffmpeg', 'ffprobe', 'gm', 'tar', 'zip'] as $c) {
$cmds[$c] = ($cmd !== false) && (Util::exec_0($cmd . ' ' . $c) || Util::exec_0($cmd . ' ' . $c . '.exe'));
}

Expand Down
21 changes: 13 additions & 8 deletions src/_h5ai/private/php/core/class-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ public static function passthru_cmd($cmd) {
return $rc;
}

public static function exec_cmdv($cmdv) {
if (!is_array($cmdv)) {
$cmdv = func_get_args();
}
public static function exec_cmdv($cmdv, $capture = false, $redirect = false) {
$cmd = implode(' ', array_map('escapeshellarg', $cmdv));

$lines = [];
$rc = null;
exec($cmd, $lines, $rc);
return implode("\n", $lines);
if ($redirect) {
// Redirect stderr to stdout (notably for ffmpeg)
$cmd .= ' 2>&1'; // This cannot be shellarg-escaped
}

if ($capture){
$lines = [];
$rc = null;
exec($cmd, $lines, $rc);
return [implode("\n", $lines), $rc];
}
return exec($cmd);
}

public static function exec_0($cmd) {
Expand Down
66 changes: 52 additions & 14 deletions src/_h5ai/private/php/ext/class-thumb.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

class Thumb {
private static $FFMPEG_CMDV = ['ffmpeg', '-ss', '0:00:10', '-i', '[SRC]', '-an', '-vframes', '1', '[DEST]'];
private static $AVCONV_CMDV = ['avconv', '-ss', '0:00:10', '-i', '[SRC]', '-an', '-vframes', '1', '[DEST]'];
private static $CONVERT_CMDV = ['convert', '-density', '200', '-quality', '100', '-strip', '[SRC][0]', '[DEST]'];
private static $GM_CONVERT_CMDV = ['gm', 'convert', '-density', '200', '-quality', '100', '[SRC][0]', '[DEST]'];
private static $FFMPEG_CMDV = ['ffmpeg', '-nostdin', '-y', '-hide_banner', '-ss', '[DUR]', '-i', '[H5AI_SRC]', '-an', '-vframes', '1', '[H5AI_DEST]'];
private static $FFPROBE_CMDV = ['ffprobe', '-v', 'quiet', '-show_format_entry', 'duration', '-of', 'default=noprint_wrappers=1:nokey=1', '[H5AI_SRC]'];
private static $AVCONV_CMDV = ['avconv', '-nostdin', '-y', '-hide_banner', '-ss', '[DUR]', '-i', '[H5AI_SRC]', '-an', '-vframes', '1', '[H5AI_DEST]'];
private static $AVPROBE_CMDV = ['avprobe', '-v', 'quiet', '-show_format_entry', 'duration', '-of', 'default=noprint_wrappers=1:nokey=1', '[H5AI_SRC]'];
private static $CONVERT_CMDV = ['convert', '-density', '200', '-quality', '100', '-strip', '[H5AI_SRC][0]', '[H5AI_DEST]'];
private static $GM_CONVERT_CMDV = ['gm', 'convert', '-density', '200', '-quality', '100', '[H5AI_SRC][0]', '[H5AI_DEST]'];
private static $THUMB_CACHE = 'thumbs';

private $context;
Expand All @@ -29,21 +31,22 @@ public function thumb($type, $source_href, $width, $height) {
return null;
}

$capture_path = $source_path;
if ($type === 'img') {
$capture_path = $source_path;
} elseif ($type === 'mov') {
if ($this->setup->get('HAS_CMD_AVCONV')) {
$capture_path = $this->capture(Thumb::$AVCONV_CMDV, $source_path);
$capture_path = $this->capture(Thumb::$AVCONV_CMDV, $source_path, $type);
} elseif ($this->setup->get('HAS_CMD_FFMPEG')) {
$capture_path = $this->capture(Thumb::$FFMPEG_CMDV, $source_path);
$capture_path = $this->capture(Thumb::$FFMPEG_CMDV, $source_path, $type);
}
} elseif ($type === 'doc') {
if ($this->setup->get('HAS_CMD_CONVERT')) {
$capture_path = $this->capture(Thumb::$CONVERT_CMDV, $source_path);
$capture_path = $this->capture(Thumb::$CONVERT_CMDV, $source_path, $type);
} elseif ($this->setup->get('HAS_CMD_GM')) {
$capture_path = $this->capture(Thumb::$GM_CONVERT_CMDV, $source_path);
$capture_path = $this->capture(Thumb::$GM_CONVERT_CMDV, $source_path, $type);
}
} else {
$capture_path = $source_path;
}

return $this->thumb_href($capture_path, $width, $height);
Expand Down Expand Up @@ -80,24 +83,59 @@ private function thumb_href($source_path, $width, $height) {
return file_exists($thumb_path) ? $thumb_href : null;
}

private function capture($cmdv, $source_path) {
private function capture($cmdv, $source_path, $type) {
if (!file_exists($source_path)) {
return null;
}

$capture_path = $this->thumbs_path . '/capture-' . sha1($source_path) . '.jpg';

if (!file_exists($capture_path) || filemtime($source_path) >= filemtime($capture_path)) {
foreach ($cmdv as &$arg) {
$arg = str_replace('[SRC]', $source_path, $arg);
$arg = str_replace('[DEST]', $capture_path, $arg);
$movtype = ($type === 'mov') ? true : false;
if ($movtype) {
// Attempt to capture after the first second anyway
$timestamp = 1;
if ($cmdv[0] === 'ffmpeg') {
$timestamp = $this->compute_duration(Thumb::$FFPROBE_CMDV, $source_path);
} else {
$timestamp = $this->compute_duration(Thumb::$AVPROBE_CMDV, $source_path);
}

foreach ($cmdv as &$arg) {
$arg = str_replace(
['[H5AI_SRC]', '[H5AI_DEST]', '[DUR]'],
[$source_path, $capture_path, $timestamp],
$arg
);
}
}
else {
foreach ($cmdv as &$arg) {
$arg = str_replace(['[H5AI_SRC]', '[H5AI_DEST]'], [$source_path, $capture_path], $arg);
}
}

Util::exec_cmdv($cmdv);
}

return file_exists($capture_path) ? $capture_path : null;
}

private function compute_duration($cmdv, $source_path) {
foreach ($cmdv as &$arg) {
$arg = str_replace('[H5AI_SRC]', $source_path, $arg);
}
$duration = Util::exec_cmdv($cmdv);
if (empty($duration) || !is_numeric($duration) || is_infinite($duration)) {
return "0.1";
}
// Seek at user-defined percentage of the total video duration
return strval(
round(
(floatval($duration) *
floatval($this->context->query_option('thumbnails.seek', 50)) / 100),
1, PHP_ROUND_HALF_UP)
);
}
}

class Image {
Expand Down
2 changes: 2 additions & 0 deletions src/_h5ai/public/css/lib/view/view.less
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
.thumb {
max-width: none;
max-height: none;
object-fit: cover;
object-position: 50% 50%;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/_h5ai/public/js/lib/ext/preview/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Session.prototype = {
}
});
dom('#pv-container').hide().clr();
showSpinner(true, item.thumbSquare || item.icon, 200);
showSpinner(true, item.thumbRational || item.icon, 200);
})
.then(() => this.load(item))
// delay for testing
Expand Down
24 changes: 2 additions & 22 deletions src/_h5ai/public/js/lib/ext/thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const settings = Object.assign({
exif: false,
chunksize: 20
}, allsettings.thumbnails);
const landscapeRatio = 4 / 3;


const queueItem = (queue, item) => {
Expand All @@ -29,33 +28,16 @@ const queueItem = (queue, item) => {
return;
}

if (item.thumbSquare) {
item.$view.find('.icon.square img').addCls('thumb').attr('src', item.thumbSquare);
} else {
queue.push({
type,
href: item.absHref,
ratio: 1,
callback: src => {
if (src && item.$view) {
item.thumbSquare = src;
item.$view.find('.icon.square img').addCls('thumb').attr('src', src);
}
}
});
}

if (item.thumbRational) {
item.$view.find('.icon.landscape img').addCls('thumb').attr('src', item.thumbRational);
item.$view.find('.icon img').addCls('thumb').attr('src', item.thumbRational);
} else {
queue.push({
type,
href: item.absHref,
ratio: landscapeRatio,
callback: src => {
if (src && item.$view) {
item.thumbRational = src;
item.$view.find('.icon.landscape img').addCls('thumb').attr('src', src);
item.$view.find('.icon img').addCls('thumb').attr('src', src);
}
}
});
Expand All @@ -67,8 +49,6 @@ const requestQueue = queue => {
return {
type: req.type,
href: req.href,
width: Math.round(settings.size * req.ratio),
height: settings.size
};
});

Expand Down