Add audio to the video
By using this method, you can add audio to a video file and save the resulting video to a specified oIutput file.
This function provides flexibility when you need to combine audio and video content.
Parameters
audioFileURI
(String): The uri of the audio file that you want to add to the video.videoFileURI
(String): The uri of the video file to which you want to add audio.
Returns
A Promise
that resolves with the uri of the output video on successful audio addition or rejects with an error message on failure.
Note: This function supports the following file formats:
Video:
.mp4
and.3gp
Audio:
.aac
If the duration of the provided audio file is longer than the duration of the video file, the resulting output video will have a duration that matches the audio's duration. To ensure that the audio fits the entire duration, the last frame of the original video will be duplicated and added to the end of the video.
We are working to provide and convient option to decide what you want to do in such cases.
(Work around: You can trim the resultant video with trimVideo
api by the video's duartion)
Usage
import React from 'react';
import { View, Button, Alert, StyleSheet, Text,Dimensions } from 'react-native';
import { addAudioToVideo } from 'rnvideoeditor';
import { launchImageLibrary } from 'react-native-image-picker';
const AVMixer = () => {
const mixAv = async () => {
try {
const result = await launchImageLibrary({
mediaType: 'video',
});
if (result.assets && !result.didCancel) {
if (!result.assets[0]) {
Alert.alert('No video selected', 'Please select a video to mix with audio.');
return;
}
const startProcessing = performance.now();
const AUDIO_URL = 'path_to_audio_file';
try {
const res = await addAudioToVideo(AUDIO_URL, result.assets[0].uri);
const endProcessing = performance.now();
Alert.alert(
`Processing Done in ${((endProcessing - startProcessing) / 1000).toFixed(2)} Seconds`,
res
);
} catch (error) {
console.log('Error in mixing audio and video', error);
}
}
} catch (error) {
console.error('Error while picking video:', error);
}
};
return (
<View style={styles.box}>
<Text>Add Audio To a File</Text>
<Button
title="Mix Last Trimmed video with sample audio"
onPress={pickAudioFile}
/>
</View>
);
};
const styles = StyleSheet.create({
box: {
width: Dimensions.get('window').width - 100,
height: 80,
marginVertical: 20,
},
});