Detecting if a file is a video in Python? -


is there python library can use detect if file video? letting users upload videos site , want prevent images , documents , except video filetypes. plan right upload file, test it, if isn't video delete it, if process through normal pipeline. i'd love test corrupt video somehow matter.

i need able support videos without extensions. had thought mimetypes library when following:

import mimetypes  url = 'http://thehighlightnetwork.appspot.com/serve/amifv94nsd5muowe60rnmssbkvusgilnynjzawl30crqvnlad7hknemmdbqcmhiaoxc0n9onngjam19ttvenepjawpeqz6dq25cwjd5dzqxok0c4iapum_l-83eqs4seunqoceylehtskfkhfc8bxzjqtlya99g2nn9lrfcxwrngyptjdezeteq' print mimetypes.guess_type(url) 

i get:

(none, none) 

i using google app engine have built in libraries can add too.

install python-magic, os-independent

pip install python-magic 

dependencies windows , osx

on windows, need download , save following libraries under c:\windows\system32:

regex2.dll sourceforge.net/projects/gnuwin32/files/regex/ zlib1.dll sourceforge.net/projects/gnuwin32/files/zlib/ magic1.dll sourceforge.net/projects/gnuwin32/files/file/ 

on osx:

when using homebrew: brew install file-formula when using macports: port install file 

then execute code in python:

import magic magic.from_file("path/to/file/filename.extension")  # usage example magic.from_file("test/test.avi", mime=true) 

for further details refer python-magic

output when 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)'  

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -