How to mux mp3 and mp4 files in Android

Problem: I am developing an Android application where I have to get images from the user's gallery, combine them to create a video file and add background music. I have used jcodec library to create mp4 video files using my image resources (I know how to get images from gallery and show them in ImageViews and create bitmaps from them). I have an mp4 file. Now I want to add background music to it using mp3 file.

Please help me with this? I cannot add it using jcodec or at least I don’t know how. I can use any library that can do the job, but it has to be in Java because I don't want to use ndk. I tried ffmpeg and opencv for java but increased the size of my app by over 15MB and jcodec has already done half the work of generating the mp4 file.

I can give you the code that I used to create the mp4 file. Thanks in advance.

0


source to share


1 answer


I think it JCODEC

doesn't support adding mp3 to MP4 . But JAVACV

Support add mp3 to mp4 .

download JAVACV

from this link https://github.com/bytedeco/javacv .

support for this library. Create video from images and also add mp3 to mp4 .



code to add mp3 to mp4.

Note. It may not work for creating mp4 from JCODEC, create mp4 with JAVACV

FrameGrabber grabber1 = new FFmpegFrameGrabber(videoPath);
            FrameGrabber grabber2 = new FFmpegFrameGrabber(audioPath);
            grabber1.start();
            grabber2.start();
            FrameRecorder recorder = new FFmpegFrameRecorder(OutputPath,
                    grabber1.getImageWidth(), grabber1.getImageHeight(), 2);
            recorder.setFormat("mp4");
            recorder.setVideoQuality(1);
            recorder.setFrameRate(grabber1.getFrameRate());
            recorder.setSampleRate(grabber2.getSampleRate());
            recorder.start();
            Frame frame1, frame2 = null;
            long timestamp = -2;
            int count = 0;
            boolean isFirstTime = false;
            boolean isFirstCheck = true;
            while ((frame1 = grabber1.grabFrame())!=null) {
                //frame1 = grabber1.grabFrame();
                frame2 = grabber2.grabFrame();
                recorder.record(frame1);
                recorder.record(frame2);

                }
            recorder.stop();
            grabber1.stop();
            grabber2.stop();
    } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
            e.printStackTrace();
        } catch (Exception e1) {

        }

      

+1


source







All Articles