No audio in MP4 file - Android

2019-01-27 04:30发布

I am using the MP4ParserMergeAudioVideo to add a new audio to an MP4 file. The library had said to use .wav file, if i keep a .wav file in the directory and give the name of the audiofile as example.m4a, I get a file not found exception. So I changed the file to a .m4a audio file. The code is given below.

findViewById(R.id.append).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        String root = Environment.getExternalStorageDirectory().toString();
        Log.e("",""+root);
        String audio = root + "/download/"+"mymovieaudio.m4a";
        String video = root + "/download/"+"My_Movie.mp4";
        String output = root + "/download/ouput.mp4";
        mux(video, audio, output);
    }
});

The mux function is like this

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(0);
        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;
    }

I successfully get the output.mp4 file in my download directory of my device. The problem is that it does not have any audio. Please help. Thanks in advance.

0条回答
登录 后发表回答