How can I convert audio .mp3 to .m4a programmatically in android
Asked Answered
P

1

7

I don't want to use ffmpeg. Currently I was able to mux m4a audio with mp4 video. I wish to add mp3 audio also which required mp3 to m4a conversion.I was able to convert wav to m4a but not mp3 to m4a with below code

Here is my code.

 private void convertAudio(String filename) throws IOException {

    String outputpath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/converted.m4a";
    // Set up MediaExtractor to read from the source.

    MediaExtractor extractor = new MediaExtractor();
    extractor.setDataSource(filename);


    int trackCount = extractor.getTrackCount();

    // Set up MediaMuxer for the destination.
    MediaMuxer muxer;
    muxer = new MediaMuxer(outputpath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    // Set up the tracks.
    HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
    for (int i = 0; i < trackCount; i++) {
        extractor.selectTrack(i);
        MediaFormat format = extractor.getTrackFormat(i);
        format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AMR_NB);

        int dstIndex = muxer.addTrack(format);
        indexMap.put(i, dstIndex);
    }
    // Copy the samples from MediaExtractor to MediaMuxer.
    boolean sawEOS = false;
    int bufferSize = 32000;
    int frameCount = 0;
    int offset = 100;
    ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
   /* if (degrees >= 0) {
        muxer.setOrientationHint(degrees);
    }*/
    // Test setLocation out of bound cases

    muxer.start();
    while (!sawEOS) {
        bufferInfo.offset = offset;
        bufferInfo.size = extractor.readSampleData(dstBuf, offset);
        if (bufferInfo.size < 0) {

            sawEOS = true;
            bufferInfo.size = 0;
        } else {
            bufferInfo.presentationTimeUs = extractor.getSampleTime();
            bufferInfo.flags = extractor.getSampleFlags();
            int trackIndex = extractor.getSampleTrackIndex();
            muxer.writeSampleData(indexMap.get(trackIndex), dstBuf,
                    bufferInfo);
            extractor.advance();
            frameCount++;

        }
    }
    muxer.stop();
    muxer.release();

    return;
}
Plyler answered 2/11, 2015 at 10:14 Comment(4)
currently i was working to merge audio and video with media muxer but i failed can u plz help me by sharing your merged codeMellisa
here is my issue: Unknown mime type 'audio/mpeg'. 12-09 11:58:33.569: A/MPEG4Writer(332): frameworks/av/media/libstagefright/MPEG4Writer.cpp:2699 CHECK(!"should not be here, unknown mime type.")Mellisa
Do you find a way to convert mp3 to m4a? If you have done, just share it, thx.Pitiful
The confusion with this question is that mp3 and m4a are two different things. Mp3 is an audio codec and m4a is just another file extension for the mp4 container. Although AAC is the most common audio codec used in mp4, it is perfectly valid to put mp3s in an mp4. So no conversion necessary. Just mux the mp3 into the mp4 container.Dennison
B
0

It would appear that the answer to this Question (as of now) is that there is no publicly available (i.e. searchable) free solution for converting "mp3" to "m4a" on Android that doesn't use "ffmpeg".

However, this doesn't mean it cannot be done:

  • Someone should be able to read the relevant specification documents and then implement the transformation; i.e. write a bunch of Java code. Indeed, the the existence of ffmpeg itself proves that this is technically feasible. And legally too.

  • Someone could have already done the work, and chosen not to share it with others.

Why isn't there a free alternative?

Probably ... because there is nobody prepared to write one. Probably, because the kind of people who would be inclined to do so for free don't see the point of reinventing the wheel!

So what should you do?

I presume that you want / need an "ffmpeg-free" implementation because of the licensing constraints of LGPL / GPL. That's cool. But if you don't want to fit in with the open-source way of doing it (and LGPL is not onerous) then you need to solve the problem for your self (or yourselves). The alternatives are:

  1. Write the software your self (or yourselves).
  2. Pay someone to do the coding work for you.
  3. Find an existing commercial implementation and pay for a license.

If you (individually or collectively) do 1 or 2, then you could chose to make the results of your labor or investment available to others, for free (under whatever license you choose) or for money.

Bondholder answered 13/8, 2017 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.