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

6

我在Ubuntu服务器上从Java程序执行ffmpeg命令时遇到错误。当我在putty中执行时,它可以成功执行,但是从Java中执行时会抛出异常:

java.io.IOException: Cannot run program "/usr/bin/ffmpeg ": 
error=2, No such file or directory

以下是我的代码:
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;

}

ffmpeg命令的输出:

/usr/bin/ffmpeg -y -i /mydata/clip1.3gp -acodec copy /mydata/clip1eg.mp4

当我在putty中运行上述命令时,它成功执行,但从Java程序中无法成功执行。
在程序中,我还尝试了以下方法,但没有成功。
usr/bin/ffmpeg
/bin/ffmpeg
ffmpeg
/root/usr/bin/ffmpeg

请告诉我我做错了什么。

谢谢。


1
我现在也遇到了同样的问题,你找到了解决办法吗? - Pablote
3个回答

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]

它应该更像这样:

0: [/usr/bin/ffmpeg]
1: [-y]
2: [-i]
3: [/mydata/clip1.3gp]
4: [-acodec]
5: [copy]
6: [/mydata/clip1eg.mp4]

所以发布的代码应该更改为:
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;

用户是否可以执行二进制文件? - SubOptimal

1

要查找ffmpeg的完整路径,请从Putty运行以下命令:

  which ffmpeg

如果安装的方式是rpm包,那么默认路径是/usr/bin/ffmpeg

是的,它是 /usr/bin/ffmpeg,但文件仍未转换。 - Madan Madan

0

我也遇到了这个问题,并通过以下方式解决:

  1. 首先,我的 ffmpeg/home/ffmpeg/ffmpeg 中,在 shell 中运行正常,但在我的 Java 程序中出现错误:java.io.IOException: Cannot run program “usr/bin/ffmpeg ”: error=2, No such file or directory

  2. 然后,我将其添加到 /usr/local/bin/ffmpeg 并链接到 /home/ffmpeg/ffmpeg,如下所示,它就可以正常工作了。

[webedit bin]$ pwd.   
/usr/local/bin
[webedit bin]$ ls -lh
total 1.6M
lrwxrwxrwx 1 root root   19 Jun  8 09:34 ffmpeg -> /home/ffmpeg/ffmpeg
lrwxrwxrwx 1 root root   20 Jun  8 09:34 ffprobe -> /home/ffmpeg/ffprobe

你可以试一试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接