Compress Video
This API is still in development and it might break or there may be breaking changes
Description
The compressVideo
utility allows you to compress video files while optionally specifying the video and audio MIME types for the resulting compressed video.
It returns a Promise that resolves with the uri of the compressed video file.
Parameters
videoFileUri
(string): The URI of the input video file that you want to compress.videoMimeType
(optional, enum): The MIME type for the compressed video.See EnumsaudioMimeType
(optional, enum): The MIME type for the compressed audio.See Enums
Note: The Output file will be a .mp4 file,but you can choose the video and audio codec as mentioned above
Return Value
A Promise that resolves with the URI of the compressed video file.
Enums
The CompressVideoMimeTypes
enum defines MIME types for specifying the video codec when compressing video files using the compressVideo
method.
Enum Values
MIME_H_264
: Represents the H.264 video codec(AVC).MIME_H_265
: Represents the H.265 video codec(HEVC).MIME_H_263
: Represents the H.263 video codec.MIME_MP4V
: Represents the MP4V video codec.
CompressVideoAudioMimeTypes
Enum
Description
The CompressVideoAudioMimeTypes
enum defines MIME types for specifying the audio codec when compressing video files using the compressVideo
methodI.
Enum Values
AUDIO_AAC
: Represents the AAC audio codec.AUDIO_AMR_NB
: Represents the AMR-NB audio codec.AUDIO_AMR_WB
: Represents the AMR-WB audio codec.
Usage
import * as React from 'react';
import {
ActivityIndicator,
Alert,
Button,
Dimensions,
StyleSheet,
Text,
View,
} from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';
import {
compressVideo,
CompressVideoMimeTypes,
CompressVideoAudioMimeTypes,
} from 'rnvideoeditor';
const VideoCompressor = () => {
const [isCompressing, setIsCompressing] = React.useState(false);
const compressAsync = async (fileUri: string) => {
try {
setIsCompressing(true);
const startProcessing = performance.now();
const compressedVideoUri = await compressVideo({
videoFileUri: fileUri,
audioMimeType: CompressVideoAudioMimeTypes.AUDIO_AMR_NB,
videoMimeType: CompressVideoMimeTypes.MIME_H_263,
});
setIsCompressing(false);
const endProcessing = performance.now();
Alert.alert(
`Compressing done in ${Number(endProcessing - startProcessing).toFixed(
2
)}`
);
console.log(compressedVideoUri);
} catch (error) {
console.log('error', e);
} finally {
setIsCompressing(false);
}
};
const pickFile = async () => {
try {
const result = await launchImageLibrary({
mediaType: 'video',
});
if (result.assets?.length && !result.didCancel) {
const fileUri = result.assets[0]?.uri;
if (!fileUri) return;
compressAsync(fileUri);
}
} catch (error) {
console.log('error', e);
}
};
return (
<View style={styles.box}>
{isCompressing && (
<View>
<ActivityIndicator size={'small'} color={'hotpink'} />
<Text>Compressing...</Text>
</View>
)}
<Button title="Pick Video and compress" onPress={pickFile} />
</View>
);
};
const styles = StyleSheet.create({
box: {
width: Dimensions.get('window').width - 100,
height: 80,
marginVertical: 20,
},
});