5

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?

AllwiN
  • 693
  • 2
  • 12
  • 26
  • Try this command in exec - "sudo kill -9 ". If you just want to restart based on your requirement also try this. Stop : sudo kill -s SIGSTOP , Restart : sudo kill -s SIGCONT . Hopefully it will help you. And you're using windows pc i felt that it's given lot many issues working with FFmpeg based on accessibility, security and etc (-_-). – Dhwanil Patel Dec 14 '19 at 06:53
  • I will try to use your code and I'm using mac – AllwiN Dec 16 '19 at 04:23

3 Answers3

2

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();
}
Virendra Varma
  • 895
  • 1
  • 12
  • 23
  • I tried this already but I cannot import the function, shows like `cannot resolve function killRunningProcess()` – AllwiN Dec 11 '19 at 09:29
  • then you are not using some other library not https://github.com/WritingMinds/ffmpeg-android-java library – Virendra Varma Dec 11 '19 at 09:37
  • I can see the method you specified in my FFmpeg lib. It's complicated than you think, In the beginning, I tried to use it and failed and here again nothing happens. – AllwiN Dec 11 '19 at 09:45
  • 1
    can you post your code how you are tying to access the method so that we all can know how much complicated it is. – Virendra Varma Dec 11 '19 at 10:07
  • It's simply on cancel click of progress dialoge. the complicity is getting the corresponding function fromthe FFmpeg lib. – AllwiN Dec 11 '19 at 10:44
2

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

pavle
  • 909
  • 14
  • 38
0

Based on googling I found the following solution, which worked for me. I defined the class (FFmpeg_Handling), and the method cancel simply.

class FFmpeg_Handling {
    private FFmpeg ffmpeg;
    void loadFFMpegBinary(Context context) {
        try {
            if (ffmpeg == null) {
                ffmpeg = FFmpeg.getInstance(context);
            }
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onFailure() {}

                @Override
                public void onSuccess() {}

            });
        } catch (Exception ignored) {}
    }


    void execFFmpegBinary(final String[] command) throws FFmpegCommandAlreadyRunningException {

        ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {}

            @Override
            public void onSuccess(String s) {}

            @Override
            public void onProgress(String s) {}

            @Override
            public void onStart() {}

            @Override
            public void onFinish() {}


        });

    }

    void cancel(){
        ffmpeg.killRunningProcesses();
    }

}

Then whenever needed, I call the cancel method through the instacne.

Instantiating:

FFmpeg_Handling fh=new FFmpeg_Handling();

Calling cancel method:

fh.cancel();
Milad
  • 1