Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill FFmpeg currently running process?

I'm working on an application that trims a video using FFmpeg. While the trimming process has started and I cancel it, the FFmpeg is terminated fully. How can I achieve by killing the current working process only?

   /* define PID */
  try {
     String pid = ffmpeg.toString();
     pid = pid.substring(pid.indexOf("[") + 1, pid.indexOf("]"));
     Process process = Runtime.getRuntime().exec("kill -2 " + pid);

     process.waitFor();
     ffmpeg.wait();
  } catch (IOException | InterruptedException e) {
     Log.e(TAG, "kill_ffmpeg: exception failed ",e );
     e.printStackTrace();
  }

This is the code I've been using now. when this code awake the activity restarts with an exception of the index out of bound.

This is the library I've been using for achieving a video editing feature using FFmpeg Here

There is a method in this FFmpeg library called killRunningProcess(). When I use this method, shows an error called cannot resolve method killRunningProcess()

How can I achieve Killing a process without memory leaks and without crashing the app when the particular code has been invoked?

like image 250
AllwiN Avatar asked Nov 25 '19 11:11

AllwiN


2 Answers

You don't have to add or import any thing, what you have to do just call the method already declare in the file

private void killAllreadyRunningProcess(FFmpeg ffmpeg){
    if(ffmpeg.isFFmpegCommandRunning())
        ffmpeg.killRunningProcess();
}
like image 56
Virendra Varma Avatar answered Oct 22 '22 22:10

Virendra Varma


Get the Process ID from the process object using the following:

public static int getPid(java.lang.Process p) {
    int pid = -1;

    try {
        Field f = p.getClass().getDeclaredField("pid");
        f.setAccessible(true);
        pid = f.getInt(p);
        f.setAccessible(false);
    } catch (Throwable e) {
        pid = -1;
    }
    return pid;
}

Then send a signal to the process to terminate by calling the following: android.os.Process.sendSignal(pid, 15); // 15 is the value for SIG_TERM ref: https://github.com/WritingMinds/ffmpeg-android-java/issues/33#issuecomment-218816755

like image 2
pavle Avatar answered Oct 22 '22 22:10

pavle