There are a few notes to mention about this lua, the first one is don't expect the audio to be perfect during the moments the camhack has to work. They end up getting crusty, so it would be best to replace the audio using the audio from the non camhack dump, they would be identical.
The other thing to note is this lua doesn't have the ability to put sprites on top of other graphical layers, so that players can see here sonic is if he's ever behind walls.
However there's an easy way to cheat this by dumping the camhack footage with every layer disabled except for sprites, which the steps look like this (the color code for the pink is $ff00ff):
final output:
here is the avisynth code I used to layer these:
Language: avisynth
main = avisource("blah camhack.avi").ConvertToRGB32
sprites = avisource("blah sprites.avi").ConvertToRGB32
maskclip = ColorKeyMask(sprites, $ff00ff, 1)
final = Overlay(main, sprites, mask=maskclip.ShowAlpha, mode="blend", opacity=1)
In the future this might become obsolete once there's a way to force the sprite layer on top, but in the case where that's not possible, this should work as an alternative.