MacOS, export email folder with each message to its own file
Procmail is a possibility, but a) it's no longer maintained, and b) it may or may not run under macos. There are some other programs listed on procmail's Wikipedia page that may be useful. 

But, just as a guess, I think you might have to "roll your own". If you know Python, you can use the mailbox library.