Reputation: 4419
Is there any Python library that I can use to detect if a file is a video? I am letting users upload videos to my site and I want to prevent images and documents and anything except video filetypes. I my plan right now it to upload any file, test it, if it isn't a video delete it, if it is process it through the normal pipeline. I'd love to test for corrupt video somehow but that is another matter.
I need to be able to support videos without extensions. I had thought the mimetypes library could do this but when I do the following:
import mimetypes
url = 'http://thehighlightnetwork.appspot.com/serve/AMIfv94NsD5mUOwE60RnMSsBKVUsgilNyNJZawl30CRqVnLad7HkNeMmdBQCMhiAOXc0N9onNGjAM19TTVENEPjAwpeQZ6dq25CWjD5DzQXoK0c4IaPum_L-83EQS4SeUNqOCEYleHTskfkhfC8BXZJQtlyA99g2nN9lrfCXWrNGYPtjDeZETEQ'
print mimetypes.guess_type(url)
I get:
(None, None)
I am using Google App Engine so I have all the built in libraries but can add nearly anything too.
Upvotes: 1
Views: 4994
Reputation: 18455
Install python-magic
, OS-independent
pip install python-magic
Dependencies for Windows and OSX
On Windows, you need to download and save the following libraries under C:\Windows\System32:
regex2.dll from sourceforge.net/projects/gnuwin32/files/regex/
zlib1.dll from sourceforge.net/projects/gnuwin32/files/zlib/
magic1.dll from sourceforge.net/projects/gnuwin32/files/file/
On OSX:
When using Homebrew: brew install file-formula
When using macports: port install file
Then execute the code in python:
import magic
magic.from_file("path/to/file/filename.extension")
# usage example
magic.from_file("test/test.avi", mime=True)
For any further details refer Python-Magic
Output when i ran:
>>> import magic
>>> magic.from_file("test.avi")
'RIFF (little-endian) data, AVI, 320 x 240, 25.00 fps, video: XviD, audio: MPEG-1 Layer 3 (stereo, 22050 Hz)'
Upvotes: 2
Reputation: 43533
On UNIX systems, there exists a program for identifying files. It is called file
and matches patterns from a plain-text database.
If google app-engine gives you access to libmagic (which I doubt but I'm not familiar with it), you can install python-magic to use that.
Otherwise, probably the best way is to download the signature database. You can find e.g. the one used by FreeBSD for videos online. The documentation of tha file's format is also online.
Using these signature you should be able to detect the type of a file by reading the start of the file and matching it with the signatures.
Upvotes: 1