Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a audio file is a valid as MP3 in shell

Tags:

bash

shell

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

like image 202
Robin pilot Avatar asked Apr 25 '15 13:04

Robin pilot


1 Answers

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.

like image 99
Mike Avatar answered Sep 20 '22 14:09

Mike