简体   繁体   English

在Android中将音频文件与捕获的视频合并

[英]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. 我的要求是:一个可以录制视频(mp4)的android应用程序,然后我的本地文件夹中有一些音频文件(mp3),我想将其添加到通过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. PS Merge必须在内部完成。摄像机不会录制任何音频。

Please help 请帮忙

I couldn't succeeded by using mp3 files.Instead I use an mp4 file for the background audio. 我无法使用mp3文件成功,而是使用mp4文件作为背景音频。 If you are interested to deal with mp4 files,the below code snippet may help you 如果您有兴趣处理mp4文件,下面的代码段可能会对您有所帮助

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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