From b7d78fe59b73294ac13baa51a6a7cd94698cb205 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Sat, 7 Sep 2024 19:22:44 +0100 Subject: [PATCH] [Video] Only compress if >25mb or unknown format (#5187) Co-authored-by: Hailey --- src/lib/media/video/compress.ts | 17 +++++++++++++++-- src/state/queries/video/compress-video.ts | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lib/media/video/compress.ts b/src/lib/media/video/compress.ts index e783a843..ebbbc203 100644 --- a/src/lib/media/video/compress.ts +++ b/src/lib/media/video/compress.ts @@ -1,9 +1,13 @@ 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 @@ -11,12 +15,21 @@ export async function compressVideo( ): Promise { 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', () => { diff --git a/src/state/queries/video/compress-video.ts b/src/state/queries/video/compress-video.ts index 533b5841..cefbf940 100644 --- a/src/state/queries/video/compress-video.ts +++ b/src/state/queries/video/compress-video.ts @@ -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, }),