Camera tracking : solving floor plane when the floor is not flat
If modelling a simple approximation isn't good enough and the scene and ground remains relatively static in the footage in terms of moving objects and lighting changes, I'd try photogrammetry. This allows you to create a 3D model of the scene which can edit however you like. For instance you can extract the ground and use it as shadow catcher.

For open source photogrammetry software I'd suggest MeshRoom. Another good software is COLMAP, it misses some features that MeshRoom has, e.g. texture extraction for the mesh.