about summary refs log tree commit diff
path: root/src/lib/media/video/compress.ts
blob: 60e5e94a0056edb5b58d051fd1b8f427244c9ae0 (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
import {getVideoMetaData, Video} from 'react-native-compressor'

export type CompressedVideo = {
  uri: string
  size: number
}

export async function compressVideo(
  file: string,
  opts?: {
    getCancellationId?: (id: string) => void
    onProgress?: (progress: number) => void
  },
): Promise<CompressedVideo> {
  const {onProgress, getCancellationId} = opts || {}

  const compressed = await Video.compress(
    file,
    {
      getCancellationId,
      compressionMethod: 'manual',
      bitrate: 3_000_000, // 3mbps
      maxSize: 1920,
    },
    onProgress,
  )

  const info = await getVideoMetaData(compressed)
  return {uri: compressed, size: info.size}
}