Skip to content

Pipe Support

Taner Şener edited this page Oct 5, 2021 · 4 revisions

FFmpegKit supports executing ffmpeg and ffprobe commands only, starting another process and redirecting its input/output to use inside ffmpeg or ffprobe is not possible.

If you try to run the following command in one of the execute() methods,

cat <image path> | -i pipe: video.mp4

it will fail with the following error.

Unable to find a suitable output format for 'cat'

However pipe: protocol, or reading input from another pipe is still supported. You can still use the output of another process inside your ffmpeg and ffprobe commands. What you need to do is create a named pipe and feed it with the command output.

FFmpegKit has a registerNewFFmpegPipe method on FFmpegKitConfig class to help you create new pipes. You can use it to create a new pipe use it as input or output in your commands.

Android

In order to do that on Android:

1. Create a new pipe

String pipe1 = FFmpegKitConfig.registerNewFFmpegPipe(mainActivity);

2. Build your command using the pipe

String ffmpegCommand = "-i " + pipe1 + " -r 25 <output video path>";

3. Execute your command

FFmpegKit.execute(ffmpegCommand);

4. Push data to your pipe by starting another process

Runtime.getRuntime().exec(new String[]{"sh", "-c", "cat <image path> > " + pipe1});

5. Close the pipe

FFmpegKitConfig.closeFFmpegPipe(pipe1);

Please note that ffmpeg will block the execution of your command in step #3 until some data is available in your pipe. So, if you don't have a step #4, the thread that executes step #3 will wait indefinitely.

iOS / macOS / tvOS

It is possible to implement the same example on iOS, macOS and tvOS. Objective C API also has a registerNewFFmpegPipe method on the FFmpegKitConfig class. Steps #1, #2 and #3 can be repeated easily. However pushing data to your pipe is more difficult on Apple platforms. Initiating another process is not allowed on them. Therefore, you have to push the data manually by opening the pipe using the NSFileHandle class and calling its writeData method.

Flutter / React Native

In addition to standard pipe methods (registerNewFFmpegPipe and closeFFmpegPipe) available in all APIs, FFmpegKit plugins on Flutter and React Native have another pipe related method named writeToPipe to easily send data to pipes. It is implemented on FFmpegKitConfig class. You can use it instead of step #4 in the example.

PS: All test applications in the FFmpegKit Test repository include a PIPE tab, which demonstrates an example scenario similar to the one used in this page.

Clone this wiki locally