aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/ffmpeg/ffmpeg-images.ts
blob: 2db63bd8b32af0bf7f69d19249d16aecdc6e7043 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
import { FFmpegCommandWrapper, FFmpegCommandWrapperOptions } from './ffmpeg-command-wrapper'

export class FFmpegImage {
  private readonly commandWrapper: FFmpegCommandWrapper

  constructor (options: FFmpegCommandWrapperOptions) {
    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: {
    path: string
    destination: string
    newSize: { width: number, height: number }
  }): Promise<void> {
    const { path, destination, newSize } = options

    this.commandWrapper.buildCommand(path)
      .fps(20)
      .size(`${newSize.width}x${newSize.height}`)
      .output(destination)

    return this.commandWrapper.runCommand()
  }

  async generateThumbnailFromVideo (options: {
    fromPath: string
    folder: string
    imageName: string
  }) {
    const { fromPath, folder, imageName } = options

    const pendingImageName = 'pending-' + imageName

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

    return new Promise<string>((res, rej) => {
      this.commandWrapper.buildCommand(fromPath)
        .on('error', rej)
        .on('end', () => res(imageName))
        .thumbnail(thumbnailOptions)
    })
  }
}