ProcessBuilder构造函数参数如何工作?

时间:2014-09-19 20:31:50

标签: java ffmpeg

我这样做:

String[] command = {ffmpegLoc+"ffmpeg.exe",
            "-i ",
            "\""+dir+params.getString(4)+".flv"+"\"",
            "-copyts",
            "-crf 18",
            "-profile:v baseline",
            "-level 3.0",
            "-pix_fmt yuv420p",
            "-preset veryslow",
            "\""+dir+params.getString(4)+".mp4"+"\""};
    try {
        getLogger().info("ffmpeg command " + command);
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        getLogger().info("Starting process");
        Process process = builder.start();
        InputStream stream =  process.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while((line = in.readLine()) != null) {
          System.out.println(line);
        }
        process.waitFor();

我从ffmpeg得到这个错误,它正确初始化:

Unrecognized option 'i '.
Error splitting the argument list: Option not found

所以我想“也许我不需要将每个参数拆分成数组”

所以我用这个替换了命令:

String[] command = {ffmpegLoc+"ffmpeg.exe",
                "-i " + "\""+dir+params.getString(4)+".flv"+"\"" + " -copyts -crf 18 -profile:v baseline -level 3.0 -pix_fmt yuv420p -preset veryslow "+"\""+dir+params.getString(4)+".mp4"+"\""};

现在我明白了:

Unrecognized option 'i C:/Program'.
Error splitting the argument list: Option not found

i之前的连字符发生了什么?

那些包含.flv文件路径的双引号会发生什么变化?

这里发生了什么?

编辑:

我根据this回答删除了转义的双引号,现在我明白了:

Unrecognized option 'i C:/Program Files (x86)/Wowza Media Systems/Wowza Streaming Engine 4.1.0/content/recorder/vid_test001.flv -copyts -crf 18 -profile:v baseline -level 3.0 -pix_fmt yuv420p -preset veryslow C:/Program Files (x86)/Wowza Media Systems/Wowza Streaming Engine 4.1.0/content/recorder/vid_test001.mp4'.
Error splitting the argument list: Option not found

同样,i前面的连字符丢失了。

编辑2:

让我们将它们结合起来:没有转义的双引号与各自的索引相结合:

String[] command = {ffmpegLoc+"ffmpeg.exe",
            "-i",
            dir+params.getString(4)+".flv",
            "-copyts", 
            "-crf 18",
            "-profile:v baseline",
            "-level 3.0",
            "-pix_fmt yuv420p",
            "-preset veryslow",
            dir+params.getString(4)+".mp4"};
现在ffmpeg给了我:

Unrecognized option 'crf 18'.

所以我们到达了第一个空白区,然后失败了。

1 个答案:

答案 0 :(得分:3)

当你创建一个字符串数组时,你假设如果你在命令行执行shell会做的一些工作。这就是为什么双引号是不必要的。但是您需要将选项名称和选项值拆分为单独的字符串;那就是shell如何将它们传递给命令。所以,试试这个:

String[] command = { ffmpegLoc+"ffmpeg.exe",
                     "-i", dir+params.getString(4)+".flv",
                     "-copyts",
                     "-crf", "18",
                     "-profile:v", "baseline",
                     "-level", "3.0",
                     "-pix_fmt", "yuv420p",
                     "-preset", "veryslow",
                     dir+params.getString(4)+".mp4"};