Kill the process launched by exec () after some time, and save the frames in an array

Let me start by saying that I am completely new to Java. I’m from a PHP background, but it happens that one of my PHP tasks needs to be converted to Java.

The task is to split the video into frames using ffmpeg, and then work with these frames. I completed this process in PHP. And now I can convert it to Java.

I looked through some lessons and got the databases (using the IDE, running a java program, etc.). I use Eclipse for this purpose.

I managed to run ffmpeg using a java program using

public static void main(String[] args) throws IOException { String livestream = "D:/video.mpg"; String folderpth = "D:/frames"; String cmd="D:/ffmpeg/bin/ffmpeg.exe -i "+ livestream +" -r 10 "+folderpth+"/image%d.jpg"; //System.out.println(cmd); Runtime r = Runtime.getRuntime(); Process p = r.exec(cmd); // this starts creating frames // and stores them on local disk // any way to store that frame name in array? // something like String[] frames = {"image%d.jpg"} // so that frames array contains image1.jpg, image2.jpg..and so on? } 

This works fine, I get frames in the folder. Now what I want to do is kill the process after some, say, 5 minutes, because the video lasts more than 2 hours, and I don’t want you to come to the taskbar and kill the process manually. I would also like to know if there is a way to save the frame names created by ffmpeg into an array for future use.

I tried using p.destroy() , but this did not stop the process at all. How can I use something similar like setTimeout() which is used in jQuery?

Some metadata

OS: Windows 7

IDE: Eclipse

+4
source share
3 answers

p.destroy() sends kill PID to linux. This means that the process receives a signal, but does not necessarily end. You must execute kill -9 PID to ensure that the process is indeed complete. Unfortunately, the standard Java API does not provide this functionality, so you need to do it yourself.

But it is not so difficult. There are only 2 commands: kill for Unix and killtask for windows. Both accept a process id. You can detect this by reflection: private int filed pid represents in the platform subclass a specific Process which instance you get from runtime.exec()

EDIT

In linux runtime.exec() , an instance of UnixProcess returned that extends Process . I don’t have any windows available right now and I can’t check it, but as far as I remember, in windows it returns an instance of WindowsProcess or something like that. Both have a private int field pid , which can be extracted using reflection:

 Process proc = Rutime.getRuntime().exec("my command"); Field f = proc.getClass().getDeclaredField("pid"); f.setAccessible(true); int pid = (Integer)f.get(proc); 
+3
source

A simple way is to use java.util.Timer and java.util.TimerTask to set the timer for 5 minutes (or whatever interest you’re interested in), and the timer task will not force the process handle to kill it when the timer goes out and starts the timer task.

0
source

I think the Apache watchdog will help you. It waits for execution within a given time. If a time exceeding the set time ends the process. It is available in commons exec

Code example:

 ExecuteWatchdog watchdog = new ExecuteWatchdog(1000*60*5); Process process= Runtime.getRuntime().exec("cmd"); watchdog.start(process); 
0
source

All Articles