合并android中的音频和视频文件

时间:2015-07-04 14:00:45

标签: java android android-videoview mp4parser

我使用mp4parser来合并音频和视频文件,下面是我尝试过的示例,但我在第一行本身得到空指针异常。我已将音频和视频文件保存在手机内存中的所需位置。如果我调试,第一行需要大量的时间和时间只需在空指针错误后停止

try
{
          H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/video.mp4"));
            AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/audio.acc"));

            Movie movie = new Movie();
            movie.addTrack(h264Track);
            movie.addTrack(aacTrack);


            Container mp4file = new DefaultMp4Builder().build(movie);

            FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel();
            mp4file.writeContainer(fc);
            fc.close();

        } catch (Exception ee)
        {
            Toast.makeText(this,ee.getMessage(),Toast.LENGTH_LONG).show();
        }

我的上述代码有什么不对?

1 个答案:

答案 0 :(得分:1)

**试试这个 我已经遵循了很少的东西来合并音频和视频。

  • 1)捕获空白视频不要使用任何音频源 “mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);”
  • 2)将其保存到SD卡
  • 3)它不支持MIME type = mp3。
  • 4)所以如果我们必须合并视频和视频。音频必须是mp4或aac **

5)按钮点击或On Crete

调用方法
  • String audiopath =“/ sdcard / audio.m4a”;

  • String videopath =“/ sdcard / video.mp4”;

  • String outputpath =“/ sdcard / output.mp4”;

  • mux(视频,音频,输出);

6)主代码在这里仅传递存储视频,音频(m4a,aac),输出路径的路径。

 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();
        Log.e("Audio Video", "11");
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

private static class BufferedWritableFileByteChannel implements WritableByteChannel {
    //    private static final int BUFFER_CAPACITY = 1000000;
    private static final int BUFFER_CAPACITY = 10000000;

    private boolean isOpen = true;
    private final OutputStream outputStream;
    private final ByteBuffer byteBuffer;
    private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];

    private BufferedWritableFileByteChannel(OutputStream outputStream) {
        this.outputStream = outputStream;
        this.byteBuffer = ByteBuffer.wrap(rawBuffer);
        Log.e("Audio Video", "13");
    }

    @Override
    public int write(ByteBuffer inputBuffer) throws IOException {
        int inputBytes = inputBuffer.remaining();

        if (inputBytes > byteBuffer.remaining()) {
            Log.e("Size ok ", "song size is ok");
            dumpToFile();
            byteBuffer.clear();

            if (inputBytes > byteBuffer.remaining()) {
                Log.e("Size ok ", "song size is not okssss ok");
                throw new BufferOverflowException();
            }
        }

        byteBuffer.put(inputBuffer);

        return inputBytes;
    }

    @Override
    public boolean isOpen() {
        return isOpen;
    }

    @Override
    public void close() throws IOException {
        dumpToFile();
        isOpen = false;
    }

    private void dumpToFile() {
        try {
            outputStream.write(rawBuffer, 0, byteBuffer.position());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}