mirror of https://github.com/tycrek/ass
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.9 KiB
89 lines
2.9 KiB
3 years ago
|
import { FileData } from "./definitions";
|
||
|
import ffmpeg from 'ffmpeg-static';
|
||
|
import Jimp from 'jimp';
|
||
3 years ago
|
const shell = require('any-shell-escape');
|
||
3 years ago
|
import { exec } from 'child_process';
|
||
|
import { isProd, path } from './utils';
|
||
3 years ago
|
const { diskFilePath } = require('../config.json');
|
||
3 years ago
|
|
||
3 years ago
|
// Thumbnail parameters
|
||
3 years ago
|
const THUMBNAIL = {
|
||
|
QUALITY: 75,
|
||
|
WIDTH: 200 * 2,
|
||
|
HEIGHT: 140 * 2,
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
/**
|
||
|
* Builds a safe escaped ffmpeg command
|
||
|
* @param {String} src Path to the input file
|
||
|
* @param {String} dest Path of the output file
|
||
|
* @returns {String} The command to execute
|
||
|
*/
|
||
3 years ago
|
function getCommand(src: String, dest: String) {
|
||
3 years ago
|
return shell([
|
||
3 years ago
|
ffmpeg, '-y',
|
||
3 years ago
|
'-v', (isProd ? 'error' : 'debug'), // Log level
|
||
3 years ago
|
'-i', src, // Input file
|
||
|
'-ss', '00:00:01.000', // Timestamp of frame to grab
|
||
|
'-frames:v', '1', // Number of frames to grab
|
||
3 years ago
|
'-s', `${THUMBNAIL.WIDTH}x${THUMBNAIL.HEIGHT}`, // Dimensions of output file
|
||
3 years ago
|
dest // Output file
|
||
3 years ago
|
]);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Builds a thumbnail filename
|
||
|
* @param {String} oldName The original filename
|
||
|
* @returns {String} The filename for the thumbnail
|
||
|
*/
|
||
3 years ago
|
function getNewName(oldName: String) {
|
||
3 years ago
|
return oldName.concat('.thumbnail.jpg');
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Builds a path to the thumbnails
|
||
|
* @param {String} oldName The original filename
|
||
|
* @returns {String} The path to the thumbnail
|
||
|
*/
|
||
3 years ago
|
function getNewNamePath(oldName: String) {
|
||
3 years ago
|
return path(diskFilePath, 'thumbnails/', getNewName(oldName));
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Extracts an image from a video file to use as a thumbnail, using ffmpeg
|
||
|
* @param {*} file The video file to pull a frame from
|
||
|
*/
|
||
3 years ago
|
function getVideoThumbnail(file: FileData) {
|
||
|
return new Promise((resolve: Function, reject: Function) => exec(
|
||
3 years ago
|
getCommand(file.path, getNewNamePath(file.randomId)),
|
||
3 years ago
|
// @ts-ignore
|
||
|
(err: Error) => (err ? reject(err) : resolve())
|
||
3 years ago
|
));
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Generates a thumbnail for the provided image
|
||
|
* @param {*} file The file to generate a thumbnail for
|
||
|
*/
|
||
3 years ago
|
function getImageThumbnail(file: FileData) {
|
||
3 years ago
|
return new Promise((resolve, reject) =>
|
||
3 years ago
|
Jimp.read(file.path)
|
||
3 years ago
|
.then((image) => image
|
||
3 years ago
|
.quality(THUMBNAIL.QUALITY)
|
||
|
.resize(THUMBNAIL.WIDTH, THUMBNAIL.HEIGHT, Jimp.RESIZE_BICUBIC)
|
||
3 years ago
|
.write(getNewNamePath(file.randomId)))
|
||
3 years ago
|
.then(resolve)
|
||
|
.catch(reject));
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Generates a thumbnail
|
||
|
* @param {*} file The file to generate a thumbnail for
|
||
|
* @returns The thumbnail filename (NOT the path)
|
||
|
*/
|
||
3 years ago
|
module.exports = (file: FileData) =>
|
||
3 years ago
|
new Promise((resolve, reject) =>
|
||
3 years ago
|
(file.is.video ? getVideoThumbnail : file.is.image ? getImageThumbnail : () => Promise.resolve())(file)
|
||
3 years ago
|
.then(() => resolve((file.is.video || file.is.image) ? getNewName(file.randomId) : file.is.audio ? 'views/ass-audio-icon.png' : 'views/ass-file-icon.png'))
|
||
3 years ago
|
.catch(reject));
|