Using Machine Learning to Predict Musical Scales
From a very high level -- You can convert the song to a spectrogram, there are a large number of implementations to do this. From there you can analyze the sound waves. In the case of the key, for instance, the note A is equal to 440 hz. Look into FFT as well.  Hope this helps get you started. I know spotify trains neural networks on spectrograms of songs to find similar songs based on "sound".