[Video] Only compress if >25mb or unknown format (#5187)
Co-authored-by: Hailey <me@haileyok.com>zio/stable
parent
42fb92064a
commit
b7d78fe59b
|
@ -1,9 +1,13 @@
|
||||||
import {getVideoMetaData, Video} from 'react-native-compressor'
|
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'
|
import {CompressedVideo} from './types'
|
||||||
|
|
||||||
|
const MIN_SIZE_FOR_COMPRESSION = 1024 * 1024 * 25 // 25mb
|
||||||
|
|
||||||
export async function compressVideo(
|
export async function compressVideo(
|
||||||
file: string,
|
file: ImagePickerAsset,
|
||||||
opts?: {
|
opts?: {
|
||||||
signal?: AbortSignal
|
signal?: AbortSignal
|
||||||
onProgress?: (progress: number) => void
|
onProgress?: (progress: number) => void
|
||||||
|
@ -11,12 +15,21 @@ export async function compressVideo(
|
||||||
): Promise<CompressedVideo> {
|
): Promise<CompressedVideo> {
|
||||||
const {onProgress, signal} = opts || {}
|
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(
|
const compressed = await Video.compress(
|
||||||
file,
|
file.uri,
|
||||||
{
|
{
|
||||||
compressionMethod: 'manual',
|
compressionMethod: 'manual',
|
||||||
bitrate: 3_000_000, // 3mbps
|
bitrate: 3_000_000, // 3mbps
|
||||||
maxSize: 1920,
|
maxSize: 1920,
|
||||||
|
minimumFileSizeForCompress,
|
||||||
getCancellationId: id => {
|
getCancellationId: id => {
|
||||||
if (signal) {
|
if (signal) {
|
||||||
signal.addEventListener('abort', () => {
|
signal.addEventListener('abort', () => {
|
||||||
|
|
|
@ -20,7 +20,7 @@ export function useCompressVideoMutation({
|
||||||
mutationKey: ['video', 'compress'],
|
mutationKey: ['video', 'compress'],
|
||||||
mutationFn: cancelable(
|
mutationFn: cancelable(
|
||||||
(asset: ImagePickerAsset) =>
|
(asset: ImagePickerAsset) =>
|
||||||
compressVideo(asset.uri, {
|
compressVideo(asset, {
|
||||||
onProgress: num => onProgress(trunc2dp(num)),
|
onProgress: num => onProgress(trunc2dp(num)),
|
||||||
signal,
|
signal,
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in New Issue