0

Help me please, i have to use ffmpeg cmdline in my application. I built the ffmpeg binary and now i'm able to start using ffmpeg as follows:

public class FfmpegCmd {
...
   public ProcessRunnable create(){
     final List<String> cmd = new LinkedList<String>();

     ... // filling ffmpeg parameters

     final ProcessBuilder pb = new ProcessBuilder(cmd);
     return new ProcessRunnable(pb);
}
...
}

And i start ffmpeg conversion as follows:

final FfmpegCmd task = new FfmpegCmd();

new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... arg0) {
                task.create().run();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {                 
                Toast.makeText(MainActivity.this, "Ffmpeg cmd complete.", Toast.LENGTH_SHORT).show();
            }

        }.execute();

It works fine. But I wanna to add ability to terminate ffmpeg command execution. How can i do that? How ProcessRunnable can be terminated?

I need something like "send CTRL+C to ProcessRunnable"

borune
  • 548
  • 5
  • 21

1 Answers1

1

solved it as follows:

Process ffmpeg = Runtime.getRuntime().exec(mFfmpegInstallPath + " -y -i
rtsp://192.168.1.254/xxx.mov -b 2M -vcodec mpeg4 /sdcard/video.avi\n");

...

private void kill_ffmpeg(){
    Log.d("output: ",ffmpeg.toString());

    /* 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.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
borune
  • 548
  • 5
  • 21