I think you can add the transformation inside the model as long as scripting the model doesn’t complain about unsupported methods. A quick check of transforms.MelSpectrogram shows that PyTorch methods seem to be used, so I guess it would work.
If I’m not mistaken, @tom also used a similar workflow by adding transformations into the model to export it to his mobile in a blog post 5.