I'm trying to run a command in my java program that allows me to use the external software ffmpeg to extract subtitles from a video file.
On my cmd the command works fine, but when I run it from my IDE from a process builder, it executes but never finishes.
It's only when I stop my application that the subtitle file appears in the specified folder, which means that the command executes correctly but never stops.
can anyone help?
here's some code to illustrate what I mean
List<String> command = new ArrayList<>();
command.add("ffmpeg");
command.add("-i");
command.add(fileName);
command.add("-c:s");
command.add("srt");
command.add(subfile);
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
processBuilder.directory(new File(UPLOAD_MOVIE_DIR_disc + movie.getTitle() ));
processConvert.waitFor(); //never finish
by the time I got an answer, I had to use a new thread next to it, which would stop the process execution after a while, but this is not suitable for an application.
The issue with your code is related to how you're handling the process output streams. By default, ProcessBuilder
uses pipes for communication, which can cause the waitFor()
method to hang indefinitely if the streams aren't drained.
you can fix your code by redirect standard error or using process streams