0

Is there any way to know the execution time of the command? Making exec synchronous

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(ffmpeg +" -i "+source+" -vcodec h264 -acodec aac  "+destination);

the ffmpeg command is taking time so I want to log something after the execution. Is there any way to log the things after the command has executed?

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • So, your basic question comes down to - how to calculate the duration between two points in time. Rather a common problem, but since the date/time APIs have changed since Java 8, it can be a little confusing to find the right answer. However, if the answer doesn't indulging the `java.time.*` API, then it's not the answer you're looking for. [For example](https://stackoverflow.com/questions/25776787/java-simpledateformat-format-issue-with-yyyy/25777559#25777559) – MadProgrammer Nov 18 '20 at 20:28
  • nope @MadProgrammer I am aware of execution. I want to make this synchronous.. so that execution takes places after the conversion (ffmeg process) has finished. – Danyal Sandeelo Nov 18 '20 at 20:30
  • So, what's time go to do with it? Just `waitFor` the process to complete - might want to check the `exitCode` and print a statement – MadProgrammer Nov 18 '20 at 20:30
  • @DanyalSandeelo Possible duplicate [Wait for process to finish before proceeding in Java](https://stackoverflow.com/questions/17972380/wait-for-process-to-finish-before-proceeding-in-java) - again, not an uncommon question, although I'd recommend using `ProcessBuilder` – MadProgrammer Nov 18 '20 at 20:33
  • " I want to log something after the execution." @MadProgrammer and the logs do their job right after the execution which I want to do be done after theprocess has completed the conversion. – Danyal Sandeelo Nov 18 '20 at 20:34
  • @MadProgrammer I tried it but apparently I have missed something. I will give it another try using that. – Danyal Sandeelo Nov 18 '20 at 20:34

1 Answers1

1

You could record the time before the process starts, waitFor it to finish, and then record the time again, and subtract the two:

long before = System.currentTimeMillis();
Runtime rt = Runtime.getRuntime();
Process process =
    rt.exec(ffmpeg + " -i " + source + " -vcodec h264 -acodec aac  " + destination);
process.waitFor();
long after = System.currentTimeMillis();
long execTime = after - before;
System.out.println("Processing took " + execTime + " milliseconds");
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I tested `waitFor()` seems like it halts everything, or maybe I didn't wait much.. Let me give it another try. – Danyal Sandeelo Nov 18 '20 at 20:32
  • @DanyalSandeelo: if the process produces any output, then you must consume that output or the process might hang. – Joachim Sauer Nov 18 '20 at 20:42
  • how do we consume it? it generates output but I don't need that output. I just need execution as the output is another file. – Danyal Sandeelo Nov 18 '20 at 20:43
  • @DanyalSandeelo Use a `ProcessBuilder` - it will give a lot more capability and configuration options as well as over come some of the issues related to `Process` - as a [crud example](https://stackoverflow.com/questions/11893469/java-execute-a-program-with-options-like-ls-l/11893495#11893495) – MadProgrammer Nov 18 '20 at 20:52
  • @MadProgrammer you are right.. `process.waitFor();` is not working on my end. Process builder makes sense – Danyal Sandeelo Nov 18 '20 at 20:56