How to resolve video thumbnail cannot be created in Nextcloud?

Reading Time: 2 minutes

Usually video thumbnail is created when you upload video file to your Nextcloud like this.

But in my case it isn’t like below.

Root cause and countermeasure

In my case they are root causes.

1. ffmpeg isn't installed which is used by Nextcloud
2. escapeshellarg doesn't work to multibyte path because locale is C/POSIX

I performed these countermeasures.

1. Installed ffmpeg
2. Setup Nextcloud's locale to ja_JP.UTF-8 (because I am Japanese)

For more detail about 2,
Video thumbnail is created in generateThumbNail() in ./lib/private/Preview/Movie.php which calls escapeshellarg().

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

  if (self::$avconvBinary) {
    $cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
      ' -i ' . escapeshellarg($absPath) .
      ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
      ' > /dev/null 2>&1';
  } else {
    $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
      ' -i ' . escapeshellarg($absPath) .
      ' -f mjpeg -vframes 1' .
      ' ' . escapeshellarg($tmpPath) .
      ' > /dev/null 2>&1';

escapeshellarg() is affected by locale,
so if multibyte like Japanese are included in argument of escapeshellarg, escapeshellarg doesn’t work as expected.

Installed ffmpeg

Just installing by package manager like this.

apt install ffmpeg

Setup Nextcloud’s locale to ja_JP.UTF-8

There will be 2 steps.

1. Adding ja_JP.UTF-8 to locale
2. Call setlocale(LC_CTYPE, 'ja_JP.UTF-8'); in somewhere of Nextcloud

Adding ja_JP.UTF-8 to locale

We can achieve like this.

RUN	apt -y install locales && \
	echo "ja_JP UTF-8" > /etc/locale.gen && \
	locale-gen && \

After this you just need to restart php-fpm.

Call setlocale(LC_CTYPE, ‘ja_JP.UTF-8’);

I considered and decided to use auto_prepend_file mechanism.

First create corresponding file to auto_prepend_file. in of php-fpm container.

echo '<?php setlocale(LC_CTYPE, "ja_JP.UTF-8");' > /var/www/prepend.php
chown www-data /var/www/prepend.php

Next is to configure Nginx server block. 

fastcgi_param PHP_VALUE "auto_prepend_file=/var/www/prepend.php";

Then setlocale() will be called every time.

Here is commit of this modification.

Modified locale from C to ja_JP.UTF-8 to be able to create thumbnail … · kurofuku/nextcloud-container-fpm@4d97971
…of movie by ffmpeg. - Without this locale change creating preview of file which is multibyte folder fails.

Then you should be able to see thumbnail which is in mutibyte path!


How was it?

Enjoy your Nextcloud life!


Copied title and URL