-1

I am writing a program in Java that uses ffmpeg to "snip" a video into several pieces and the stitch them back together again. I have everything working relatively smoothly in Windows, but I cannot get ffmpeg to work in Mac, or in Linux for that matter. I'm focusing on mac right now though. I thought that it might be a permissions problem, but when I run it with sudo I get an error that says (after typing in the password:

sudo: ffmpeg: command not found

when I run it without sudo I get:

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

I think that it might be because the ffmpeg package, on the Mac machine, was downloaded with homebrew, and ffmpeg is stored in /usr/local/Cellar/ffmpeg instead of the default folder, wherever it may be. That may not be the problem though, because I deleted ffmpeg and re-downloaded it with homebrew. It may have been in its defaulter folder in my first tests as well. It would be great to figure this out. Most of my family uses Mac (not me) and I really want to share my work with them. That is why I chose to code this in Java. Oh, and I did try using the directory to the binary in the command. Here's the code:

    //snips out all the clips from the main video
    public void snip() throws IOException, InterruptedException {
        
        for(int i = 0; i < snippets.size(); i++) {
            //ffmpeg -i 20sec.mp4 -ss 0:0:1 -to 0:0:5 -c copy foobar.mp4
            String newFile = "foobar" + String.valueOf(i) + ".mp4";
            
            //THIS WORKS
            if(OS.isWindows()) {
                ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoName, "-ss",
                        snippets.get(i).getStartTime(), "-to", snippets.get(i).getEndTime(), newFile);
                            
                Process process = processBuilder.inheritIO().start();
                process.waitFor();
                System.out.println("Win Snip " + i + "\n");
            }
            
            else if (OS.isMac()) {
                //FFMPEG LOCATION: /usr/local/Cellar/ffmpeg
                //THE ERROR: sudo: ffmpeg: command not found
                //ERROR W/OUT SUDO: java.io.IOException: Cannot run program "ffmpeg": error=2, No such file or directory
                ProcessBuilder processBuilder = new ProcessBuilder("sudo", "-S", "ffmpeg", "-f", videoName, "-ss",
                        snippets.get(i).getStartTime(), "-to", snippets.get(i).getEndTime(), newFile);
                
                Process process = processBuilder.inheritIO().start();
                process.waitFor();
                System.out.println("Mac Snip " + i + "\n");
            }
            
            else if (OS.isUnix()) {
                System.out.println("Your operating system is not supported");
                //TODO
                //need to figure out if deb/red hat/whatever are different
            }
            
            else if (OS.isSolaris()) {
                System.out.println("Your operating system is not supported yet");
                //TODO probably won't do
            }
            
            else {
                 System.out.println("Your operating system is not supported");
            }
            //add to the list of files to be concat later
            filesToStitch.add(newFile);
            filesToDelete.add(newFile);
            
        }
        //System.out.println(stitchFiles);
    }
3
  • 1
    Why don't you just use the full path to the binary in that case? You might find then that you don't even need to use sudo Commented Aug 3, 2020 at 20:23
  • Yea, that didn't solve my problem Commented Aug 4, 2020 at 0:49
  • See this answer. Commented Dec 10, 2024 at 7:12

2 Answers 2

0

As Mac OS is UNIX-based, you need to put "./" before the executable name if the location isn't in the configured path (i.e. the $PATH environment variable). Changing "ffmpeg" to "./ffmpeg" should hopefully work (assuming that it is genuinely located in the working directory -- you can also change the working directory by calling directory() on your ProcessBuilder if necessary).

Sign up to request clarification or add additional context in comments.

2 Comments

I tried what you said, with ./ but I keep getting the same errors. I think I fully understand what you mean by changing the working directory.
If you change the command to be "pwd" to print the directory it's looking in, copy ffmpeg to that directory and then change the command back to "./ffmpeg" does it still not find it?
0

After listening to everyone telling me it was a problem with the filepath I decided to double check that I had the right directory. I didn't. I saw a "ffmpeg" file in the homebrew folder, but I found out that I could type:

type ffmpeg

into the terminal to find the filepath. It turns out it was actually under usr/local/bin/ffmpeg. All I had to do was add it to my code

ProcessBuilder processBuilder = new ProcessBuilder("usr/local/bin/ffmpeg", "-f", videoName, "-ss", snippets.get(i).getStartTime(), "-to", snippets.get(i).getEndTime(), newFile);

I feel like a complete moron.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.