java - 在android中合并音频和视频文件

标签 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)捕获空白视频不要使用任何音频源,例如 “mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);”
  • 2)保存到SD卡
  • 3)不支持MIME type=mp3。
  • 4)所以如果我们必须合并视频和视频。音频必须是 mp4 或 aac .**

5)在按钮单击或在克里特岛调用方法

  • 字符串音频路径 = "/sdcard/audio.m4a";

  • 字符串视频路径 = "/sdcard/video.mp4";

  • 字符串输出路径 = "/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);
        }
    }
}

关于java - 在android中合并音频和视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221467/

相关文章:

java - Java HashMap的机制

java - Docker 统计 100% 内存

java - Domino OSGi 插件抛出异常 java.lang.NoClassDefFoundError : org. apache.commons.logging.Logfactory

java - 覆盖 Java 中的 public final 方法(反射?!)

android - 我想从数据库中获取数据并将其显示在 ListView 上。

Android - 如何使用 uri 源在 VideoView 上设置缩略图?

android - 视频不会在 VideoView 中恢复

java - 如何使 Jersey Rest POST 请求同步

java - 通过android应用程序获取执行命令的输出

Android VideoView 及时跳转