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);
}
sudo