Generate thumbnails for video files on QNAP NAS

Support for Synology NAS, QNAP NAS and other Embedded Linux systems
Post Reply
User avatar
rednoah
The Source
Posts: 24105
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Generate thumbnails for video files on QNAP NAS

Post by rednoah »

Multimedia Console and video files

Multimedia Console generates thumbnails for image files but does not generate thumbnails for video files (1, 2, 3, 4, 5, 6) for some reason.


:!: File Station, Qfile, DLNA Media Server, Media Streaming Add-on and friends therefore cannot display thumbnails for video files:
Screenshot



:idea: Multimedia Console does however generate thumbnails in the .@__thumb folder if CAYIN MediaSign Player is installed. File Station and Qfile use the .@__thumb folder. Some QNAP applications like DLNA Media Server and Media Streaming Add-on notably do not use the .@__thumb folder and instead use the .system/thumbnail index:

Console Output: Select all

$ find /share/Multimedia/Movies/ -type f
/share/Multimedia/Movies/.@__thumb/s800The Man from Earth (2007).mkv
/share/Multimedia/Movies/.@__thumb/s100The Man from Earth (2007).mkv
/share/Multimedia/Movies/.@__thumb/defaultThe Man from Earth (2007).mkv
/share/Multimedia/Movies/The Man from Earth (2007).mkv
$ find /share/CACHEDEV1_DATA/.system/thumbnail/ -type f
/share/CACHEDEV1_DATA/.system/thumbnail/201/1.jpg
/share/CACHEDEV1_DATA/.system/thumbnail/201/1-1.jpg
/share/CACHEDEV1_DATA/.system/thumbnail/201/1-2.jpg




Generate thumbnails for video files

The following docker command uses filebot with -exec to execute ffmpeg for each video file to generate the missing thumbnail files:

Shell: Select all

SHARE='/share/Multimedia/Movies'

docker run --rm -it -e PUID=0 -e PGID=0 -v "data:/data" -v "$SHARE:$SHARE" rednoah/filebot \
	-find "$SHARE" --filter 'def i = f.dir / ".@__thumb" / "default$f.name"; f.video && !i.exists() && seconds > 0 && (i.dir.exists() || i.dir.mkdirs())' \
		-exec sh -xc 'ffmpeg -an -ss {quote seconds/5} -i {quote f} -vframes 1 -vf scale=800:800:force_original_aspect_ratio=increase -f mjpeg -y {quote folder}/.@__thumb/s800{quote f.name} \
		           && ffmpeg -i {quote folder}/.@__thumb/s800{quote f.name} -vf scale=260:260:force_original_aspect_ratio=decrease -f mjpeg -y {quote folder}/.@__thumb/s100{quote f.name} \
		           && ffmpeg -i {quote folder}/.@__thumb/s800{quote f.name} -vf scale=400:400:force_original_aspect_ratio=increase -f mjpeg -y {quote folder}/.@__thumb/default{quote f.name} \
		           && touch -r {quote f} {quote folder}/.@__thumb/s800{quote f.name} {quote folder}/.@__thumb/s100{quote f.name} {quote folder}/.@__thumb/default{quote f.name}'
ScreenshotScreenshot





Generate thumbnail index

After generating thumbnails for each video file in the .@__thumb folder, we then need to prepare the .system/thumbnail index to make thumbnails available to DLNA Media Server and Media Streaming Add-on as well. The regenerate-thumbnail-index.sh script will take care of that:

Shell: Select all

sudo /opt/filebot/bin/regenerate-thumbnail-index.sh
regenerate-thumbnail-index.sh

Shell: Select all

#!/bin/sh -xu
SYSTEM_VOLUME="$(getcfg SHARE_DEF defVolMP -f /etc/config/def_share.info)"

function database {
	"$SYSTEM_VOLUME/.qpkg/MultimediaConsole/mariadb10/bin/mariadb" --socket=/tmp/mariadb10_mmc.sock --user=root --password=qnapqnap --database s01 --batch --default-character-set=utf8mb4 --execute "$1" --skip-column-names
}

function thumbnail {
	[ ! -f "$1" ] || [ "$1" -ef "$2/$3" ] || (mkdir -m 777 -p "$2" && ln -svf "$1" "$2/$3")
}


database 'SELECT v.iVideoId, d.cFullPath, v.cFileName FROM videoTable v INNER JOIN dirTable d ON v.iDirId = d.iDirId' | \
while IFS=$'\t' read -r iVideoId cFullPath cFileName
do
	__thumb="/share/$cFullPath.@__thumb"
	__shard="$SYSTEM_VOLUME/.system/thumbnail/2$(printf "%02x" "$iVideoId" | tail -c 2)"

	thumbnail "$__thumb/s800$cFileName"    "$__shard" "$iVideoId.jpg"
	thumbnail "$__thumb/default$cFileName" "$__shard" "$iVideoId-1.jpg"
	thumbnail "$__thumb/s100$cFileName"    "$__shard" "$iVideoId-2.jpg"
