I am writing a Bash Shell Script that needs to determine if a provided MP3 Audio file is valid or invalid. How would I accomplish this in Bash?
eg fake: > file.mp3
or mv file.txt file.mp3
It depends on how certain you want to be.
Just Check the extension: You can easily check just the extension of the provided file in Bash with:
if [ ${file: -4} == ".mp3" ]
Which essentially takes the last four characters of the string file
and asserts that it equals .mp3
Check the File Headers:
Slightly more assured, this will check a small portion of the actual file data. You can do this by checking the Mime-Type of the file using the file
function in Bash. This will give you, well, the Mime-Type.
However, these can be spoofed, and you will not know for certain if the actual data in the file is valid. To do this you would need to do deep inspection of the binary data, and, likely, actually decode it. This is not something you can do in a simple Bash Script.
Check the file data its-self: You could use FFMpeg and FFProbe at the command line to test the files contents. I think the best bet is FFProbe, as it will give you a lot of data about the file. If you still have questions, please attempt and add your script to your question to receive more help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With