Created
July 16, 2010 09:24
-
-
Save ik5/478182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# gem install ruby-filemagic | |
# please note that this program works only on Unix based systems and Linux. | |
# It uses the "file command" library (and information) in a native code to validate file content (rather then extension). | |
# Important note: It only read the *header* of the files rather the whole, content so we might still have malicious content ... :( | |
require 'rubygems' | |
require 'filemagic' | |
# I use it to detect if the audio files are valid for Asterisk | |
# This supports only wav and mp3 at the moment. I can not detect gsm files it returns "data", so no support will be added. | |
def valid_audio?(file) | |
fm = FileMagic.new | |
magic = fm.file(file).upcase | |
fm.close # free memory, specially if we are using a daemon or webapp ... | |
# wav file | |
return true if ((magic.include? 'WAVE AUDIO' ) && (magic.include? 'MONO') && (magic.include? '8000 HZ')) || | |
# mp3 | |
((magic.include? 'MPEG') && (magic.include? 'LAYER III') && (magic.include? '8 KHZ')) | |
false | |
end; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment