如何在Android中复制带音频的视频 [英] How to dub a video with audio in android

查看:372
本文介绍了如何在Android中复制带音频的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中制作一个配音应用. 该应用程序的流程是:

I want to make a dubbing app in Android. Flow of the app is:

  1. 从图库中获取视频和音频.
  2. 降低视频文件的原始声音.并在该视频文件中混合(复制)选定的音频.
  3. 在此视频文件上混合音频后,将其保存到外部存储器中.

我正在为此使用MediaMuxer,但未成功.请为此提供帮助.

I am using MediaMuxer for this, but m not success. Please help me regarding this.

关于, 普拉泰克

推荐答案

即使我一直在寻找相同的东西来使用mediaMuxer用音频复制我的视频,MediaMuxer对于我来说是一个很难理解的概念,因为我是初学者.我最终引用了这个github代码. https://github.com/tqnst/MP4ParserMergeAudioVideo 这是我的救星.对那个人真的很感谢. 我只是从中获取了我想要的代码,即用我指定的音频复制视频.

even i was looking for the same to dub my video with an audio using mediaMuxer, MediaMuxer was a little difficult concept for me to understand as i am beginner . i ended up refering this github code. https://github.com/tqnst/MP4ParserMergeAudioVideo it was my saviour. really thanx to that person. i just picked up the code i wanted from it, i.e dubbing a video with the audio i specify.

这是我在下面的项目中使用的代码

here is my code i used in my project below

private void mergeAudioVideo(String originalVideoPath,String AudioPath,String OutPutVideoPath) {
    // TODO Auto-generated method stub
    Movie video = null;
    try {
        new MovieCreator();
        video = MovieCreator.build(originalVideoPath);
    } catch (RuntimeException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();

    }

    Movie audio = null;
    try {
        new MovieCreator();
        audio = MovieCreator.build(AudioPath);
    } catch (IOException e) {
        e.printStackTrace();

    } catch (NullPointerException e) {
        e.printStackTrace();

    }
    List<Track> videoTracks = new LinkedList<Track>();
    for (Track t : video.getTracks()) {
        if (t.getHandler().equals("vide")) {
            videoTracks.add(t);
         //seperate the video from the orginal video
        }
    }
    Track audioTrack = audio.getTracks().get(0);// get your audio track to dub the video
    Movie result = new Movie();
    result.addTrack(videoTracks.get(0)); // add the video seprated from the originals
    result.addTrack(audioTrack); //add the track to be put in resul video

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

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(OutPutVideoPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
    try {
        out.writeContainer(byteBufferByteChannel);
        byteBufferByteChannel.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

这是BufferedWritableFileByteChannel类,用于将outputVideo数据写入目录.

and here is the BufferedWritableFileByteChannel class to write the outputVideo data to the directory.

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

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

public BufferedWritableFileByteChannel(OutputStream outputStream) {
    this.outputStream = outputStream;
    this.byteBuffer = ByteBuffer.wrap(rawBuffer);
}

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

    if (inputBytes > byteBuffer.remaining()) {
        dumpToFile();
        byteBuffer.clear();

        if (inputBytes > byteBuffer.remaining()) {
            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);
    }
  }
}

不要忘记在您的项目中添加库.

and dont forget to add the libraries in your project.

这可能不是您所提问题的确切答案.但至少可以为可能的解决方案提供一些启示.

this may not be the exact answer to your question. but atleast it will able to shed some light on the probable solution.

这篇关于如何在Android中复制带音频的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