我在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
请告诉我我做错了什么。
谢谢。