Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Added function to generate video thumbnails.

HappyDay8ed8e1bf0857
diff --git a/app/services/thumbsgenerator.js b/app/services/thumbsgenerator.js
index 93c97c8aa..c3f31536c 100644
--- a/app/services/thumbsgenerator.js
+++ b/app/services/thumbsgenerator.js
@@ -188,8 +188,12 @@ export function generateThumbnailPromise(
if (fileSize && fileSize < maxFileSize) {
return Pro.ThumbsGenerator.generateZipContainerImageThumbnail(fileURL, maxSize, supportedImgs);
}
- } else if (Pro && supportedVideos.indexOf(ext) >= 0) {
- return Pro.ThumbsGenerator.generateVideoThumbnail(fileURL, maxSize);
+ } else if (supportedVideos.indexOf(ext) >= 0) {
+ if (Pro) {
+ return Pro.ThumbsGenerator.generateVideoThumbnail(fileURL, maxSize);
+ } else {
+ return generateVideoThumbnail(fileURL);
+ }
}
return generateDefaultThumbnail();
}
@@ -231,19 +235,19 @@ function generateImageThumbnail(fileURL) {
*/
let angleInRadians;
switch (orientation) {
- case 8:
- angleInRadians = 270 * (Math.PI / 180);
- break;
- case 3:
- angleInRadians = 180 * (Math.PI / 180);
- break;
- case 6:
- angleInRadians = 90 * (Math.PI / 180);
- break;
- case 1:
- // ctx.rotate(0);
- break;
- default:
+ case 8:
+ angleInRadians = 270 * (Math.PI / 180);
+ break;
+ case 3:
+ angleInRadians = 180 * (Math.PI / 180);
+ break;
+ case 6:
+ angleInRadians = 90 * (Math.PI / 180);
+ break;
+ case 1:
+ // ctx.rotate(0);
+ break;
+ default:
// ctx.rotate(0);
}
if (img.width >= img.height) {
@@ -273,3 +277,45 @@ function generateImageThumbnail(fileURL) {
img.onerror = errorHandler;
});
}
+
+function generateVideoThumbnail(fileURL) {
+ return new Promise((resolve) => {
+ const canvas = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
+ const img = new Image();
+ const video = document.createElement('video');
+ const captureTime = 0; // time in seconds at which to capture the image from the video
+
+ const errorHandler = err => {
+ console.warn(
+ 'Error while generating thumbnail for: ' +
+ fileURL +
+ ' - ' +
+ JSON.stringify(err)
+ );
+ resolve('');
+ };
+
+ video.onloadedmetadata = function () {
+ if ('function' === typeof captureTime) {
+ captureTime = secs(this.duration);
+ }
+ // limit the captureTime to the max video duration if it exceeds it
+ this.currentTime = Math.min(Math.max(0, (captureTime < 0 ? this.duration : 0) + captureTime), this.duration);
+ };
+
+ video.onseeked = function (e) {
+ canvas.height = video.videoHeight;
+ canvas.width = video.videoWidth;
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
+ const dataurl = canvas.toDataURL(AppConfig.thumbType);
+ img.onerror = errorHandler;
+ resolve(dataurl);
+ img = null;
+ canvas = null;
+ video = null;
+ };
+ video.onerror = errorHandler;
+ video.src = fileURL;
+ });
+}