Extract audio track from mp4 and save it to a playable audio file Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Extract audio track from mp4 and save it to a playable audio file without wasting too much if your time.

The question is published on by Tutorial Guruji team.

The following code is my work which can extract the audio track and save it to a file in android. However, I don’t know how to encode the audio track to be a playable audio file (e.g., .m4a or .aac). In addition, I also read the format information of the audio track {mime=audio/mp4a-latm, aac-profile=2, channel-count=2, track-id=2, profile=2, max-input-size=610, durationUs=183600181, csd-0=java.nio.HeapByteBuffer[pos=0 lim=2 cap=2], sample-rate=44100}.

            MediaExtractor extractor = new MediaExtractor();

            try
            {
                extractor.setDataSource("input.mp4");

                int numTracks = extractor.getTrackCount();
                int audioTrackIndex = -1;

                for (int i = 0; i < numTracks; ++i)
                {
                    MediaFormat format = extractor.getTrackFormat(i);
                    String mime = format.getString(MediaFormat.KEY_MIME);

                    if (mime.equals("audio/mp4a-latm"))
                    {
                        audioTrackIndex = i;
                        break;
                    }
                }

                // Extract audio
                if (audioTrackIndex >= 0)
                {
                    ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
                    File audioFile = new File("output_audio_track");
                    FileOutputStream audioOutputStream = new FileOutputStream(audioFile);

                    extractor.selectTrack(audioTrackIndex);

                    while (true)
                    {
                        int readSampleCount = extractor.readSampleData(byteBuffer, 0);

                        if (readSampleCount < 0)
                        {
                            break;
                        }

                        // Save audio file
                        byte[] buffer = new byte[readSampleCount];
                        byteBuffer.get(buffer);
                        audioOutputStream.write(buffer);
                        byteBuffer.clear();
                        extractor.advance();
                    }

                    audioOutputStream.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                extractor.release();
            }

Answer

To extract the audio stream without re-encoding:

ffmpeg -i input-video.mp4 -vn -acodec copy output-audio.aac

-vn is no video. -acodec copy says use the same audio stream that’s already in there.

Read the output to see what codec it is, to set the right filename extension.

We are here to answer your question about Extract audio track from mp4 and save it to a playable audio file - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji