简体   繁体   中英

FFmpeg audio video merge command

Can you let me know , whether the below command is the correct one for video audio merge ?

vabsolutePath= video file absolute path (only video),
aabsolutePath= audio file absolute path (only audio)

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

The below code is used in android for merging. Here the issue is the code is executing, but the output merge file "result.mp4" is not playable/not produced. Could you please help to find out the issue in code/ command?

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";

    }




}


}

While it is not possible to give you a concrete answer due to lacking details I will say yes , assuming:

  • Your ffmpeg command is implemented properly in your code and provides no errors.
  • You are outputting to an appropriate output format container for your video and audio formats.
  • The vabsolutePath contains only video and aabsolutePath contains only audio. Otherwise use the -map option to manually select streams instead of relying on the default stream selection behavior. Otherwise it may select the wrong streams.

Try creating destination(output) file in the below way-

  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();

        }

Use dest.getAbsolutePath() as an output file path to ffmpeg command.You can change desired directory as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM