An alternative to the other posted answer which does header
parsing. This means someone could still include other data behind a valid header.
Is to verify the whole file which costs more CPU but also has a stricter policy. A library that can do this is python audiotools and the relevant API method is AudioFile.verify.
Used like this:
import audiotools
f = audiotools.open(filename)
try:
result = f.verify()
except audiotools.InvalidFile:
# Invalid file.
print("Invalid File")
else:
# Valid file.
print("Valid File")
A warning is that this verify
method is quite strict and can actually flag badly encoded files as invalid. You have to decide yourself if this is a proper method or not for your use case.