Skip to main content

Extract audio track from the video

This method allows you to extract the audio from a video file specified by its file uri.

This function is useful when you need to extract the audio track from a video.

Parameters

  • videoFileUri (string): The file uri of the video from which you want to extract the audio.

Returns

A Promise that resolves with the following values:

  • onfulfilled (string): Returns the output audio file's uri upon successful extraction.

  • onrejected (string): Returns a meaningful error message with context if an error occurs during the extraction process.

Note: The format of the output audio file will be .aac, and video file will have a format of .mp4

Usage

import React from 'react';
import {
View,
Button,
Alert,
StyleSheet,
Text,
Dimensions,
} from 'react-native';
import { extractAudioFromVideo } from 'rnvideoeditor';
import { launchImageLibrary } from 'react-native-image-picker';
const ExtractAudio = () => {
const handleExtract = React.useCallback(async () => {
try {
const result = await launchImageLibrary({
mediaType: 'video',
});

if (result.assets && !result.didCancel) {
const fileUri = result.assets[0].uri;

if (fileUri) {
const audioUri = await extractAudioFromVideo(fileUri);
console.log(audioUri);
Alert.alert('Audio Extracted');
}
}
} catch (error) {
console.error('Error during video picking or audio extraction:', error);
}
}, []);

return (
<View style={styles.container}>
<View style={styles.box}>
<Text>Pick Video and extract audio from it</Text>
<Button title="Pick Video and extract Audio" onPress={handleExtract} />
</View>
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
box: {
width: Dimensions.get('window').width - 100,
height: 80,
marginVertical: 20,
},
});