Files
bsky-social-app/src/lib/media/video/compress.web.ts
T
2026-06-16 11:03:23 +03:00

36 lines
903 B
TypeScript

import {type ImagePickerAsset} from 'expo-image-picker'
import {VIDEO_MAX_SIZE} from '#/lib/constants'
import {VideoTooLargeError} from '#/lib/media/video/errors'
import {type CompressedVideo} from './types'
// doesn't actually compress, just reads the File bytes
export async function compressVideo(
asset: ImagePickerAsset,
_opts?: {
signal?: AbortSignal
onProgress?: (progress: number) => void
},
): Promise<CompressedVideo> {
let bytes: ArrayBuffer
if (asset.file) {
bytes = await asset.file.arrayBuffer()
} else {
// fallback: fetch from blob/object URL
bytes = await fetch(asset.uri).then(res => res.arrayBuffer())
}
if (bytes.byteLength > VIDEO_MAX_SIZE) {
throw new VideoTooLargeError()
}
const mimeType = asset.mimeType ?? asset.file?.type ?? 'video/mp4'
return {
size: bytes.byteLength,
uri: asset.uri,
bytes,
mimeType,
}
}