ffmpeg-cli-wrapper
ffmpeg-cli-wrapper copied to clipboard
Stopping ffmpeg / wrapper remotely
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 ?
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.
I may propose a solution until you find a better one. What I've done is :
- get StdIn of the process as you get StdOut
- make FFmpeg wrapper instance as global variable in my code
- 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();
}
}
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 rtmpdump
command 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
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?
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
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?
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.