【问题标题】:FFmpeg audio video merge commandFFmpeg 音视频合并命令
【发布时间】:2017-10-20 08:34:35
【问题描述】:

你能告诉我,下面的命令是否正确 一个用于视频音频合并?

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

    }




}


}

【问题讨论】:

    标签: android ffmpeg


    【解决方案1】:

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

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

    【讨论】:

    • 问题已更新为完整代码和问题描述。您能检查一下吗?
    • @djac 是ffmpeg 命令本身的问题还是您的代码有问题?显示实际发出的ffmpeg 命令以及该命令的完整日志。
    • 无法链接可执行文件“/data/data/xxxxxffmpeg”:有文本重定位。这是错误,我的 gradle 中有“compile 'com.writingminds:FFmpegAndroid:0.3.2”。
    • @djac 这是实际问题,您的问题标题应该是什么。我建议编辑您的原始问题并将其作为主题。在这种情况下我无能为力,因为我对 Android 一无所知,也不知道什么是“gradle”。
    【解决方案2】:

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

      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命令的输出文件路径。您可以根据需要更改所需的目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-26
      • 2019-10-24
      • 2019-09-06
      • 2012-05-24
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多