How do I mix the alpha channel of one video with another?

I have two videos one is alpha channel only and one is RGB+Alpha, I want to take the pixels of the RGB+Alpha video and mix them with the Alpha only video. In other words mix the Video with the Alpha only channel … I’m using xofQTKitPlayer

Hi there!

I suggest using the ofxHapPlayer and a shader. Like so, you can have total control.

is there any example for this, cant find anything ))

Hi @Freeka_Tet,
Take a look at examples/gl/alphaMaskingShaderExample. Instead of using an image use a video file that you can pass to the texture.
Also, you can use the textures’ setAlphaMask function as it follows.

ofApp.h file

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

	ofVideoPlayer video, maskVideo;
	bool bVideosLoaded = false;
};

ofApp.cpp file:

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){

	auto r = ofSystemLoadDialog("Select video file"); // use the load dialog to find the video on your 
//computer. if you already know the path and/or dont want to have the load dialog popping up just 
//manually specify the file path to the video file. 
	if(r.bSuccess){
		bVideosLoaded = video.load(r.getPath());
		cout << r.getPath() << endl; // just print the video path.
	}
	if(bVideosLoaded){
		r = ofSystemLoadDialog("Select masking video file");
		if(r.bSuccess){
			bVideosLoaded = maskVideo.load(r.getPath());
			cout << r.getPath() << endl;
		}
	}	
	if(bVideosLoaded){ //Checking that both videos loaded properly		
		video.play();
		maskVideo.play();
	}
}

//--------------------------------------------------------------
void ofApp::update(){
	if(bVideosLoaded){
		video.update();
		maskVideo.update();
		if(video.isFrameNew() || maskVideo.isFrameNew()){
			video.getTexture().setAlphaMask(maskVideo.getTexture());	// THIS IS THE MAGIC LINE!
		}
	}
}

//--------------------------------------------------------------
void ofApp::draw(){
	if(bVideosLoaded){
		video.draw(0,0);
		maskVideo.draw(video.getWidth(), 0);
	}
}

Just tested it and it works. MAybe it might be wise to check if both files have the same size. I am unsure of how having diferent sizes might affect

hope this helps. Cheers

@arturo @zach @theo What do you think about adding the code I just posted as one of OF’s examples? I think it can be useful to expose ofTexture::setAlphaMask(...) function as well as dealing with videos with alpha in an easy way. Let me know and I’ll make a PR. Cheers

Thanks @roymacdonald!
my issue was with ofxHapPlayer.
you need to use *texture:

alpha[i].update();
ofTexture *mask = alpha[i].getTexture();
texture[i].getTexture()->setAlphaMask(*mask);
texture[i].update(); 

took me a second to realize!!

1 Like

That is be because ofxHapPlayer returns a pointer when calling getTexture() while ofVideoPlayer returns a reference. good that you figured out.
cheers

1 Like

yeah sure, i thought there was some example for setAlphaMask but if there’s none examples are always welcome of course

should I put it in the video , gl or graphics section?

i’d say gl