mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 01:39:37 +02:00
Correctly handle webp images
This commit is contained in:
parent
dff2e95369
commit
260a6e5ec4
3 changed files with 23 additions and 43 deletions
|
@ -10,19 +10,7 @@ export class FFmpegImage {
|
|||
this.commandWrapper = new FFmpegCommandWrapper(options)
|
||||
}
|
||||
|
||||
convertWebPToJPG (options: {
|
||||
path: string
|
||||
destination: string
|
||||
}): Promise<void> {
|
||||
const { path, destination } = options
|
||||
|
||||
this.commandWrapper.buildCommand(path)
|
||||
.output(destination)
|
||||
|
||||
return this.commandWrapper.runCommand({ silent: true })
|
||||
}
|
||||
|
||||
processGIF (options: {
|
||||
processImage (options: {
|
||||
path: string
|
||||
destination: string
|
||||
newSize?: { width: number, height: number }
|
||||
|
@ -35,7 +23,7 @@ export class FFmpegImage {
|
|||
|
||||
command.output(destination)
|
||||
|
||||
return this.commandWrapper.runCommand()
|
||||
return this.commandWrapper.runCommand({ silent: true })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
import { FFmpegImage } from '@peertube/peertube-ffmpeg'
|
||||
import { getFFmpegCommandWrapperOptions } from './ffmpeg-options.js'
|
||||
|
||||
export function processGIF (options: Parameters<FFmpegImage['processGIF']>[0]) {
|
||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).processGIF(options)
|
||||
export function processImage (options: Parameters<FFmpegImage['processImage']>[0]) {
|
||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).processImage(options)
|
||||
}
|
||||
|
||||
export function generateThumbnailFromVideo (options: Parameters<FFmpegImage['generateThumbnailFromVideo']>[0]) {
|
||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).generateThumbnailFromVideo(options)
|
||||
}
|
||||
|
||||
export function convertWebPToJPG (options: Parameters<FFmpegImage['convertWebPToJPG']>[0]) {
|
||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).convertWebPToJPG(options)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { copy, remove } from 'fs-extra/esm'
|
||||
import { readFile, rename } from 'fs/promises'
|
||||
import { ColorActionName } from '@jimp/plugin-color'
|
||||
import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
|
||||
import { convertWebPToJPG, processGIF } from './ffmpeg/index.js'
|
||||
import { copy, remove } from 'fs-extra/esm'
|
||||
import { readFile } from 'fs/promises'
|
||||
import { processImage as processImageByFFmpeg } from './ffmpeg/index.js'
|
||||
import { logger } from './logger.js'
|
||||
|
||||
import type Jimp from 'jimp'
|
||||
|
@ -28,8 +28,8 @@ export async function processImage (options: {
|
|||
logger.debug('Processing image %s to %s.', path, destination)
|
||||
|
||||
// Use FFmpeg to process GIF
|
||||
if (extension === '.gif') {
|
||||
await processGIF({ path, destination, newSize })
|
||||
if (extension === '.gif' || extension === '.webp') {
|
||||
await processImageByFFmpeg({ path, destination, newSize })
|
||||
} else {
|
||||
await jimpProcessor({ path, destination, newSize, inputExt: extension })
|
||||
}
|
||||
|
@ -65,30 +65,26 @@ async function jimpProcessor (options: {
|
|||
}
|
||||
inputExt: string
|
||||
}) {
|
||||
const { path, destination, newSize, inputExt } = options
|
||||
const { path, newSize, inputExt, destination } = options
|
||||
|
||||
let sourceImage: Jimp
|
||||
const inputBuffer = await readFile(path)
|
||||
|
||||
const Jimp = await import('jimp')
|
||||
|
||||
try {
|
||||
sourceImage = await Jimp.default.read(inputBuffer)
|
||||
} catch (err) {
|
||||
logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
|
||||
|
||||
const newName = path + '.jpg'
|
||||
await convertWebPToJPG({ path, destination: newName })
|
||||
await rename(newName, path)
|
||||
|
||||
sourceImage = await Jimp.default.read(path)
|
||||
}
|
||||
const sourceImage = await Jimp.default.read(inputBuffer)
|
||||
|
||||
await remove(destination)
|
||||
|
||||
// Optimization if the source file has the appropriate size
|
||||
const outputExt = getLowercaseExtension(destination)
|
||||
if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
|
||||
|
||||
if (
|
||||
skipProcessing({
|
||||
sourceImage,
|
||||
newSize,
|
||||
imageBytes: inputBuffer.byteLength,
|
||||
inputExt,
|
||||
outputExt: getLowercaseExtension(destination)
|
||||
})
|
||||
) {
|
||||
return copy(path, destination)
|
||||
}
|
||||
|
||||
|
@ -114,7 +110,7 @@ async function autoResize (options: {
|
|||
|
||||
if (sourceIsPortrait && !destIsPortraitOrSquare) {
|
||||
const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height)
|
||||
.color([ { apply: ColorActionName.SHADE, params: [ 50 ] } ])
|
||||
.color([ { apply: ColorActionName.SHADE, params: [ 50 ] } ])
|
||||
|
||||
const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height)
|
||||
|
||||
|
@ -156,5 +152,5 @@ function hasExif (image: Jimp) {
|
|||
}
|
||||
|
||||
function removeExif (image: Jimp) {
|
||||
(image.bitmap as any).exifBuffer = null
|
||||
;(image.bitmap as any).exifBuffer = null
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue