These are the steps I made:
- first I corrected the scale on the armatures, the source scale was 0.094, and the target armature scale was 1.000. They need to be the same. Because the animation fits the source armature, I need to scale the target armature also to 0.094 to be able to use the same animation.
I deleted the armature modifier on the mesh and I unparented the mesh to be able to scale the target armature without influencing the mesh. I set the origin to 3D point and scaled the armature to 0.094 value. This made the armature really tiny, so I went into edit mode and scaled the bones back into it's original size to fit the mesh using the same 3D point origin.
- now that the armatures have correct scales, I had to fix the "Hips" bone in the target armature. In source armature this bone points straight up, but in target armature this bone is tilted.
Because the whole character moves from this one bone, they need to point in the same direction, so the animation moves both characters in the same direction (the translation on bones is in local space). So in edit mode I adjusted the "Hips" bone of the target armature to point up, same as in source armature.
- the hips bone fix solves the translation. To solve the rotations I just used your script to align the bone roll. I worked perfectly.
- then I switched the source armature into "Rest Position", to see where it is in space without any pose animation. I moved the target armature into the same spot (down) in edit-mode. I also moved the mesh in edit mode, to fit the armature. I aligned them on the "Hips" bone. Because both armatures have different proportion, you will have to adjust the animation, so the target touches ground correctly.
- finally I parented the mesh to target armature again choosing "Armature Deform" option and I tested the result
@Tak Basically the answer already there is correct. It just needed fixing the scale and aligning the roll of bones.