Java Process Builder runs ffmpeg commands very slowly - java

I'm trying to run ffmpeg in Java using ProcessBuilder. I'm on Windows. It works fine. But not sure why it's much slower than when I just run the same command in command prompt or PowerShell.
Why is it? Is there any ways to increase the speed?
processBuilder.command("C:\\Windows\\System32\\cmd.exe", "/c","ffmpeg.exe", "-y", "-i", video,"-vf","scale=720:-1","out.mp4");
processBuilder.redirectErrorStream(true);
try {
process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line="";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.err.println("Error in processBuilder. ");
}

You have two starts, delete the first or the redirect will not work:
process = processBuilder.start();
If your sub-process is quite verbose the problem may simply be System.out.println() as multiple line output to some Windows cmd / terminals can be exceptionally slow. You can verify if this is the case by commenting out the print, or capture to File based output before start:
processBuilder.redirectOutput(new File("stdout.log"));
Don't forget to add status check at the end and cross check rc with ffmpeg documentation:
int rc = process.waitFor();

Related

Grab CloudCompare Command Line Output with Java ProcessBuilder

I'm playing a bit around with a small JAVA gui for the command line mode of cloudcompare.
Therefore I'm using a short snippet like these:
var processBuilder = new ProcessBuilder();
try {
var process = processBuilder
.command("open", "-a", "CloudCompare.app", "-n",
"--args", "-NO_TIMESTAMP", "-C_EXPORT_FMT", "LAS",
"-O", "/Users/se/pcl_1.las",
"-O", "/Users/se/pcl_2.las",
"-MERGE_CLOUDS")
.start();
String error, line;
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = inputStream.readLine()) != null) {
System.out.println("line = " + line);
}
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((error = errorStream.readLine()) != null) {
System.out.println("error = " + error);
}
var ret = process.waitFor();
System.out.printf("Program exited with code: %d", ret);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
But if I run it on macOS the command line window opens, the process runs normal, but I can't grab any of the informations in it. There is an option to write log files from cloudcompare. That works - the log file shows that all cloud processing steps are done.
Does anybody knows, how to grab the command line output?
As mentioned here, the /usr/bin/open command is not an option to grab the stdinput stream.
I change the command to /Applications/CloudCompare.app/Contents/MacOS/CloudCompare and it works.
The next question is, how to grab the InputStream with a thread. I tried some stack overflow topics, but it doesn't work at the moment, to get the output stream in realtime. It is flushed at the end of the CloudCompare process.

How do I run GDB and accept GDB commands through Java output console?

I am trying to build a GUI for GCC which has some basic functionalities like compile, link, execute, debug, etc for C++ programs using Java. I am creating strings of command which I pass to the ProcessBuilder and run it via command prompt and GCC.
command = "cd src & cd Resources & g++ " + compileFile.cpp +" -Wall "+ " -o "+ "tempOut";
This is a sample code for compiling the file.
Part of this is the debug functionality for which I am using GDB. Now the problem is GDB needs additional input to add breakpoints, remove breakpoints and so on. I am having trouble on how to pass these necessary inputs to GDB via Java terminal. If I pass the commands in the command prompt, it is working fine and I get the desired output.
enter image description here
But whenever I fire the GDB command from the Java program, I cannot pass any inputs from the IDE's terminal. I am aware that each GDB command uses a different process and I tried attaching Java's process ID to GDB but I just get a blank output console. It seems that the GDB session has started but there is no way to interact with that process through the IDE's output console.
int pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("#")[0]);
command = "gdb attach "+ pid;
fireCommand();
EDIT
This is the method that interacts with command prompt to take input and display output in the IDE's output console:
public void fireCommand() {
String line;
String os = System.getProperty("os.name");
ProcessBuilder builder;
if(os.startsWith("Win")) {
builder = new ProcessBuilder("cmd.exe", "/c", command);
}
else{
builder = new ProcessBuilder("bash", "-c", command);
}
try {
process = builder.start();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
if(line.contains("Input the value")) {
//any other user input in non debug execution
String value = JOptionPane.showInputDialog(line);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(process.getOutputStream()));
writer.write(value, 0, value.length());
writer.newLine();
writer.close();
}
else {
output.append(line).append("\n");
}
}
int exitVal = process.waitFor();
if (exitVal == 0) {
//display("Success!");
display(output.toString());
} else {
String lineErr;
BufferedReader readerErr = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
while ((lineErr = readerErr.readLine()) != null) {
outputErr.append(lineErr).append("\n");
}
//display(exitVal);
display(outputErr.toString()); //Display the uncatched errors
}
} catch (IOException e) {
display("There was a problem with the I/O");
e.printStackTrace();
} catch (InterruptedException e) {
display("There was a interruption with the execution");
e.printStackTrace();
}
if(!outputErr.toString().isEmpty())
errorFormatDisplay(); //display Error output function
}
Any leads on this would be very helpful. Thank you.
I am aware that each GDB command uses a different process
No, gdb runs as one process. Did you mean to say that you are creating a new gdb process every time you try to pass it a gdb command ?
It seems that the GDB session has started but there is no way to interact with that process through the IDE's output console.
Maybe you should use The GDB/MI Interface, which e.g. the Emacs debugger mode gud uses.

