2

I'm trying to mix a MP3 audio file into a MP4 video. After hours of searching, I've concluded that I need to convert the MP3 files to an AAC format, which would fit in a MP4 container.

Add audio to video in android

But I can't find any documentation on how to convert the MP3 files into AAC format. So do you have any advice on how I could convert a MP3 audio to an AAC audio ?

Also, I would need to insert several audios at specific times in the final video.

Community
  • 1
  • 1
Xys
  • 8,486
  • 2
  • 38
  • 56

1 Answers1

4

You can try to use mp4parser which does some muxing to mp4, there are some people (as seen from within github repo "issues" part) who are using this to mux mp3 and mp4.

Another option you've got is to use FFmpeg, either compile your self or use something premade, I've used this. Library it self is a bit bulky, plus you might need some playing around to get FFmpeg commands right in order to get optimum quality and mux speed.

It looks something like this:

    try {
       FFmpeg ffmpeg = FFmpeg.getInstance(this);
       String cmd = "-i " + videoFilePath + " -i " + audioFilePath + " -shortest -threads 0 -preset ultrafast -strict -2 " + outputFilePath
       ffmpeg.execute(cmd, mergeListener);
    } catch (FFmpegCommandAlreadyRunningException e) {
       e.printStackTrace();
    }

And a listener:

ExecuteBinaryResponseHandler mergeListener = new ExecuteBinaryResponseHandler() {
        @Override
        public void onStart() {
            //started
        }

        @Override
        public void onFailure(String message) {
            //failed
        }

        @Override
        public void onFinish() {
           File output = new File(outputFilePath);
           //Do whatever with your muxed file
        }
    };
JuliusScript
  • 3,850
  • 1
  • 17
  • 17
  • Thank you very much :) ! I'm going to try that, I'll keep you posted. – Xys Jan 20 '16 at 13:05
  • By the way I've read a bit github repo "issues" part, but I've only seen one topic about MP3, were you refering to this one ? : https://github.com/sannies/mp4parser/issues/73 I think the only answer that seems correct is this one : "You need ‘lame’ to convert mp3 to wav file,and then use an aac encoder to encode wav file to AAC file." What do you think ? Thanks in advance – Xys Jan 20 '16 at 14:32
  • Yeah that's what I meant, there are also few closed issues. But yeah you'll need something like 'lame' or 'ffmpeg' to do decode/encode stuff. FFmpeg does give encode, decode, muxing and a lot more features, although 'lame' seems to be tailored to exactly what you need and thus could be better. I haven't tried 'lame' yet though. – JuliusScript Jan 20 '16 at 18:43
  • Thank you. I'm having huge trouble integrating either lame or ffmpeg in my project. How can I integrate those projects ? I can generate the .so file with the ndk, and load the library like this : static { System.loadLibrary("libname"); } In my app gradle I have somewhere : jniLibs.srcDirs = ['libs'] But nothing works :( – Xys Jan 22 '16 at 12:02
  • For ffmpeg I've linked just download "FFmpegAndroid" and include it as a module in your Android Studio project. In your app build.gradle include compile project(':FFmpegAndroid'), and don't forget to do loadBinary as specified in that repo. Lame repo seems a bit older, so you would need to create/adjust build.gradle file for it. – JuliusScript Jan 22 '16 at 12:27
  • Thank you for your answer ! However I don't understand the line compile project(':FFmpegAndroid'). Are you talking about the project "ffmpeg-android-java" that you have linked or another one ? I saw that in that project there is a "FFmpegAndroid" folder containing the libs. Should I copy that folder in my project directory ? Thank you in advance. – Xys Jan 22 '16 at 13:27
  • I am talking about "FFmpegAndroid" folder - it's module that you need, and compile(":FFmpegAndroid") is for this folder. Also you can add module to project through "File -> New -> Import Module". – JuliusScript Jan 22 '16 at 13:53
  • Thank you, I've finally included the FFmpeg module. However I don't know why, but it seems that all the doc points to execute(String cmd, ...) but for me it shows execute(String[] cmd, ...) . So should I concatenate the cmd with another String ? Thank you again for all, you have been really helpful ! – Xys Jan 25 '16 at 11:51
  • Also, it seems that when executing a FFmpeg command, it fails because the ffmpeg file does not exist in this path : /data/data/com.mycompany.myproject/files/ffmpeg Have you encountered this kind of problem ? EDIT : I needed to use ffmpeg.loadBinary – Xys Jan 25 '16 at 17:11
  • Probably changed to include array of commands, just try using String[]{cmd} – JuliusScript Jan 25 '16 at 17:27
  • Yes that's what I did indeed. Now my new problem is that with the command you provided, it tells me "No such file or directory". Yet I've checked that every file exists before launching the command, I don't understand.. – Xys Jan 25 '16 at 18:12
  • Probably need File file = new File(Environment.getExternalStorageDirectory() + File.separator + "output.mp4"); file.createNewFile(); – JuliusScript Jan 25 '16 at 18:37
  • The problem was that I needed to split the command like that : String[] command = cmd.split(" "); Thank you very much for your help ! – Xys Feb 05 '16 at 15:00
  • Thanks it's working. I need video compress and same time add mp3 audio to mp4 video. Any FFmpeg command for do the above work in single command? – RaJ Oct 22 '20 at 08:11