https://github.com/nopol10/alpha-movie
Android video player with alpha channel (chroma key & alpha-packing) support. Plays alpha-packed webm videos
https://github.com/nopol10/alpha-movie
alpha alpha-packing android native shader transparent video video-player webm
Last synced: 5 months ago
JSON representation
Android video player with alpha channel (chroma key & alpha-packing) support. Plays alpha-packed webm videos
- Host: GitHub
- URL: https://github.com/nopol10/alpha-movie
- Owner: nopol10
- License: apache-2.0
- Fork: true (pavelsiamak/alpha-movie)
- Created: 2021-05-07T12:02:07.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-31T11:01:10.000Z (about 5 years ago)
- Last Synced: 2024-03-09T09:45:47.965Z (about 2 years ago)
- Topics: alpha, alpha-packing, android, native, shader, transparent, video, video-player, webm
- Language: Java
- Homepage: https://github.com/nopol10/alpha-movie
- Size: 7.76 MB
- Stars: 10
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Alpha Movie
Alpha Movie is an Android video player library with alpha channel support.
Video Player uses `OpenGL` to render video and apply a *shader* that makes alpha compositing possible. The player encapsulates `MediaPlayer` and has its base functionality. Video stream is displayed by `TextureView`.
This fork comes with the added feature of being able to display "alpha-packed" videos such as the one shown below.

With this, you can convert webm videos with transparency into "alpha-packed" mp4 videos in order to use them in your Android app. Refer [here](#alpha-packed-videos) for more details.
(Footballer demo video taken from [https://simpl.info/videoalpha/](https://simpl.info/videoalpha/))
---
## Gradle Dependency
[  ](https://bintray.com/pavelsemak/alpha-movie/alpha-movie/_latestVersion)
[](https://www.apache.org/licenses/LICENSE-2.0.html)
The easiest way to start using Alpha Movie is to add it as a *Gradle Dependency*. The Gradle dependency is available via [jCenter](https://bintray.com/pavelsemak/alpha-movie/alpha-movie/view). Please make sure that you have the jcenter repository included in the project's `build.gradle` file (*jCenter* is the default Maven repository used by Android Studio):
```gradle
repositories {
mavenCentral()
}
```
Then add this dependency to your module's `build.gradle` file:
```gradle
dependencies {
// ... other dependencies
implementation 'io.github.nopol10:alpha-movie:1.3.7'
}
```
## Getting Started
Add `AlphaMovieView` into you activity layout:
```xml
```
Next you need to initialize the player. In your `Activity` class add:
```java
public class MainActivity extends AppCompatActivity {
private AlphaMovieView alphaMovieView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alphaMovieView = (AlphaMovieView) findViewById(R.id.video_player);
alphaMovieView.setVideoFromAssets("video.mp4");
}
@Override
public void onResume() {
super.onResume();
alphaMovieView.onResume();
}
@Override
public void onPause() {
super.onPause();
alphaMovieView.onPause();
}
}
```
In this code snippet we load video from *assets* specifying filename `video.mp4`:
```java
alphaMovieView.setVideoFromAssets("video.mp4");
```
Video can also be set by *Url, FileDescriptor, MediaSource* and other sources.
You need to add `alphaMovieView.onPause()` and `alphaMovieView.onResume()` in activity's `onPause()` and `onResume()` callbacks. Calling these methods will pause and resume `OpenGL` rendering thread.
Video playback can be paused and resumed using `alphaMovieView.pause()` and `alphaMovieView.start()` methods.
## Alpha Packed Videos
webm videos containing alpha data can be converted into an "alpha packed" video that contains the color frames on the left, and alpha frames on the right of the video.
This results in a video that can now be processed by the alpha packed video shader bundled in this package.
To generate and play such a video from an existing transparent webm video, do the following:
1. Download [ffmpeg](https://ffmpeg.org/download.html). The most recent version is recommended but anything after 20th July 2016 should work.
1. Run `ffmpeg -vcodec libvpx -i input_video.webm -vf "split [a], pad=iw*2:ih [b], [a] alphaextract, [b] overlay=w" -x264opts keyint=30 -y output_video.mp4`
This will generate the `output_video.mp4` file in the directory containing `input_video.webm`
1. Put `output_video.mp4` in your assets and follow the steps above to load the video into the AlphaMovieView.
1. Change the xml to the following
```xml
```
The result will look like the following. You can view this in the example app in this project.

---
## How it works?
Alpha Movie player uses `OpenGL` to render video with a *shader* attached to gl renderer. This *shader* modifies each pixel of video frame. By default it converts *green* color to transparent.
So default alpha channel color is *green*. This color can be changed to any *rgb* color by adding xml attribute `alphaColor`:
```xml
```
In the code snippet above we set `custom:alphaColor="#ff0000"`. It means that alpha channel color is set to red.
Also we specify *accuracy* attr to be *0.7*. Accuracy is the value between **0** and **1**. It should be lower if you wish more shades of specified color be transparent and vice versa. By default `accuracy="0.95"`.
#### Custom shader
There is a possibility to apply your own *custom shader*. Add `shader` attr:
```xml
```
And define your custom shader in *string* values, for example:
```xml
#extension GL_OES_EGL_image_external : require\n
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
varying mediump float text_alpha_out;
void main() {
vec4 color = texture2D(sTexture, vTextureCoord);
if (color.g - color.r >= 0.1 && color.g - color.b >= 0.1) {
gl_FragColor = vec4(color.r, (color.r + color.b) / 2.0, color.b, 1.0 - color.g);
} else {
gl_FragColor = vec4(color.r, color.g, color.b, color.a);
}
}
```
In this case accuracy and alphaColor attrs are not affecting anything because they are used only when custom shader is not defined.