Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement video with transparent background in react-native

How can i implement a Video Player with transparent background in React-native? The video is saved with the extension .mov (only extension found to support transparent background).

Used react-native-player but it only renders a back screen.

Tried with https://www.w3schools.com/html/mov_bbb.mp4 both loaded locally and via uri and it worked.

Platform target: iOs

like image 833
Gogumania Avatar asked Nov 07 '22 07:11

Gogumania


1 Answers

I needed same thing and tried many different methods and my conclusion is transparent movie file is not an accepted practice/standard. Only apple quicktime supports it.

Here are few workarounds I tried and made it partially work with a lot of performance challenges.

Best working method is to export all mov frames (with whatever frame rate is acceptible for you - for me I was trying to render something that was going to be exported as gif, so even 15 fps worked for me). Export movie frames as transparent png. Ffmpeg can easily do that.

Then either with simple loop, load and update frames from the js code. This is still very challenging because loading each frame in an image view from jsbundle simply does not catch up with your frame rate needs. As far as my experience, I only saw low single digit frame rates with loading resources from jsbundle. On the other hand, there is a solution for this. You can put your png files in xcassets in ios/xcode, and drawables in android. Then use { uri: 'filename' } to load resources from native apps. This resulted very good quality and speed for me on iOS even for 20-30 second video lengths at 1080p quality. Unfortunately this suffered from memory issues and didn't work on Android - Android only handled 100something frame for me. If you have more than 150-200 frames for your movie in total, regardless of fps, it will hit to the memory limits. I spent more than a week to research alternative ways to overcome memory issue, load many large bitmaps in memory for sequence display them in the app. There are theoretical methods to utilize memory on android without heap size limitations but I found it too low level and beyond my android/java knowledge.

Another path I experimented without success was webM format. It's webp format from google that is supported with some push but I couldn't find enough resources online about webm playback. I succeeded to play a webm file but it just works like gif players which almost all of them don't have clear ways to support transparent animated gifs. So I wasn't able to get to a solution with webp/m as well.

Right now I am trying to fake the transparent portions of my video with other RN animated elements over the video.

By the way, I tried most of these solutions natively on iOS and Android separately and both platforms had its own problems with them.

like image 195
Mehmet Fatih Yıldız Avatar answered Nov 15 '22 06:11

Mehmet Fatih Yıldız