FFmpeg音频视频合并命令

时间:2017-10-20 08:34:36

标签: android ffmpeg

您可以告诉我,以下命令是否正确 一个用于视频音频合并?

vabsolutePath =视频文件绝对路径(仅限视频),
aabsolutePath =音频文件绝对路径(仅音频)

String ccommand[] = {"-i",vabsolutePath,"-i",aabsolutePath, "-c:v", "copy", "-c:a", "aac","-shortest", dabsolutePath};

以下代码在android中用于合并。 这里的问题是代码正在执行,但输出合并文件“result.mp4”无法播放/不生成。 你能帮忙找出代码/命令中的问题吗?

public class Mrge  extends AppCompatActivity {

    private Button var_button_save,var_button_send;
    Uri vuri=null;
    public String vabsolutePath=null,  aabsolutePath=null, dabsolutePath=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_layout);

        OutputStream out;

        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            InputStream ins = getResources().openRawResource(
                    getResources().getIdentifier("anvkl",
                            "raw", getPackageName()));


            byte[] buf = new byte[1024];
            int n;
            while (-1 != (n = ins.read(buf)))
                stream.write(buf, 0, n);

            byte[] bytes = stream.toByteArray();

            String root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
            File createDir = new File(root + "master" + File.separator);

            createDir.mkdir();


            File file = new File(root + "master" + File.separator + "master.mp4");


            file.createNewFile();
            out = new FileOutputStream(file);
            out.write(bytes);
            out.close();



            vabsolutePath = file.getAbsolutePath();

            //-------------------------------------------------------------------

            ins = getResources().openRawResource(
                    getResources().getIdentifier("audio",
                            "raw", getPackageName()));

            while (-1 != (n = ins.read(buf)))
                stream.write(buf, 0, n);

            bytes = stream.toByteArray();


            root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
            createDir = new File(root + "audio" + File.separator);
            createDir.mkdir();


            file = new File(root + "audio" + File.separator + "audio.aac");

            file.createNewFile();
            out = new FileOutputStream(file);
            out.write(bytes);
            out.close();

            aabsolutePath = file.getAbsolutePath();

            root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
            createDir = new File(root + "result" + File.separator);
            createDir.mkdir();


            file = new File(root + "result" + File.separator + "result.mp4");

            file.createNewFile();

            dabsolutePath = file.getAbsolutePath();


            //------------------------------------------------------------------------






        } catch (IOException e) {
            e.printStackTrace();
        }
        String ccommand[] = {"-y", "-i",vabsolutePath,"-i",aabsolutePath, "-c:v", "copy", "-c:a", "aac","-shortest", dabsolutePath};

        loadFFMpegBinary();
        execFFmpegBinary(ccommand);

    }






        FFmpeg ffmpeg;
        private void loadFFMpegBinary() {
            try {
                if (ffmpeg == null) {

                    ffmpeg = FFmpeg.getInstance(this);
                }
                ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                    @Override
                    public void onFailure() {
                        //showUnsupportedExceptionDialog();
                    }

                    @Override
                    public void onSuccess() {

                    }
                });
            } catch (FFmpegNotSupportedException e) {
                //showUnsupportedExceptionDialog();
            } catch (Exception e) {

            }
        }




    private void execFFmpegBinary(final String[] command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler()
            {
                @Override
                public void onFailure(String s) {

                }

                @Override
                public void onSuccess(String s) {


                }


            @Override
            public void onProgress(String s) {

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {


            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {

            String m="hi";

    }




}


}

2 个答案:

答案 0 :(得分:1)

虽然由于缺乏细节而无法给出具体答案,但我会说,假设:

  • 您的ffmpeg命令已在您的代码中正确实现,并且不会出现任何错误。
  • 您正在输出适合您的视频和音频格式的输出格式容器。
  • vabsolutePath仅包含视频,aabsolutePath仅包含音频。否则,请使用-map选项手动选择流,而不是依赖于默认的stream selection行为。否则可能会选择错误的流。

答案 1 :(得分:0)

尝试以下面的方式创建目标(输出)文件 -

  File moviesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES
        );
        boolean success = true;
        if (!moviesDir.exists()) {
            success = moviesDir.mkdir();
        }
        if(success) {
            File dest = new File(moviesDir, filePrefix + fileExtn);
            int fileNo = 0;
            while (dest.exists()) {
                fileNo++;
                dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
            }
         ....//execute ffmpeg command with dest.getAbsolutePath() as an output file path

        }else
        {
            Snackbar.make(mainlayout, "Unable to create directory", 4000).show();

        }

使用dest.getAbsolutePath()作为ffmpeg命令的输出文件路径。您可以根据需要更改所需的目录。