aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/ffmpeg/ffmpeg-images.ts
blob: 7f64c6d0a582db0067aa78b7ecc35de59c2ffc93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import ffmpeg from 'fluent-ffmpeg'
import { FFMPEG_NICE } from '@server/initializers/constants'
import { runCommand } from './ffmpeg-commons'

function convertWebPToJPG (path: string, destination: string): Promise<void> {
  const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL })
    .output(destination)

  return runCommand({ command, silent: true })
}

function processGIF (
  path: string,
  destination: string,
  newSize: { width: number, height: number }
): Promise<void> {
  const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL })
    .fps(20)
    .size(`${newSize.width}x${newSize.height}`)
    .output(destination)

  return runCommand({ command })
}

async function generateThumbnailFromVideo (fromPath: string, folder: string, imageName: string) {
  const pendingImageName = 'pending-' + imageName

  const options = {
    filename: pendingImageName,
    count: 1,
    folder
  }

  return new Promise<string>((res, rej) => {
    ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
      .on('error', rej)
      .on('end', () => res(imageName))
      .thumbnail(options)
  })
}

export {
  convertWebPToJPG,
  processGIF,
  generateThumbnailFromVideo
}