结合音频和视频-Android [英] Combine Audio with Video - Android

查看:107
本文介绍了结合音频和视频-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于捕获视频和下载音频的应用程序.我能够分别保存这些文件,但是找不到将这些文件组合在一起以制作带有音频的新视频的方法.

I am developing an app to capture video and download audio. I am able to save these files separately but could not find a way to combine these files to make a new video with audio.

视频文件为.mp4格式,音频为.mp3格式.它们的长度完全相同.

Video file is in .mp4 format and audio is in .mp3 format. They have exact same length.

有什么办法可以合并这些文件?我已经尝试过,但是找不到合并两个文件的方法.

Is there any way to combine these files? I have tried but could not find a way to combine two files.

我也可以使用FFMPEG来做到这一点,但是并没有获得实现它的最佳方法.请帮帮我,我是android新手.

Also I can do it with FFMPEG but didn't get any best way how to implement this. Please help me out, I'm new in android.

推荐答案

我认为 MP4Parser 库很合适对于这种情况.我在以下链接中找到了以下示例代码,将音频和视频合并到单个mp4文件中:

I think MP4Parser library is apt for this scenario. I'd found the below sample code to merge audio and video into single mp4 file in this link Video Recording And Processing In Android .

public class Mp4ParserAudioMuxer implements AudioMuxer {
    @Override
    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;
    }
}

如果您正在考虑通过ffmpeg进行操作,则可以使用静态ffmpeg二进制文件您需要对其进行构建.

If you are thinking of doing it via ffmpeg, you can use static ffmpeg binaries or you need to build it.

这篇关于结合音频和视频-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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