Transparent video?

For the past few days, I have been wondering how to show a transparent video on an OpenGL scene. I can already extract video frames from a movie file using ffmpeg and display them as a video. But how about a transparent video?

Is there a compression format that supports alpha channel? How is the compression ratio, is it still good? If I simply use chroma key, how can I candle a fade-in/out images (I don’t want to deal with a complicated math formula here)?

All of my questions here were answered after reading this StackOverflow question. Well not answered per se :P, but I immediately what I need to do to show a transparent video. Basically I need to store the alpha mask. But instead of using the alpha channel in a RGBA video stream (I still need to make experiments with this though), I can use an additional area of the video to store the mask. With this, I can use the regular RGB-channel video with those already-known compression format, ratio, and what-not. But in exchange, I need to enlarge the video dimension to store the alpha mask. Hopefully the compression algorithm is clever enough to compress more this part of the video 😛

Having that, all I need to do next is to modify the already-simple fragment shader that I use. So now I need to take the first half of video and use the RGB values of it. Then take the second half and get the Red value (or Blue or Green) and use it as the alpha channel of the RGB.

varying vec2 texcoord;
uniform sampler2D textureId;

void main() {
    vec2 tc_rgb = texcoord * vec2(1.0, 0.5);
    vec2 tc_alpha = tc_rgb + vec2(0.0, 0.5);
    vec4 frame = texture2D(textureId, tc_rgb)
    vec4 alpha = texture2D(textureId, tc_alpha);
    gl_FragColor = vec4(frame.rgb, alpha.r);
}

Yippy, that’s all! Ah yes, don’t forget to enable GL_BLEND and set the blend function otherwise you will have a bad time 😛

3 thoughts on “Transparent video?”

  1. Hello Fajran Iman Rusadi,

    How can I say my great respect unto you? I am apt-web user for long time and I admit completely it is so useful for Indonesian user. I said, Indonesian user. People who has only limitation in internet bandwidth.

    May I send my small idea? How if apt-web has a more single link to compress all links generated by search into one single ZIP? Or a simple PHP hack (I think you are capable to do) to download all links by just clicking 1 special link? I have tried kde-standard and it is horror seeing how much links are. I know, this is just small idea. All prerogative rights is in your cold-hands. Thank you.

    Thank you so much. Syukran wa jazakumullahu khairan.

  2. Hello Fajran Iman Rusadi,

    kenapa tulisannya harus pake bhs.inggris sedangkan sya ga bisa bhs.inggris.
    tapi saya pengen tau apa yang ada dalam tulisan tersebt.

    terimakasih..

Leave a Reply