arthenica / ffmpeg-kit

FFmpeg Kit for applications. Supports Android, Flutter, iOS, Linux, macOS, React Native and tvOS. Supersedes MobileFFmpeg, flutter_ffmpeg and react-native-ffmpeg.
https://arthenica.github.io/ffmpeg-kit
GNU Lesser General Public License v3.0
4.55k stars 605 forks source link

Issue with Raw Image Streaming to FFmpeg Process via Pipe on React Native #1011

Open uripont opened 4 months ago

uripont commented 4 months ago

Title: Challenges with Raw Image Streaming to FFmpeg in React Native Application

Description:

Goal

We aim to implement a solution for capturing and encoding video directly within a React Native application. The objective is to stream raw image data directly into an FFmpeg process for continuous video encoding, ensuring minimal storage overhead and efficient processing.

Problem

  1. Pipe Auto Closes with PNGs: When attempting to stream PNG images to the FFmpeg process, the pipe auto-closes after each image is passed. This behavior disrupts the continuous encoding process, resulting in incomplete video streams.

  2. Inability to Pass Base64 Raw Images: In our current implementation we attempted to pass as base64 encoded raw images. However, the FFmpeg process expects a file path rather than raw data when passing it with FFmpegKitConfig.writeToPipe(), making it impossible to feed the images directly into the encoder. This limits our ability to keep the encoding process active until explicitly closed.

The ideal solution involves continuously streaming raw image data directly to FFmpeg without the need for intermediate storage. This would involve:

Challenges

We seek guidance on:

Environment:

Expected Behavior: Continuous streaming of raw image data to FFmpeg, allowing seamless video encoding until the process is manually terminated.

Actual Behavior: Pipes auto-close after each PNG frame, and raw image data cannot be directly passed due to file path requirements.

Any advice or solutions to address these issues would be greatly appreciated. Thank you!

Indra-poudel commented 3 months ago

@uripont did you able to come with solution. I have similar usecase :D

uripont commented 3 months ago

Unfortunately no, we haven't found any solution to this issue.

But we have found an alternative way to fulfill our needs: instead of encoding a video ourselves frame by frame, we end up re-encoding an already recorded video to achieve the same result.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

tanersener commented 2 weeks ago

The current FFmpegKitConfig.writeToPipe() method is designed to work with only one input file. It closes the pipe as soon as that input is fully read, so it doesn’t support multiple files as you mentioned. While it’s technically possible to extend this functionality, it's outside the project’s focus. Additionally, there are already other ways to write data into pipes, so adding limited pipe features in FFmpegKit isn’t very beneficial.

If you need to use pipes with multiple files, we suggest extending the existing iOS and Android implementations. You can find the source code here for iOS and here for Android. Although these aren’t written for React Native, the approach is similar, you can replace Android/iOS specific methods with React Native equivalents.

As for passing base64 data to FFmpeg, we don’t have a solution yet. I’m experimenting with some options when I can, but nothing is ready at the moment.