BufferedReader pauses on .readLine() when using ProcessBuilder to execute simple Python script

I'm trying to use ProcessBuilder to execute a simple python script from CMD, which prints to the command line, then have that text read into Java and outputted through System.out.println() in netbeans. My issue is the BufferedReader seems to pause at .readLine() then output the text in bulk once the py script has stopped running, as opposed to outputting live.
My process is as follows:
[execute python script]
Executors.newSingleThreadExecutor().execute(this::TEST);
[run execution]
public void TEST(){
try {
ProcessBuilder py = new ProcessBuilder("cmd", "/C", "C:\\Users\\Documents\\TEST.py");
String readLine;
Process launch = py.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(launch.getInputStream()));
while((readLine = reader.readLine()) != null){
System.out.println(readLine);
}
} catch (IOException ex) {Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex);}
}
[here is the python script i wish to run and read live]
import Tkinter, tkFileDialog, tkMessageBox
root = Tkinter.Tk()
root.withdraw()
root.resizable(width=Tkinter.TRUE, height=Tkinter.TRUE)
root.geometry('{}x{}'.format(400,600))
print("hello1\n")
print("hello2\n")
print("hello3\n")
print("hello4\n")
print("hello5\n")
tkMessageBox.showinfo("Complete!", "testing")
print("hello6\n")
print("hello7\n")
print("hello8\n")
print("hello9\n")
print("hello10\n")
tkMessageBox.showinfo("Complete!", "testing")
Thanks!!!
First: Your command does not do anything. It is cmd /C C:\Users\Documents\TEST.py which does nothing but tell you that C:\Users\Documents\TEST.py is not a command. You would need to call cmd /C start C:\Users\Documents\TEST.py for it to do something.
Still this won't make your code work. The problem here is that you are invoking a cmd in there you start a python process. When you now grab the input stream you are getting the cmd input stream which is not the one you are looking for.
In order to get it to work invoke python directly by calling python C:\Users\Documents\TEST.py. Make sure python is in your PATH for this to work.
Your code should then look something like this:
try
{
ProcessBuilder py = new ProcessBuilder("python", "C:\\Users\\Documents\\TEST.py");
String readLine;
Process launch = py.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(launch.getInputStream()));
while ((readLine = reader.readLine()) != null)
{
System.out.println(readLine);
}
}
catch (IOException ex)
{
Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex);
}
This gives you exactly the result you were looking for.
A common problem can be that you did not flush your python std out. I recommend adding
import sys
# your code here...
sys.stdout.flush()
to your code to flush your python output.

Slow System Commands From Java

I am calling a bash scrip script from Java.
The script does the following:
cat /home/user/Downloads/bigtextfile.txt | grep 'hello'
This particular command when run command line takes about 1 second to complete on the text file which is 150MB.
When calling the bash script via Java using the following call:
command = "sh /home/user/bashfiletocall"
p = Runtime.getRuntime().exec(command);
The time to complete takes so long I don't wait.
Am I doing something very wrong and if not can you explain the reason for the huge lack in performance?
NOTE: I was running it in Netbeans and this seems to be the problem .. when I ran the file command line it was quick. The performance between execution in netbeans and command line is huge.
Many thanks.
private String executeCommand(String command) {
StringBuilder output = new StringBuilder();
BufferedReader reader = null;
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
After starting your process you need start reading from the input stream. Otherwise the buffers are running full and p.waitFor() waits forever.
Javadoc of the Process class:
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

open a command prompt from java code and run some commands and read the cmd prompt display?

I need to open a command prompt from java code and run some commands on the same and after that I need to read that command prompt output in java code in real time.
I have tried with below code but I was not able to read the cmd prompt display/output in java.
File file = new File("D://Projects/quantum");
Process proc = rt.exec("cmd.exe /c start cmd.exe /k \"ping localhost\"", null, file);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
Check this out
Process p=Runtime.getRuntime().exec("cmd /c dir");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
This is a very fragile implementation of running a Process.
General tips.
Read and implement all the recommendations of When Runtime.exec() won't.
Once that is done, ignore the fact the article explicitly refers to the Runtime.exec() method and establish the Process using a ProcessBuilder, which makes it easier to implement some of the recommendations of the first linked article.
But even then, break the String command into a String[] arguments of command.

Categories