java.io.IOException:无法运行程序“usr/bin/ffmpeg”:错误=2,没有那个文件或目录

Mad*_*dan 5 java linux ubuntu ffmpeg

我在 Ubuntu 服务器中从 java 程序执行 ffmpeg 命令时遇到错误。当我在 putty 中执行它成功执行但从 java 它给了我例外

java.io.IOException: Cannot run program "/usr/bin/ffmpeg ": 
error=2, No such file or directory
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

public String convert3gpTomp4(File contentFile, String filename) {
    String[] cmd = new String[6];
    filename = StringUtils.substringBefore(filename, ".");      
    cmd[0] =  "/usr/bin/ffmpeg ";
    cmd[1] = "-y ";
    cmd[2] = "-i ";
    cmd[3] = contentFile.getPath();
    cmd[4] = "  -acodec copy ";
    String myfilename = filename +"eg.mp4";
    cmd[5] = contentFile.getParent() + "/" + myfilename;        

    if (execute(cmd)){
            return myfilename;
    }else{      
        return null;
    }

   }
}

public boolean execute(String[] cmd){
    try{
        Runtime rt= Runtime.getRuntime();

        Process proc = rt.exec(cmd);

        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
        errorGobbler.start();
        outputGobbler.start();

        int exitVal = proc.waitFor();
        String sb = outputGobbler.sb.toString();
        String eb = errorGobbler.sb.toString();

        System.out.println("Command Exceute Exit value: " + exitVal);

        proc.destroy();

        return true;
    }
    catch(java.io.IOException e ){System.out.println("IOException "+e);e.printStackTrace();}
    catch(java.lang.InterruptedException e){}

    return false;

}
Run Code Online (Sandbox Code Playgroud)

ffmpeg 命令的输出:

/usr/bin/ffmpeg -y -i /mydata/clip1.3gp -acodec copy /mydata/clip1eg.mp4
Run Code Online (Sandbox Code Playgroud)

当我在 putty 中运行上面的命令时,它成功执行,但来自 Java 程序。

在程序中,我也尝试了以下但没有成功。

usr/bin/ffmpeg
/bin/ffmpeg
ffmpeg
/root/usr/bin/ffmpeg
Run Code Online (Sandbox Code Playgroud)

请让我知道我哪里做错了。

谢谢,

Sub*_*mal 1

您必须删除二进制文件后的尾随空白

将此更改"/usr/bin/ffmpeg ""/usr/bin/ffmpeg";

编辑

对我来说,问题似乎是可执行文件名称有一个尾随空白,并且参数没有按预期传递给被调用的进程。

0: [/usr/bin/ffmpeg ]   <-- traling blank makes a problem
1: [-y ]   <-- unsure if the trailing blank would make problem there
2: [-i ]   <-- unsure if the trailing blank would make problem there
3: [/mydata/clip1.3gp]
4: [  -acodec copy ]   <-- this should passed as two parameters to the process
5: [/mydata/clip1eg.mp4]
Run Code Online (Sandbox Code Playgroud)

它应该更像是

0: [/usr/bin/ffmpeg]
1: [-y]
2: [-i]
3: [/mydata/clip1.3gp]
4: [-acodec]
5: [copy]
6: [/mydata/clip1eg.mp4]
Run Code Online (Sandbox Code Playgroud)

所以对发布的代码的更改应该是

String[] cmd = new String[7];
...
cmd[0] = "/usr/bin/ffmpeg";
cmd[1] = "-y";
cmd[2] = "-i";
cmd[3] = contentFile.getPath();
cmd[4] = "-acodec";
cmd[5] = "copy";
String myfilename = filename + "eg.mp4";
cmd[6] = contentFile.getParent() + "/" + myfilename;
Run Code Online (Sandbox Code Playgroud)