Skip to content

Commit

Permalink
Add ffmpeg GIF process
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsible committed Nov 21, 2020
1 parent 499586a commit 1ea77eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
35 changes: 35 additions & 0 deletions server/helpers/ffmpeg-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,40 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> {
})
}

function processGIF (
path: string,
destination: string,
newSize: { width: number, height: number },
keepOriginal = false
): Promise<void> {
return new Promise<void>(async (res, rej) => {
if (path === destination) {
throw new Error('FFmpeg needs an input path different that the output path.')
}

logger.debug('Processing gif %s to %s.', path, destination)

try {
const command = ffmpeg(path)
.fps(20)
.size(`${newSize.width}x${newSize.height}`)
.output(destination)

command.on('error', (err, stdout, stderr) => {
logger.error('Error in ffmpeg gif resizing process.', { stdout, stderr })
return rej(err)
})
.on('end', async () => {
if (keepOriginal !== true) await remove(path)
res()
})
.run()
} catch (err) {
return rej(err)
}
})
}

function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], fps, deleteSegments: boolean) {
const command = getFFmpeg(rtmpUrl)
command.inputOption('-fflags nobuffer')
Expand Down Expand Up @@ -474,6 +508,7 @@ export {
getAudioStreamCodec,
runLiveMuxing,
convertWebPToJPG,
processGIF,
getVideoStreamSize,
getVideoFileResolution,
getMetadataFromFile,
Expand Down
10 changes: 9 additions & 1 deletion server/helpers/image-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extname } from 'path'
import { remove, rename } from 'fs-extra'
import { convertWebPToJPG } from './ffmpeg-utils'
import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
import { logger } from './logger'

const Jimp = require('jimp')
Expand All @@ -10,6 +11,13 @@ async function processImage (
newSize: { width: number, height: number },
keepOriginal = false
) {
const extension = extname(path)

// Use FFmpeg to process GIF
if (extension === '.gif') {
return processGIF(path, destination, newSize, keepOriginal)
}

if (path === destination) {
throw new Error('Jimp needs an input path different that the output path.')
}
Expand Down
8 changes: 1 addition & 7 deletions server/lib/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'multer'
import { move } from 'fs-extra'
import { sendUpdateActor } from './activitypub/send'
import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
import { updateActorAvatarInstance } from './activitypub/actor'
Expand All @@ -22,12 +21,7 @@ async function updateActorAvatarFile (
const avatarName = uuidv4() + extension
const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)

// For gif do not resize, just move
if (extension === '.gif') {
await move(avatarPhysicalFile.path, destination)
} else {
await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
}
await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)

return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
Expand Down

0 comments on commit 1ea77eb

Please sign in to comment.