Skip to content

Commit

Permalink
[Video] Only compress if >25mb or unknown format (#5187)
Browse files Browse the repository at this point in the history
Co-authored-by: Hailey <[email protected]>
  • Loading branch information
mozzius and haileyok authored Sep 7, 2024
1 parent 42fb920 commit b7d78fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/lib/media/video/compress.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import {getVideoMetaData, Video} from 'react-native-compressor'
import {ImagePickerAsset} from 'expo-image-picker'

import {SUPPORTED_MIME_TYPES, SupportedMimeTypes} from '#/lib/constants'
import {CompressedVideo} from './types'

const MIN_SIZE_FOR_COMPRESSION = 1024 * 1024 * 25 // 25mb

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

const isAcceptableFormat = SUPPORTED_MIME_TYPES.includes(
file.mimeType as SupportedMimeTypes,
)

const minimumFileSizeForCompress = isAcceptableFormat
? MIN_SIZE_FOR_COMPRESSION
: 0

const compressed = await Video.compress(
file,
file.uri,
{
compressionMethod: 'manual',
bitrate: 3_000_000, // 3mbps
maxSize: 1920,
minimumFileSizeForCompress,
getCancellationId: id => {
if (signal) {
signal.addEventListener('abort', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/state/queries/video/compress-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useCompressVideoMutation({
mutationKey: ['video', 'compress'],
mutationFn: cancelable(
(asset: ImagePickerAsset) =>
compressVideo(asset.uri, {
compressVideo(asset, {
onProgress: num => onProgress(trunc2dp(num)),
signal,
}),
Expand Down

0 comments on commit b7d78fe

Please sign in to comment.