Extract Audio In Mp3 Format From Video In Android

0
43

STEP 1:

create a class called AudioUtils. Paste the below.

public class AudioUtils {

private static final String TAG = "AudioUtils";
private static final int DEFAULT_BUFFER_SIZE = 1 * 1024 * 1024;

public void genVideoUsingMuxer(FileDescriptor srcPath, FileDescriptor dstPathFd, String dstPath, int startMs, int endMs, boolean useAudio, boolean useVideo) throws IOException {
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
muxer = new MediaMuxer(dstPathFd, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} else {
muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
}

// Set up the tracks and retrieve the max buffer size for selected
// tracks.
HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
int bufferSize = -1;
for (int i = 0; i < trackCount; i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); boolean selectCurrentTrack = false; if (mime.startsWith("audio/") && useAudio) { selectCurrentTrack = true; } else if (mime.startsWith("video/") && useVideo) { selectCurrentTrack = true; } if (selectCurrentTrack) { extractor.selectTrack(i); int dstIndex = muxer.addTrack(format); indexMap.put(i, dstIndex); if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) { int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); bufferSize = newSize > bufferSize ? newSize : bufferSize;
}
}
}
if (bufferSize < 0) { bufferSize = DEFAULT_BUFFER_SIZE; } // Set up the orientation and starting time for extractor. MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever(); retrieverSrc.setDataSource(srcPath); String degreesString = retrieverSrc.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); if (degreesString != null) { int degrees = Integer.parseInt(degreesString); if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}
}
if (startMs > 0) {
extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
}
// Copy the samples from MediaExtractor to MediaMuxer. We will loop
// for copying each sample and stop when we get to the end of the source
// file or exceed the end time of the trimming.
int offset = 0;
int trackIndex = -1;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
muxer.start();
while (true) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) { Log.d(TAG, "Saw input EOS."); bufferInfo.size = 0; break; } else { bufferInfo.presentationTimeUs = extractor.getSampleTime(); if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
Log.d(TAG, "The current sample is over the trim end time.");
break;
} else {
bufferInfo.flags = extractor.getSampleFlags();
trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
extractor.advance();
}
}
}
muxer.stop();
muxer.release();
return;
}

}

STEP 2

String destPath = Environment.getExternalStoragePublicDirectory("") + "/AudFolder2/asd.mp3"
try {
        FileDescriptor outputAudioFileDescriptor = getActivity().getContentResolver().openFileDescriptor(outputAudioUri, "w").getFileDescriptor();
        
  FileDescriptor inputVideoFileDescriptor = getActivity().getContentResolver().openFileDescriptor(Uri.parse(inputVideoUri), "r").getFileDescriptor();

        new AudioUtils().genVideoUsingMuxer(pfdVideo.getFileDescriptor(), pfdAudio.getFileDescriptor(), destPath, -1, -1, true, false);
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, "saveAudioToDevice: ERROR = "+ e.getMessage() );
    }
  

LEAVE A REPLY

Please enter your comment!
Please enter your name here