Madan Madan
Madan Madan

Reputation: 684

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

I am experiencing errors when executing ffmpeg command from java program in Ubuntu server. When I execute in putty it executes successfully but from java it gives me the exceptions of

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

My Code below:

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;

}

Output of ffmpeg command:

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

When I run above command in putty , it executes successfully but from Java program.

In the program, I also tried with the following but no success.

usr/bin/ffmpeg
/bin/ffmpeg
ffmpeg
/root/usr/bin/ffmpeg

Please let me know where I am doing wrong.

Thanks,

Upvotes: 6

Views: 14936

Answers (3)

chou
chou

Reputation: 374

I have this problem too and solve it by:

  1. First my ffmpeg in /home/ffmpeg/ffmpeg, when run in shell it works but in my java program, it goes wrong java.io.IOException: Cannot run program “usr/bin/ffmpeg ”: error=2, No such file or directory

  2. then i add it to /usr/local/bin/ffmpeg and link to /home/ffmpeg/ffmpeg as follow, it works.

[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

you can have a try

Upvotes: 0

SubOptimal
SubOptimal

Reputation: 22983

You must remove the trailing blank after the binary

change "/usr/bin/ffmpeg " this to "/usr/bin/ffmpeg";

edit

For me the problem seems to be that the executable name has a trailing blank and the parameters are not passed as expected to the called process.

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]

It should be rather like

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

So the change to the posted code should be

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;

Upvotes: 1

gromi08
gromi08

Reputation: 515

To find the full path of ffmpeg run the following command from your putty:

  which ffmpeg

The default path if installation was by rpm package is /usr/bin/ffmpeg.

Upvotes: 1

Related Questions