Adding to my first reply - there’s also a free plugin available on the forums here that supposedly does it (though I haven’t tried in in a while)…
https://forums.unrealengine.com/showthread.php?89191-Auto-Terrain-Texturer-Version-2-Release
That link that mittense provided looks very detailed and should give you a good understanding of the theory and all that behind it to apply it to UE4