简体   繁体   中英

Merge audio file with captured video in android

Is it any possible to merge audio and video file into single file. My requirement is: An android application which can record a video (mp4), then I have some audio files (mp3) in my local folder, and I want to add it to the video that is captured via camcoder. So the final effect will be like the video is played and the added audio will be heard which is to be merged internally. how can I do it? PS Merge has to done internally.Camcorder will not record any audio.

Please help

I couldn't succeeded by using mp3 files.Instead I use an mp4 file for the background audio. If you are interested to deal with mp4 files,the below code snippet may help you

String root = Environment.getExternalStorageDirectory().toString();
            String audio = root +"/test.mp4";
            String video = root +"/myvideo.mp4";
            String output = root+ "/ouputVideo.mp4";
            Log.e("FILE", "audio:"+audio + " video:"+video+ " out:"+output);
            mux(video, audio, output);



public boolean mux(String videoFile, String audioFile, String outputFile) {
    Movie video;
    try {
        video = new MovieCreator().build(videoFile);
    } catch (RuntimeException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    Movie audio;

    try {
        audio = new MovieCreator().build(audioFile);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (NullPointerException e) {

        e.printStackTrace();
        return false;
    }

    Track audioTrack = audio.getTracks().get(1);

    //video.getTracks().remove(1);
    video.addTrack(audioTrack);

    Container out = new DefaultMp4Builder().build(video);

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
    try {
        out.writeContainer(byteBufferByteChannel);
        byteBufferByteChannel.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM