ffmpeg-cli-wrapper icon indicating copy to clipboard operation
ffmpeg-cli-wrapper copied to clipboard

Stopping ffmpeg / wrapper remotely

Open turmalin opened this issue 8 years ago • 7 comments

Hi,

I use this library as an RTSP client to record IP camera streams. I can start the streams but I could not figure out how to stop recoding remotely, since ffmpeg waits for q button to stop and terminate.

What can be the proper way of stopping ffmpeg / wrapper, without killing the sub-process ?

turmalin avatar Mar 30 '16 13:03 turmalin

That is not a use case I designed for. Other that killing the sub process I don't know of a clean way. I'll leave this bug open as a reminder to add better support for sending input to ffmpeg while it is running.

bramp avatar Mar 30 '16 14:03 bramp

I may propose a solution until you find a better one. What I've done is :

  1. get StdIn of the process as you get StdOut
  2. make FFmpeg wrapper instance as global variable in my code
  3. Generate two threads, one thread starts FFmpeg wrapper instance, other thread gets StdIn using getProcessIn() and stops it, both methods using same global wrapper instance

we should add following code fragment to FFmpeg.java

to imports: ...

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

....

to members: .... private BufferedWriter processIn; ....

to methods: ....

  private BufferedWriter wrapOutWriter(Process p) {
    return new BufferedWriter (new OutputStreamWriter(p.getOutputStream()));
  }

  public BufferedWriter getProcessIn() {
      return processIn;
  }

....

and finally altered run method:

  public void run(List<String> args) throws IOException {
    List<String> newArgs = ImmutableList.<String>builder().add(path).addAll(args).build();

    Process p = runFunc.run(newArgs);
    try {

      processIn = wrapOutWriter(p);

      // Now block reading ffmpeg's stdout. We are effectively throwing away the output.
      IOUtils.copy(wrapInReader(p), System.out); // TODO Should I be outputting to stdout?

      FFmpegUtils.throwOnError("ffmpeg", p);

    } finally {
      p.destroy();
    }
  }

turmalin avatar Apr 04 '16 09:04 turmalin

I will have the same need for my application Podcast-Server, which is a downloader with GUI for different source. In the GUI, I should be able to start / stop / pause a download.

Right now, the download is made in plain Java with InputStream, but this lead to corrupted files... so I would like to switch to ffmpeg which is simpler, has a good wrapper and is simpler to use. But, for some case (like rtmp), I had to play with rtmpdumpcommand and process...

It will be the same here for ffmpeg, so, in order to solve the of @turmalin and let user play with the process, we should be able to access the process.

I'll try to do some proposition about this 😄

First : We can use the constructor of FFmpeg to define our custom runFunc, and in this case, we can get the reference to the process ?

public FFmpeg(@Nonnull String path, @Nonnull ProcessFunction runFunction) throws IOException {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
    this.runFunc = checkNotNull(runFunction);
    this.path = path;
    version();
  }

Thanks

davinkevin avatar Jul 24 '16 04:07 davinkevin

Hello,

any news on this issue? I am also running into the problem that I have no means to stop a recording job that is running. Is there no way to stop recording an infinite stream to file?

ghost avatar Jun 26 '17 14:06 ghost

I've handle it in my application with a ProcessListener. You can see the code here 👍 https://github.com/davinkevin/Podcast-Server/tree/master/Backend/src/main/java/lan/dk/podcastserver/utils/custom/ffmpeg https://github.com/davinkevin/Podcast-Server/blob/master/Backend/src/main/java/lan/dk/podcastserver/service/FfmpegService.java

davinkevin avatar Jun 26 '17 15:06 davinkevin

I've handle it in my application with a ProcessListener. You can see the code here 👍 https://github.com/davinkevin/Podcast-Server/tree/master/Backend/src/main/java/lan/dk/podcastserver/utils/custom/ffmpeg https://github.com/davinkevin/Podcast-Server/blob/master/Backend/src/main/java/lan/dk/podcastserver/service/FfmpegService.java

Hi @davinkevin The above link does not exist anymore. Do you mind giving the new link?

pgupt avatar Aug 12 '19 12:08 pgupt

https://gitlab.com/davinkevin/Podcast-Server/tree/aeb34992ebf0262eb063589b67597811e0b09a30/backend/src/main/kotlin/com/github/davinkevin/podcastserver/utils/custom/ffmpeg https://gitlab.com/davinkevin/Podcast-Server/blob/aeb34992ebf0262eb063589b67597811e0b09a30/backend/src/main/kotlin/com/github/davinkevin/podcastserver/service/FfmpegService.kt

Sorry for that 😅.

I move my project to youtube-dl instead of doing the download by myself then encoding it with ffmpeg, so I will remove those files in few iterations I think.

davinkevin avatar Aug 12 '19 14:08 davinkevin