move logic out of compressVideo

This commit is contained in:
Samuel Newman
2024-06-06 12:20:09 +03:00
parent 6a54959080
commit ed17e94cec
2 changed files with 6 additions and 10 deletions
+4 -8
View File
@@ -2,9 +2,7 @@ import * as FileSystem from 'expo-file-system'
import { import {
FFmpegKit, FFmpegKit,
FFmpegSessionCompleteCallback, FFmpegSessionCompleteCallback,
LogCallback,
ReturnCode, ReturnCode,
StatisticsCallback,
} from 'ffmpeg-kit-react-native' } from 'ffmpeg-kit-react-native'
const PRESET = 'faster' const PRESET = 'faster'
@@ -12,12 +10,10 @@ const PRESET = 'faster'
export async function compressVideo( export async function compressVideo(
file: string, file: string,
callbacks?: { callbacks?: {
onLog?: LogCallback onProgress: (progress: number) => void
onStatistics?: StatisticsCallback
}, },
) { ) {
const {onLog, onStatistics} = callbacks || {} const {onProgress} = callbacks || {}
const ext = file.split('.').pop() const ext = file.split('.').pop()
const newFile = file.replace(`.${ext}`, '.compressed.mp4') const newFile = file.replace(`.${ext}`, '.compressed.mp4')
@@ -25,8 +21,8 @@ export async function compressVideo(
FFmpegKit.executeAsync( FFmpegKit.executeAsync(
`-i ${file} -c:v libx264 -crf 25 -preset ${PRESET} -b:v 4M -vf "scale='if(gt(a,1),min(1920,iw),-1)':'if(gt(a,1),-1,min(1920,ih))'" -t 90 -c:a aac -b:a 320k -movflags +faststart ${newFile}`, `-i ${file} -c:v libx264 -crf 25 -preset ${PRESET} -b:v 4M -vf "scale='if(gt(a,1),min(1920,iw),-1)':'if(gt(a,1),-1,min(1920,ih))'" -t 90 -c:a aac -b:a 320k -movflags +faststart ${newFile}`,
resolve, resolve,
onLog, undefined,
onStatistics, stats => onProgress?.(stats.getTime()),
), ),
) )
+2 -2
View File
@@ -14,9 +14,9 @@ export function useVideoState({setError}: {setError: (error: string) => void}) {
const {mutate, data, isPending, isError, reset, variables} = useMutation({ const {mutate, data, isPending, isError, reset, variables} = useMutation({
mutationFn: async (asset: ImagePickerAsset) => { mutationFn: async (asset: ImagePickerAsset) => {
const compressed = await compressVideo(asset.uri, { const compressed = await compressVideo(asset.uri, {
onStatistics: async statistics => { onProgress: progressMs => {
if (asset.duration) { if (asset.duration) {
setProgress(statistics.getTime() / asset.duration) setProgress(progressMs / asset.duration)
} }
}, },
}) })