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 {
FFmpegKit,
FFmpegSessionCompleteCallback,
LogCallback,
ReturnCode,
StatisticsCallback,
} from 'ffmpeg-kit-react-native'
const PRESET = 'faster'
@@ -12,12 +10,10 @@ const PRESET = 'faster'
export async function compressVideo(
file: string,
callbacks?: {
onLog?: LogCallback
onStatistics?: StatisticsCallback
onProgress: (progress: number) => void
},
) {
const {onLog, onStatistics} = callbacks || {}
const {onProgress} = callbacks || {}
const ext = file.split('.').pop()
const newFile = file.replace(`.${ext}`, '.compressed.mp4')
@@ -25,8 +21,8 @@ export async function compressVideo(
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}`,
resolve,
onLog,
onStatistics,
undefined,
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({
mutationFn: async (asset: ImagePickerAsset) => {
const compressed = await compressVideo(asset.uri, {
onStatistics: async statistics => {
onProgress: progressMs => {
if (asset.duration) {
setProgress(statistics.getTime() / asset.duration)
setProgress(progressMs / asset.duration)
}
},
})