done
** Note that the QNAP DLNA media server is not fully compatible (1) with the Windows 11 built-in DLNA client so thumbnails may not work on Windows 11 even if thumbnails do work on your Smart TV. The issue has been reported to QNAP support.





Generate thumbnails from movie posters

Fetch the movie poster for each movie file (see xattr metadata) to generate the thumbnail files:

Shell: Select all

filebot -find "/volume1/Media/Movies" --apply thumbnail
Screenshot





Generate thumbnails for HEIC / HEIF image files

The following docker command uses filebot with -exec to execute convert for each HEIC / HEIF image file to generate thumbnails:

Shell: Select all

SHARE='/share/Multimedia/Photos'

docker run --rm -it -e PUID=0 -e PGID=0 -v "data:/data" -v "$SHARE:$SHARE" rednoah/filebot \
	-find "$SHARE" --filter 'def i = f.dir / ".@__thumb" / "s100$f.name"; ext ==~ /(?i:HEIC|HEIF)/ && !i.exists() && (i.dir.exists() || i.dir.mkdirs())' \
	-exec sh -xc \
		'
		convert -verbose {quote f} \
			-thumbnail "2160x2160>" -write jpg:{quote folder}/.@__thumb/s2000{quote f.name} \
			-thumbnail "800x800>" -write jpg:{quote folder}/.@__thumb/s800{quote f.name} \
			-thumbnail "400x400>" -write jpg:{quote folder}/.@__thumb/default{quote f.name} \
			-thumbnail "100x100>" jpg:{quote folder}/.@__thumb/s100{quote f.name} \
		&& touch -r {quote f} \
			{quote folder}/.@__thumb/s2000{quote f.name} \
			{quote folder}/.@__thumb/s800{quote f.name} \
			{quote folder}/.@__thumb/default{quote f.name} \
			{quote folder}/.@__thumb/s100{quote f.name}
		'
Screenshot


After generating thumbnails for each HEIC / HEIF image file in the .@__thumb folder, we then need to prepare the .system/thumbnail index and update the picture database to enable thumbnails for DLNA Media Server and QuMagie as well:

Shell: Select all

#!/bin/sh -xu
SYSTEM_VOLUME="$(getcfg SHARE_DEF defVolMP -f /etc/config/def_share.info)"

function database {
	"$SYSTEM_VOLUME/.qpkg/MultimediaConsole/mariadb10/bin/mariadb" --socket=/tmp/mariadb10_mmc.sock --user=root --password=qnapqnap --database s01 --batch --default-character-set=utf8mb4 --execute "$1" --skip-column-names
}

function thumbnail {
	[ ! -f "$1" ] || [ "$1" -ef "$2/$3" ] || (mkdir -m 777 -p "$2" && ln -svf "$1" "$2/$3")
}


database 'SELECT p.iPictureId, d.cFullPath, p.cFileName FROM pictureTable p INNER JOIN dirTable d ON p.iDirId = d.iDirId WHERE p.ScannedFlag > 1' | \
while IFS=$'\t' read -r iPictureId cFullPath cFileName
do
	__thumb="/share/$cFullPath.@__thumb"
	__shard="$SYSTEM_VOLUME/.system/thumbnail/1$(printf "%02x" "$iPictureId" | tail -c 2)"

	if [ -f "$__thumb/s2000$cFileName" ]
	then
		thumbnail "$__thumb/s2000$cFileName"   "$__shard" "$iPictureId-4k.jpg"
		thumbnail "$__thumb/s800$cFileName"    "$__shard" "$iPictureId.jpg"
		thumbnail "$__thumb/default$cFileName" "$__shard" "$iPictureId-1.jpg"
		thumbnail "$__thumb/s100$cFileName"    "$__shard" "$iPictureId-2.jpg"

		database "UPDATE pictureTable SET ScannedFlag=1 WHERE iPictureId=$iPictureId"
	fi
done




Delete thumbnails for video files

Find and delete the .@__thumb/s* and .@__thumb/default* thumbnail files:

Shell: Select all

find "/share/Multimedia/Movies" -type f '(' -path '*/.@__thumb/s[18]00*' -or -path '*/.@__thumb/default*' ')' -print -delete
:idea: Please read the FAQ and How to Request Help.
Post Reply