mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 09:49:20 +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)
|
this.commandWrapper = new FFmpegCommandWrapper(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
convertWebPToJPG (options: {
|
processImage (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: {
|
|
||||||
path: string
|
path: string
|
||||||
destination: string
|
destination: string
|
||||||
newSize?: { width: number, height: number }
|
newSize?: { width: number, height: number }
|
||||||
|
@ -35,7 +23,7 @@ export class FFmpegImage {
|
||||||
|
|
||||||
command.output(destination)
|
command.output(destination)
|
||||||
|
|
||||||
return this.commandWrapper.runCommand()
|
return this.commandWrapper.runCommand({ silent: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
import { FFmpegImage } from '@peertube/peertube-ffmpeg'
|
import { FFmpegImage } from '@peertube/peertube-ffmpeg'
|
||||||
import { getFFmpegCommandWrapperOptions } from './ffmpeg-options.js'
|
import { getFFmpegCommandWrapperOptions } from './ffmpeg-options.js'
|
||||||
|
|
||||||
export function processGIF (options: Parameters<FFmpegImage['processGIF']>[0]) {
|
export function processImage (options: Parameters<FFmpegImage['processImage']>[0]) {
|
||||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).processGIF(options)
|
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).processImage(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateThumbnailFromVideo (options: Parameters<FFmpegImage['generateThumbnailFromVideo']>[0]) {
|
export function generateThumbnailFromVideo (options: Parameters<FFmpegImage['generateThumbnailFromVideo']>[0]) {
|
||||||
return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).generateThumbnailFromVideo(options)
|
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 { ColorActionName } from '@jimp/plugin-color'
|
||||||
import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
|
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 { logger } from './logger.js'
|
||||||
|
|
||||||
import type Jimp from 'jimp'
|
import type Jimp from 'jimp'
|
||||||
|
@ -28,8 +28,8 @@ export async function processImage (options: {
|
||||||
logger.debug('Processing image %s to %s.', path, destination)
|
logger.debug('Processing image %s to %s.', path, destination)
|
||||||
|
|
||||||
// Use FFmpeg to process GIF
|
// Use FFmpeg to process GIF
|
||||||
if (extension === '.gif') {
|
if (extension === '.gif' || extension === '.webp') {
|
||||||
await processGIF({ path, destination, newSize })
|
await processImageByFFmpeg({ path, destination, newSize })
|
||||||
} else {
|
} else {
|
||||||
await jimpProcessor({ path, destination, newSize, inputExt: extension })
|
await jimpProcessor({ path, destination, newSize, inputExt: extension })
|
||||||
}
|
}
|
||||||
|
@ -65,30 +65,26 @@ async function jimpProcessor (options: {
|
||||||
}
|
}
|
||||||
inputExt: string
|
inputExt: string
|
||||||
}) {
|
}) {
|
||||||
const { path, destination, newSize, inputExt } = options
|
const { path, newSize, inputExt, destination } = options
|
||||||
|
|
||||||
let sourceImage: Jimp
|
|
||||||
const inputBuffer = await readFile(path)
|
const inputBuffer = await readFile(path)
|
||||||
|
|
||||||
const Jimp = await import('jimp')
|
const Jimp = await import('jimp')
|
||||||
|
const sourceImage = await Jimp.default.read(inputBuffer)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
await remove(destination)
|
await remove(destination)
|
||||||
|
|
||||||
// Optimization if the source file has the appropriate size
|
// 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)
|
return copy(path, destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,5 +152,5 @@ function hasExif (image: Jimp) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeExif (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