Skip to content

Instantly share code, notes, and snippets.

@sport4minus
Last active October 24, 2022 18:58
Show Gist options
  • Save sport4minus/625308abafe617456a504d4160ffd498 to your computer and use it in GitHub Desktop.
Save sport4minus/625308abafe617456a504d4160ffd498 to your computer and use it in GitHub Desktop.
Crossfade two videos on Raspberry Pi /w omxplayer and node.js using omxplayer-controll
/*
crossfade-videos.js by github.com/sport4minus
test based on omxplayer-controll example by github.com/winstonwp
– crossfade two videos on raspberry pi using node.js, omxplayer-controll https://github.com/winstonwp/omxplayer-controll and omxplayer.
– working based on omxplayer's capability to set a video render layer.
– two instances of omxplayer are started on different layers, the upper ones' alpha is changed during runtime.
- this example uses two hypothetical videos named movie.mov and other_movie.mov which are in the same directory as the .js file
- tested on raspberry pi 3
*/
// foreground movie
var omxp = require('omxplayer-controll');
var alpha = 255;
var opts = {
'audioOutput': 'hdmi', // 'hdmi' | 'local' | 'both'
'blackBackground': false, //false | true | default: true
'disableKeys': true, //false | true | default: false
'disableOnScreenDisplay': true, //false | true | default: false
'disableGhostbox': true, //false | true | default: false
'subtitlePath': '', //default: ""
'startAt': 0, //default: 0
'startVolume': 0.8, //0.0 ... 1.0 default: 1.0
'layer': 3 // selects video render layer 3, which is on top of layer 2, naturally
};
omxp.open('movie.mov', opts);
omxp.on('aboutToFinish',function(){
console.log('File about to finish');
});
//background movie
var omxp1 = require('omxplayer-controll');
var opts1 = {
'audioOutput': 'hdmi', // 'hdmi' | 'local' | 'both'
'blackBackground': false, //false | true | default: true
'disableKeys': true, //false | true | default: false
'disableOnScreenDisplay': true, //false | true | default: false
'disableGhostbox': true, //false | true | default: false
'subtitlePath': '', //default: ""
'startAt': 0, //default: 0
'startVolume': 0.8, //0.0 ... 1.0 default: 1.0
'layer': 2 //selects video render layer 2, which is.. beneath layer 3.
};
omxp1.open('other_movie.mov', opts1);
//crossfade back and forth by alpha value of 5 every frame (nextTick)
var fadeSpeed = 5;
function fadeIn(){
if(alpha < 255){
omxp.setAlpha(alpha, function(err){console.log(err)});
alpha += fadeSpeed;
process.nextTick(fadeIn);
} else {
process.nextTick(fadeOut);
}
}
function fadeOut(){
if(alpha > 0){
omxp.setAlpha(alpha, function(err){console.log(err)});
alpha -= fadeSpeed;
process.nextTick(fadeOut);
} else {
process.nextTick(fadeIn);
}
}
//fading begins 5 seconds after initializing the script, as omxplayer instances may not be ready yet.
setTimeout(fadeOut, 5000);
@ArosPrince
Copy link

Hi, does this actually work for you? I have tried it on both RPi 3 and RPi 3 B+ and it flickers like crazy. To me it looks like z-fighting. It also flickers when I only set fixed alpha (say 127) to the foreground movie and do not change it over time... It even flickers when the foreground movie has alpha 255 or 0... What Pi did you try this with and which Raspbian? Thanks...

@sport4minus
Copy link
Author

Hello @ArosPrince,
sorry for seeing this just now.
This actually worked for me, yes. I will look up what system i used, and let you know.
(FYI my video files were of a 1280x720 resolution, at some other occasion i was under the impression that higher-res didn't go as smoothly, but this might be also related to setting this up a year later/new raspian... )

@pixelcrash
Copy link

Hi - could this code fade 2 videos with 1920x1080 30fps in 2021?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment